1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> #include <string> using namespace std; class Book { char *title; int price; public: Book(const char* title, int price) { this->price = price; int size = strlen(title); this->title = new char[size + 1]; strcpy(this->title, title); } Book(Book &b) { this->price = b.price; int size = strlen(b.title); this->title = new char[size + 1]; strcpy(this->title, b.title); } ~Book() { delete[]title; } /* (2) 디폴트 복사생성자 Book(Book &b){ this->title = title; this->price = price; } */ void set(const char* title, int price) { this->price = price; int size = strlen(title); this->title = new char[size + 1]; strcpy(this->title, title); } void show() { cout << title << ' ' << price << "원" << endl; } }; int main() { Book cpp("명품 C++", 10000); Book java = cpp; java.set("명품자바", 12000); cpp.show(); java.show(); } | cs |
'Programming > 명품 C++ Programming' 카테고리의 다른 글
| 명품 C++ Programming 실습문제 6장 1번 (0) | 2018.05.09 |
|---|---|
| 명품 C++ Programming 6장 OpenChallenge (0) | 2018.05.09 |
| 명품 C++ Programming 실습문제 5장 7번 (0) | 2018.05.09 |
| 명품 C++ Programming 실습문제 5장 6번 (0) | 2018.05.09 |
| 명품 C++ Programming 실습문제 5장 5번 (0) | 2018.05.09 |