728x90
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
class Book {
private:
double price;
int pages;
char* title;
char* author;
public:
Book(double price, int pages, const char* title, const char* author);
~Book();
void changeTitle(const char* title);
void show();
};
Book::Book(double price, int pages, const char* title, const char* author)
{
this->price = price;
this->pages = pages;
int len_t = strlen(title);
this->title = new char[len_t + 1];
strcpy(this->title, title);
int len_a = strlen(author);
this->author = new char[len_a + 1];
strcpy(this->author, author);
}
Book::~Book()
{
if (title)
delete[] title;
if (author)
delete[] author;
}
void Book::changeTitle(const char* title)
{
if (strlen(title) > strlen(this->title))
return;
strcpy(this->title, title);
}
void Book::show()
{
cout << "가격 : " << price << endl;
cout << "페이지 수 : " << pages << endl;
cout << "제목 : " << title << endl;
cout << "저자 : " << author << endl << endl;
}
int main()
{
cout << "2018305065 전유정" << endl;
Book b1(15000.0, 500, "명품 C++ programming", "황기태");
Book b2(b1);
b1.show();
b2.show();
b2.changeTitle("C언어");
cout << "=========== 제목 변경 ==========" << endl;
b1.show();
b2.show();
}
반응형
'전공 공부 > C++' 카테고리의 다른 글
참조에 의한 호출과 참조를 리턴하는 함수 (char& find) (0) | 2021.05.26 |
---|---|
클래스 Circle 참조에 의한 호출 (0) | 2021.05.26 |
복사생성자 (0) | 2021.05.20 |
average 함수(call by address) (0) | 2021.05.20 |
참조 변수 (0) | 2021.05.13 |