Solved

Basic Polimorphism question

Posted on 2006-11-10
5
192 Views
Last Modified: 2010-04-01
Hi Experts,

I have the following

class A
{
};

class B : public A
{
};

class C: public B
{

}

class D
{
public:
void someFunction(A *a);

};

Can I do the following??

A *aa = new A( );
B *bb = new B( );
C *cc = new C( )
someFunction(aa);

someFunction(bb);
someFunction(cc);
0
Comment
Question by:ambuli
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
Comment Utility
Yep.  Inheritance is the "is-a" relationship.  

aa "is a" A.
bb "is a" A, since a B "is a" A.

Anything that' s a subclass of A can be used where an A is expected.

Incidentally, that's not even polymorphism, that's just straight inheritance.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Yes, of course you can:

class A
{
};

class B : public A
{
};

class C: public B
{

};

class D
{
public:
void someFunction(A *a);

};

void D::someFunction(A* a)
{

}

int main ()
{

A *aa = new A( );
B *bb = new B( );
C *cc = new C( );

D d;
d.someFunction(aa);

d.someFunction(bb);
d.someFunction(cc);

return 0;
}

C:\tmp\cc>cl baspoly.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

baspoly.cpp
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:baspoly.exe
baspoly.obj

But, keep in mind that you only will be able to access the 'A' aspect of teh object passed in, everything else would require a typecast.
0
 

Author Comment

by:ambuli
Comment Utility
Again, the same problem.  If one of the object is multiple inherited...   Sorry, I was thinking the multiple inheritiance shouldn't matter and simpilified the problem.  So, I guess, I cannot do this with multiple inheritance...

class A{};
class B: public A{};
class X:public A{};
class C: public B, public X{};
0
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 250 total points
Comment Utility
All those things still qualify as an instance of A.

Multiple inheritance just means that a C there can pass anywhere you need a B (which can pass anywhere you need an A) and it can also pass anywhere you need an X (which, completely unrelated, can also be used anywher eyou need an A).

If class X had no parent, you could still pass a C* to a function that wanted an A*.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
As with your other Q, there is no problem unless something else is fundamentally going wrong.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now