PDA

Click to See Complete Forum and Search --> : Inheritance in C++ (urgent)


VB_HELP
May 8th, 2001, 11:50 AM
I have a Class Account with some functions like
withdraw , deposit, Print as public functions
and I have also classes called Checking , Savings which are inherited from the above ones . I have overided the print function of Class Account in both Checking and Savings classes.
But my below code does not work properly.

Account *A[10];

A[1] = new Checking();
A[2] = new Savings();

When I reference A[1]->print() sometimes it is giving some error or just printing from Account but not coming from Checking or savings
Can Any one Help me :(

thanks

VB_HELP
May 8th, 2001, 12:06 PM
if my question was not clear please let me know
can Anyone help me in this ?

parksie
May 8th, 2001, 12:13 PM
For this you need to use virtual inheritance, which I explained somewhere else....do a search for Base and Derived :)

Make the public functions that get overridden virtual:

class Base {
public:
virtual void Print() { ... }
};

VB_HELP
May 8th, 2001, 12:23 PM
Thanks,
I guess it works out

PsyVision
May 9th, 2001, 04:13 PM
Like:

class CObject
{
public:
CObject();
~CObject();

virtual void draw() {}
};

class CCube : public CObject
{
public:
CCube();
~CCube();

void Draw()
{
// Drawing Code
}
};

:D Sorry if its a bit late etc but what the hell

parksie
May 9th, 2001, 04:15 PM
Don't see why people put "C" in front of class names, but oh well ;)

Anyway, that's just normal inheritance. Virtual inheritance is calling a derived class member function through a base class pointer. The compiler magically knows to call the right one :) Cool really :D

parksie
May 9th, 2001, 04:21 PM
http://www.vbforums.com/showthread.php?s=&threadid=45422

At the bottom :)