本文共 1000 字,大约阅读时间需要 3 分钟。
虚基类应用举例
#include#include using namespace std;class Person{public: Person(char *nam,char s,int a) //构造函数 { strcpy(name,nam); sex=s; age=a; }protected: //保护成员 char name[20]; char sex; int age;};class Teacher:virtual public Person //声明Person为公用继承的虚基类{public: Teacher(char *nam,char s,int a,char *t):Person(nam,s,a) //构造函数 { strcpy(title,t); }protected: //保护成员 char title[10];}; //职称class Student:virtual public Person //声明Person为公用继承的虚基类{public: Student(char *nam,char s,int a,float sco): //构造函数 Person(nam,s,a),score(sco) {} //初始化表protected: //保护成员 float score;};//成绩class Graduate:public Teacher,public Student //声明Teacher和Student类为公用继承直接基类{public: Graduate(char *nam,char s,int a,char *t,float sco,float w): //构造函数 Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w) {} //初始化表 void show( ) //输出研究生的有关数据 { cout<<"name:"< <
转载地址:http://udmia.baihongyu.com/