Solved

Basic Polimorphism question

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

786 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