inheritance problem

C

cmk128

Hi

#include <stdio.h>
#include <string>
using namespace std;

class mother{
private:
string str;
public:
mother(){
str="peter cheung";
}

string getStr(){
return str;
}
};

class child:public mother{
};

class child2:public child2{
};

void func1(mother *m){
}

int main(){
mother *m=new child2(); <------------ this will fail, but
this can, mother *m=new child()
child2 c;
printf("str=%s\n",m->getStr().c_str());
func1(&c); <------------- this will fail too
because c is from class child2, not child . java can accept parameter
of all its sub-class, whatever how depth it is inheritanced. do we have
anyway to solve it?
return 1;
}

thanks
from Peter ([email protected])
 
Z

ZikO

class child2:public child2{

Everything looks fine if you change child2 on child, I mean this:

class child2:public child2{

on this:

class child2:public child {

All the rest looks fine and compiles correctly, as far as the result of this
program is what you wanted to have :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

child list. . 5
parent's child list 5
help for inheritance problem 2
[Newbie] Namespace and inheritance... 4
Java matrix problem 3
Infinite loop problem 1
Virtual function 2
Inheritance and friendship 4

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
BrentonMcc
Top