아하
생활

생활꿀팁

기민한진도개272
기민한진도개272

c++ 에서 클랙식 상속오류 질문좀합니다

책의 예제를 보고 따라하는 중인데 상속 관련된 예제 중에

답지 그대로 실행했는데도 상속 에러가 떠서 질문드립니다..

어디서 에러가 난건가요?


#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); } void ShowBookInfo() { 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); } void ShowEBookInfo() { cout << "제목: " << title << endl; cout << "ISBN: " << isbn << endl; cout << "가격: " << price << endl; cout << "인증키: " << DRMKey << endl; } ~EBook() { delete[] DRMKey; }};
int main(){ Book book("좋은 C++", "555-12345-890-0", 20000); book.ShowBookInfo(); cout<<endl; EBook ebook("좋은 C++ ebook", "555-12345-890-1", 10000, "fafawfwef"); ebook.ShowEBookInfo();
return 0;}


- 에러메세지:


exit status 1

main.cpp: In member function 'void EBook::ShowEBookInfo()':

main.cpp:44:35: error: 'char* Book::title' is private within this context

cout << "제목: " << this->title << endl;

^~~~~

main.cpp:8:11: note: declared private here

char *title;

^~~~~

main.cpp:45:33: error: 'char* Book::isbn' is private within this context

cout << "ISBN: " << this->isbn << endl;

^~~~

main.cpp:9:11: note: declared private here

char *isbn;

^~~~

main.cpp:46:35: error: 'int Book::price' is private within this context

cout << "가격: " << this->price << endl;

^~~~~

main.cpp:10:9: note: declared private here

int price;

^~~~~

main.cpp: In function 'int main()':

main.cpp:57:51: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

Book book("좋은 C++", "555-12345-890-0", 20000);

^

main.cpp:57:51: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

main.cpp:60:72: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

EBook ebook("좋은 C++ ebook", "555-12345-890-1", 10000, "fafawfwef");

^

main.cpp:60:72: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

main.cpp:60:72: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

55글자 더 채워주세요.
1개의 답변이 있어요!
  • 수줍은이구아나17
    수줍은이구아나17

    일단 대부분의 에러가

    main에서 일어났는데요

    ebook, book에서 private으로 되어있는 변수들을 가져다 쓸 수 없다고 하네요 public으로 변경해서 하시던지

    public으로 이루어진 함수를 이용해서 원하는부분만 보여주게 하셔야겠네요