#include <iostream>
#include <cstring>
using namespace std;
class Book
{
private:
char* title;
char* isbn;
int price;
public:
Book(char* title, char* isbn, int value)
:price(value)
{
this->title = new char[strlen(title)+1];
this->isbn = new char[strlen(isbn)+1];
strcpy(this->title, title);
strcpy(this->isbn, isbn);
}
explicit Book(const Book& ref) //복사 생성자
:price(ref.price)
{
title = new char[strlen(ref.title)+1];
isbn = new char[strlen(ref.isbn)+1];
strcpy(title, ref.title);
strcpy(isbn, ref.isbn);
}
Book& operator=(const Book& ref) //대입 연산자
{
delete []title;
delete []isbn;
price = ref.price;
title = new char[strlen(ref.title)+1];
isbn = new char[strlen(ref.isbn)+1];
strcpy(title, ref.title);
strcpy(isbn, ref.isbn);
return *this;
}
void ShowBookInfo() const
{
cout<<"제목: "<<title<<endl;
cout<<"ISBN: "<<isbn<<endl;
cout<<"가격: "<<price<<endl;
}
~Book()
{
delete []title;
delete []isbn;
}
};
class EBook :public Book
{
private:
char* DRMKey;
public:
EBook(char* title, char* isbn, int value, char* key)
:Book(title, isbn, value)
{
DRMKey = new char[strlen(key)+1];
strcpy(DRMKey, key);
}
EBook(const EBook& ref) //복사 생성자
:Book(ref) //기초클래스의 복사 생성자 호출
{
DRMKey = new char[strlen(ref.DRMKey)+1];
strcpy(DRMKey, ref.DRMKey);
}
EBook& operator=(const EBook& ref) //대입 연산자
{
delete []DRMKey;
Book::operator=(ref); //기초클래스의 대입 연산자 호출.
DRMKey = new char[strlen(ref.DRMKey)+1];
strcpy(DRMKey, ref.DRMKey);
return *this;
}
void ShowEBookInfo()
{
ShowBookInfo();
cout<<"인증키: "<<DRMKey<<endl;
}
~EBook()
{
delete []DRMKey;
}
};
int main(void)
{
Book book("zzzz", "adf2342343", 23434);
book.ShowBookInfo();
cout<<endl;
EBook ebook("asdf", "s234234", 23423, "1234ee");
ebook.ShowEBookInfo();
cout<<endl<<endl;
///////////////////////////////////////////////////////////////////
Book book1(book); //복사생성자 호출
book1.ShowBookInfo();
cout<<endl;
Book book2("thg", "234", 234221);
book1 = book2; //대입 연산자 호출
book1.ShowBookInfo();
cout<<endl;
///////////////////////////////////////////////////////////////////
EBook ebook1("asdf", "wer", 123, "qwe");
ebook1 = ebook; //대입연산자 호출 ebook1.operator=(ebook);
ebook1.ShowEBookInfo();
cout<<endl;
EBook ebook2(ebook1); //복사생성자호출 ebook2 = ebook1;
ebook2.ShowEBookInfo();
return 0;
}