Solved

Basic Polimorphism question

Posted on 2006-11-10
5
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17919777
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
ID: 17919788
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
ID: 17919880
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
ID: 17919887
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
ID: 17919897
As with your other Q, there is no problem unless something else is fundamentally going wrong.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

622 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