Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Basic Polimorphism question

Posted on 2006-11-10
5
Medium Priority
?
241 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
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 1000 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 1000 total points
ID: 17919897
As with your other Q, there is no problem unless something else is fundamentally going wrong.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 clear a vector as well as how to detect empty vectors in C++.

916 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