New in C++:Abstract Data Types(ADT) - Base Class

S

SunScreen

Hi all,

I would like to help with the following:

// my conventions:
// BC = Base Class
// DC = Derived Class

class BC // this ADT base class or interface
{
BC(){}
~BC(){}
bcFunc1() = 0;
bcFunc2() = 0;
}

class DC : public BC
{
DC(){}
~DC(){}
bcFunc1() { // Do something }
bcFunc2() { // Do something }

dcFunc() { // Do something }
}

int main()
{
// I declare a pointer to BC assigned to DC object
BC* myBC = new DC;

myBC->bcFunc1();
myBC->bcFunc2();

// now the tricky part - the following line will not compile
// myBC->dcFunc();

// one way to handle this is using the dynamic_cast operator
DC* myDC = dynamic_cast<DC*> (myBC);

// now I can call the DC function
myDC->dcFunc();

delete myBC;
delete myDC;
}

I know that using the dynamic_cast operator is bad programming code.
Is there any other way call the DC function?


Thanx in Advance,
Sun
 
J

John Harrison

SunScreen said:
Hi all,

I would like to help with the following:

// my conventions:
// BC = Base Class
// DC = Derived Class

class BC // this ADT base class or interface
{
BC(){}
~BC(){}
bcFunc1() = 0;
bcFunc2() = 0;
}

class DC : public BC
{
DC(){}
~DC(){}
bcFunc1() { // Do something }
bcFunc2() { // Do something }

dcFunc() { // Do something }
}

int main()
{
// I declare a pointer to BC assigned to DC object
BC* myBC = new DC;

myBC->bcFunc1();
myBC->bcFunc2();

// now the tricky part - the following line will not compile
// myBC->dcFunc();

// one way to handle this is using the dynamic_cast operator
DC* myDC = dynamic_cast<DC*> (myBC);

// now I can call the DC function
myDC->dcFunc();

delete myBC;
delete myDC;
}

I know that using the dynamic_cast operator is bad programming code.
Is there any other way call the DC function?

Well you could use static_cast, but that is equally 'bad programming code'.
These things are necessary sometimes but the reason they are considered bad
practice is because the need to call a dervied class member function though
a base class pointer when the member function is not present and virtual in
the base class, is normally considered bad design.

What need do you have to do this? I'm not saying that you shouldn't, I'm
just saying that you should think carefully about it. It may be that you can
avoid this by redesigning your code.

john
 
H

Howard

SunScreen said:
Hi all,

I would like to help with the following:

// my conventions:
// BC = Base Class
// DC = Derived Class

class BC // this ADT base class or interface
{
BC(){}
~BC(){}
bcFunc1() = 0;
bcFunc2() = 0;
}

class DC : public BC
{
DC(){}
~DC(){}
bcFunc1() { // Do something }
bcFunc2() { // Do something }

dcFunc() { // Do something }
}

int main()
{
// I declare a pointer to BC assigned to DC object
BC* myBC = new DC;

myBC->bcFunc1();
myBC->bcFunc2();

// now the tricky part - the following line will not compile
// myBC->dcFunc();

// one way to handle this is using the dynamic_cast operator
DC* myDC = dynamic_cast<DC*> (myBC);

// now I can call the DC function
myDC->dcFunc();

delete myBC;
delete myDC;
}

I know that using the dynamic_cast operator is bad programming code.
Is there any other way call the DC function?


Thanx in Advance,
Sun

Yes, a couple of ways. For one, you could make a virtual function dcFunc()
in the base class that does nothing (or is abstract virtual). Or, you could
make a new virtual function in both classes, then have it do nothing (or be
abstract virtual) in the base class but have it call dcFunc() in the DC
class. Either way, you use a virtual function in the base class in order to
call its overridden function in the derived class.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top