분류 전체보기 (164) 썸네일형 리스트형 []연산자 오버로딩 #include #include using namespace std; class CIntArray { private: int* m_pnData; int m_nSize; public: CIntArray(int nSize) { m_pnData = new int[nSize]; memset(m_pnData, 0, sizeof(int) * nSize); } ~CIntArray() { delete m_pnData; } int Get(int index) { return m_pnData[index]; } void Set(int index, int value) { m_pnData[index] = value; } int& operator[](int index) { return m_pnData[index]; } }; int.. 클래스 사용해서 이름과 점수 입력받고 정렬하기 #include #include using namespace std; class Play { public: string name; int score = 0; Play() {} Play(string name, int score) { this->name = name; this->score = score; } bool operator score > N; Play* plays = new Play[N]; for (size_t i = 0; i > plays[i].name; cin >> plays[i].score; } sort(plays, plays + N);.. +연산자,cout <<오버로딩 #include using namespace std; class MyClass { int num = 0; public: int Get() { return num; } void Set(int a) { num = a; } MyClass operator+ (MyClass y) { MyClass res; res.num = this->num + y.num; return res; } friend ostream& operator 생성자 종류 #include using namespace std; class Person { private: int age; public: // 기본생성자 Person() { cout explicit - 명시적형변환(자동형변환을 금지한다) #include using namespace std; class Person { private: int age; public: explicit Person(int _age) { age = _age; } void Print() { cout 복사 생성자 #include using namespace std; class Person { private: char* name; public: Person(const char* str) { name = new char[strlen(str) + 1]; strcpy_s(name, strlen(str) + 1, str); } Person(const Person& per) { name = new char[strlen(per.name) + 1]; strcpy_s(name, strlen(per.name) + 1, per.name); } void Show() const { std::cout Show(); return 0; } 프렌드 함수 #include using namespace std; class MyClass { int num = 0; public: MyClass() {} MyClass(int a) : num(a) {} void Show() { cout Buyer Seller 함수 구현 #include using namespace std; class Buyer { public: int apple_num = 0; int money = 0; void Buy(int price,int number); }; void Buyer::Buy(int price, int number) { money -= price; apple_num += number; } class Seller { public: int apple_num = 0; int money = 0; void Sell(int price, int number); }; void Seller::Sell(int price, int number) { apple_num -= number; money += price; } void Trade(Buyer& buy.. 이전 1 ··· 10 11 12 13 14 15 16 ··· 21 다음