So I made this class:
class Book
{
public:
Book(string newTitle = "???", string newAuthor = "???");
virtual ~Book();
string getTitle();
string getAuthor();
void setTitle(string newTitle);
void setAuthor(string newAuthor);
virtual string allInfo();
private:
string title;
string author;
};
And I was going to cover the allInfo()
-function in two other classes
One called HardcoverBooks
and another called AudioBooks
. Both inheriting from Book
.
This is what I did subsequently in the .cpp
files in both classes, first the AudioBook
class:
string AudioBook::allInfo(){
stringstream newString;
newString<<"Title: "<<this->title<<endl<<"Author: "<<this->author<<endl
<<"Narrator: "<<this->narrator<<endl
<<"Length(in minutes): "<<this->length<<endl<<endl;
return newString.str();
}
And this in the HardcoverBook
class:
string HardcoverBook::allInfo(){
stringstream newString;
newString<<"Title: "<<this->title<<endl<<"Author: "<<this->author<<endl
<<"Pages: "<<this->pages<<endl<<endl;
return newString.str();
}
Everything is working fine and dandy,except that the AudioBook
class is complaining about this:
include\Book.h||In member function 'virtual std::string AudioBook::allInfo()':| include\Book.h|41|error: 'std::string Book::title' is private| mningsuppgiftIIB\src\AudioBook.cpp|27|error: within this context| include\Book.h|42|error: 'std::string Book::author' is private| mningsuppgiftIIB\src\AudioBook.cpp|27|error: within this context| ||=== Build finished: 4 errors, 0 warnings ===|
But in HardcoverBook
it doesn't complain about this at all,strangely enough.
My questions:
What do I do to make this work? (i.e. to make both classes be able to use the function
allInfo()
in their own way)Why doesn't it work like this?
EDIT: This is some homework I'm doing,and one of the requirements is to make the member variables and properties private. So protected does work,kudos for that guys,but I'll add another bonus question:
- How do I make it work with private member variables?