c++继承问题...........

class Birthdate
{
public:
Birthdate(int y,int m,int d):year(y),month(m),day(d){}
void show_date()
{
cout<<"year/ "<<year<<endl;
cout<<"month/ "<<month<<endl;
cout<<"day/ "<<day<<endl;
}
private:
int year;
int month;
int day;
};
class Professor
{
public:
Professor(int m,int d):birthday(y,m,d){}
private:
Birthdate birthday;
};
void main()
{
Professor prof(2014,1,1);
prof.birthday.show_date();
}
代码如上,在professor类中定义了Birthdate 类的birthday对象,show_date()在类中定义时是public,在主函数为什么不能用prof.birthday.show_date(),对Birthdate类中的show_date()函数进行调用?

birthday是professor的私有成员,所以prof.birthday这样是不合法的啦!
应该在Professor类里边定义一个public的showdata函数,在这个函数里边调用birthday的show_data方法,这样即满足了封装又给出了接口。

这不是继承,好像叫组合吧。没有伸手代码,一下给你个思路

 class Birthdate
{
public:
Birthdate(int y,int m,int d):year(y),month(m),day(d){}
void show_date()
{
cout<<"year/ "<<year<<endl;
cout<<"month/ "<<month<<endl;
cout<<"day/ "<<day<<endl;
}

private:
int year;
int month;
int day;
};
class Professor : Birthdate
{
public:
Professor(int y,int m,int d): base(y,m,d){}

};
void main()
{
Professor prof(2014,1,1);
prof.show_date();
}