虚基类的程序,挺好的

作者在 2011-06-18 16:55:38 发布以下内容
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
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:virtual 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)
             <<setw(7)<<grade<<setw(17)<<specialty<<endl;
}

};
class teacher:virtual public person
{
protected:
char title[15];
char specialty[20];
public:
teacher(char *na,int y, char *ti,char*spec):person(na,y)
{
strcpy(title,ti);
strcpy(specialty,spec);
}
void display(int this_year)
{
cout<<"            teacher   age         title       specialty\n";
cout<<setw(20)<<name<<setw(5)<<cal_age(this_year);
cout<<setw(14)<<title<<setw(17)<<specialty<<endl;
}
};
class in_service_graduate:public teacher,public graduate
{
public:
in_service_graduate(char *na,int y,char*ti,char*spec1,int g,char*spec2):teacher(na,y,ti,spec1),graduate(na,y,g,spec2),person(na,y){}
void display(int this_year)
{
         cout<<"in_service_graduate    age      title   work_specialty grade study_specialty\n";
cout<<setw(20)<<graduate::name<<setw(5)<<graduate::cal_age(this_year);
cout<<setw(14)<<teacher::title;
cout<<setw(10)<<teacher::specialty<<setw(10)<<graduate::grade<<setw(10)<<graduate::specialty<<endl;
}
};
int main()
{
graduate gr("zhang_ling",1978,2001,"computer");
teacher te("wang_qiang",1976,"tutor","electronics");
in_service_graduate sg("liu_hua",1975,"lectuer","automation",2002,"computer");
gr.display(2002);
cout<<endl;
te.display(2002);
cout<<endl;
sg.display(2002); 
system("pause");
return 0;
}
默认分类 | 阅读 1141 次
文章评论,共2条
zhongjiezhe
2011-06-20 08:51
1
cout&lt;&lt;setw(20)&lt;&lt;graduate::name&lt;&lt;setw(5)&lt;&lt;graduate::cal_age(this_year);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;setw(14)&lt;&lt;teacher::title;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;setw(10)&lt;&lt;teacher::specialty&lt;&lt;setw(10)&lt;&lt;graduate::grade&lt;&lt;setw(10)&lt;&lt;graduate::specialty&lt;&lt;endl;<br />
继承以后可以不加作用域::直接写成<br />
cout&lt;&lt;setw(20)&lt;&lt;name&lt;&lt;setw(5)&lt;&lt;cal_age(this_year);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;setw(14)&lt;&lt;title;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;setw(10)&lt;&lt;teacher::specialty&lt;&lt;setw(10)&lt;&lt;grade&lt;&lt;setw(10)&lt;&lt;graduate::specialty&lt;&lt;endl;<br />
speciality要加::是因为在两个子类中使用了相同的变量定义
lianjiecuowu(作者)
2011-06-20 11:52
2
<div class="quote"><span class="q"><b>zhongjiezhe</b>: cout&lt;&lt;setw(20)&lt;&lt;graduate::name&lt;&lt;setw(5)&lt;&lt;graduate::cal_age(this_year);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;setw(14)&lt;&lt;teacher::title;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&l</span></div>额。。。。我知道
游客请输入验证码