1.)Derive publication2 from publication.
2.)Derive Audio from publication2
3.)Derive Book from publication2
Intuitively, is appears to make senese, however the compiler does not like what it sees, giving me the following error: "non static member reference must be relative to specific object."
class Publication{
public:
void input(){
cout<<"Title: ";getline(cin,title);
cout<<"Price: ";cin>>price;
while(price<0){
cout<<"Price can't be less than zero, enter again: ";
cin>>price;
}
}
void print(){
cout<<"Title: "<<title<<endl;
cout<<"Price: "<<price<<endl;
}
private:
string title;
double price;
};
class Book:Publication2{
public:
void input(){
Publication2::input();//error: non static member reference must be relative to specific object
cout<<"Pages: ";cin>>pages;
while(pages<0){
cout<<"Can't be less than zero, enter again: ";
cin>>pages;
}
}
void print(){
Publication2::print();//error: non static member reference must be relative to specific object
cout<<"Pages: "<<pages<<endl;
}
private:
int pages;
};
class Audio:Publication2{
public:
void input(){
Publication2::input();//error: non static member reference must be relative to specific object
cout<<"Play time: ";cin>>play_time;
while(play_time<0){
cout<<"Can't be less than zero, enter again: ";
cin>>play_time;
}
}
void print(){
Publication2::print();//error: non static member reference must be relative to specific object
cout<<"Play time: "<<play_time<<endl;
}
private:
double play_time;
};
class Publication2:Publication{
public:
void input(){
Publication::input();
}
void print(){
Publication::print();
}
private:
};
int main()
{
_getch();
return 0;
}
Not sure if this is because you've omitted some code, but you might need to move the definition of Publication2 up above the classes that inherit from it. Besides that, it compiles for me on VS2008 with no errors besides not being able to find _getch().
Your definition of the derived class with access it's base class doesn't look right.
class Audio:Publication2
I would do this:
class Audio : public Publication2
which specifys public access to the base class. Maybe it setting it to public by default, I think this is bad form to take such a shortcut.
Do you split your code into different files? Header files for declaration of classes, and cpp files for definition.
If you do this, then you only need to include the header files for the objects you refer to. THis way, the order you put things in a file will never be a problem. At the moment, your function definitions are inline, which isn't a good thing if there's lots of code.
Also you don't have default constructors, it won't work if you create a derived object.
In main, all it does is call _getch(); should the the leading underscore be there? You need to include the header that this function is in. Even if it was right, it doesn't do anything at all with the result, not even assignment. You haven't created any objects either.