Computing.Net > Forums > Programming > Derive a class from derived class??

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Derive a class from derived class??

Reply to Message Icon

Name: v
Date: July 17, 2003 at 20:51:00 Pacific
OS: Win XP Pro SP1
CPU/Ram: Athlon 2200+
Comment:

Hey guys, this is quite an easy and obvious question, but I couldn't find the answer in my textbooks or online.

Here's a problem:

I have a class A, a class B derived from class A, and a class C derived from class B

class A
class B : public class A
class C : public class B

each of this classes has a method called print( ) , so it's like this:

class A
{ print() {...} ;
};

class B : public A
{
print() {
A::print() ;
... ;
}
};

And
class C : public B {
print( ) {
B::print() ;
... ;
} } ;

there's dynamic binding, so I have something like this

A * a = new B() ;
A * b = new C() ;
a->print() ;
b->print() ;

Logically, print() in class A shoud be a virtual function, e.g. virtual print ( ) ;

But what about print( ) in class B?? It's a base class for class C, afterall.

Also, when I make print( ) in either class virtual method, my program doesn't compile, it spews thousands of linking errors on my screen. But if I leave virtual out, print() doesn't work as desired, only print() of class A is called for objects a and b.


I'm tired of this. I can't ask anyone about it, because it's late at night, I can't find it on the Internet, because all examples portray classes derived from two different classes, but not from another derived class. My textbooks don't explain how to do it. I don't know how to link the damn thing so that it would work

thank you for any help.



Sponsored Link
Ads by Google

Response Number 1
Name: igork
Date: July 24, 2003 at 06:14:59 Pacific
Reply:

Hi.

If you are talking about C++ then:

1) it is enough to declare a method as a virtual one only in the base class , in all derived classes the method will be virtual too. So if you declare A::print() as a virtual, them B::print() and C::print() will be virtual too. On the other hand it wont cause any damage if you declare all the print() methods as virtual;

2) It would be very usefull if you supply an example of the error message you get.

3) As far as I understand from your question, if you declare A::print() as virtual it should solve the problem.


0

Response Number 2
Name: bamakhrama
Date: August 2, 2003 at 16:17:14 Pacific
Reply:

try this code:
class A
{
public:
A(){}
virtual print(){cout print();
b->print();

return 0;
}


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


C Prgramming help QBasic newbie question



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Derive a class from derived class??

How to find a jar given a class? www.computing.net/answers/programming/how-to-find-a-jar-given-a-class/10286.html

Searching a String inside a Class www.computing.net/answers/programming/searching-a-string-inside-a-class/16020.html

Win32: Reading a texts from a file www.computing.net/answers/programming/win32-reading-a-texts-from-a-file/11673.html