Solved

Basic Polimorphism question

Posted on 2006-11-10
5
219 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 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