Amonod 0 Newbie Poster

Hello. I am currently reading c++ primer plus 5th edition and I've become stuck at Chapter 13, Exercise 1, which is on class inheritance.

The classes looks like this:

class Cd {
.....
public: 
	...
	virtual void Report() const; 
	.... 
}; 

class Classic : public Cd
{
....
public:
	.....
	virtual void Report() const;
	.....
};

with the definitions looking like this:

void Cd::Report() const
{
	cout << "Performer(s): " << performers << endl;
	cout << "Label: " << label << endl;
	cout << "Selections: " << selections << ", playtime: " << playtime << endl;
}

void Classic::Report() const
{
	Cd::Report();
	cout << "Primary work: " << primary << endl;
}

and main invokes the virtual function like this

Cd c1("Beatles", "Capitol", 14, 35.5);
Cd *pcd = &c1; 
pcd->Report();

At this point the error message " The Debugger has exited due to signal 11 (SIGSEGV)" appears. I think what is happening is that the redefinition in the derived class is hiding the original Report function from the base class, and when the program tries to cout << primary it is trying to access memory which doesnt exist. Especially since if the call

pcd->Cd::Report();

is made instead, it prints fine, just lacking the extra element added by the Classics class. However, since the two classes have exactly the same prototype for this function this isn't supposed to happen, and since they are virtual, the implementation should decide which one to run based on what the pointer points to, in this case a Cd object. At this point through the book I am used to the solution being a small thing I have overlooked, but I am really stuck on this one. Any help would be greatly appreciated.

I tried to exclude any excess and unnecessary code to avoid clutter, but if I removed too much then I apologize and just tell me what else to add to the post and I will do so.

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.