Inheritance help

Hi guys,

Having an issue with inheritance.

Here's what I need.

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."

Anyone can identify my problem?

Thanks,

Mike

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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().
Hi Zhuge,

No, you are right, it's a matter of placing publication2 above the other classes, did not even occur to me.

Thanks a lot!

Mike
Hi,

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.

Let us know how you get on

Just wondering what is the purpose of the Publication2 class ?
Topic archived. No new replies allowed.