BookŬ·¡½º¿Í ¸ÞÀÎÇÔ¼ö »çÀ̸¦ ä¿ö¼ °á°ú´Â µµÃ⠵Ǵµ¥ this¾øÀÌ µ¿ÀÏÇÑ °á°ú¸¦ ³¾¼ö ÀÖÀ»±î¿ä?
#include <iostream>
#include <cstring>
using namespace std;
class Book {
char* title;
int price;
public:
Book(const char* title, int price);
Book(const Book& b);
~Book();
void set(const char* title, int price);
void show() { cout << title << ' ' << price << "¿ø" << endl; }
};
// ÀÛ¼ºÇÑ ³»¿ë : start
Book::Book(const char* title, int price) {
this->price = price;
int size = strlen(title) + 1;
this->title = new char[size];
strcpy(this->title, title);
}
Book::Book(const Book& b) {
this->price = b.price;
int size = strlen(b.title) + 1;
this->title = new char[size];
strcpy(this->title, b.title);
}
Book::~Book() {
delete[] title;
}
void Book::set(const char* title, int price) {
if (this->title) delete[] this->title;
this->price = price;
int size = strlen(title) + 1;
this->title = new char[size];
strcpy(this->title, title);
}
// ÀÛ¼ºÇÑ ³»¿ë : end
int main() {
Book cpp("C++", 10000);
Book java(cpp);
java.set("ÀÚ¹Ù", 12000);
cpp.show();
java.show();
}