c++子类输出父类成员问题

来源:百度知道 编辑:UC知道 时间:2024/06/29 21:38:36
(2)下面程序对应图1所示的类层次继承结构:

person

person

#include <iostream.h>

#include <iomanip.h>

#include <string.h>

graduate

teacher

class person

{

protected:

char name[20];

int birth_year;

in-service_graduate

public:

person(char *na, int year) {

strcpy(name,na);

birth_year=year;

}

int cal_age(int this_year) {

return this_year-birth_year;

}

};

class graduate :public person

{

protected:

int grade;

char specialty[20];

public:

graduate(char *na, int y, int g, char *spec):person(na,y) {

grade=g;

//VC6.0编译通过

#include <iostream.h>
#include <iomanip.h>
#include <string.h>
class person
{
protected:
char name[20];
int birth_year;
public:
person(char *na, int year) { strcpy(name,na); birth_year=year; }
int cal_age(int this_year) { return this_year-birth_year; }
};

class graduate :public person
{
protected:
int grade;
char specialty[20];
public:
graduate(char *na, int y, int g, char *spec):person(na,y) { grade=g; strcpy(specialty,spec); }
void display(int this_year)
{
cout<<" graduate age grade specialty\n";
cout<<setw(20)<<name<<setw(5)<<cal_age(this_year);
cout<<setw(7)<<grade<<setw(17)<<specialty<<endl;
}
};

class teacher :public person
{
protected:
char title[15];
char specialty[20];