关闭

虚基态程序

标签: 虚基态
65人阅读 评论(0) 收藏 举报
分类:
#include <iostream>
#include<string>
using namespace std;
class Person
{public:
Person(string nam,char s,int a)
{name=nam;
     sex=s;
age=a;}
protected:
    string name;
char sex;
int age;
};
//声明Person的派生类Teacher
class Teacher:virtual public Person
{
public:
Teacher(string nam,char s,int a,string t):Person(name,sex,age),title(t){}
protected:
string title;
};//声明派生类


//声明Person的派生类Student
class Student:virtual public Person
{
public:Student(string name,char s,int a,float sco):Person(name,sex,age),score(sco){}
protected:float score;
};


//声明多重继承的派生类 Graduate
class Graduate:public Teacher,public Student
{
public:Graduate(string nam,char s,int a,string 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<<endl;
  cout<<age<<endl;
  cout<<sex<<endl;
  cout<<title<<endl;
  cout<<wage<<endl;
  }
private:
float wage;
};




int main(){
  Graduate gradl("wang-li",'f',24,"assistant",89.5,1234.5);
  gradl.show();
  return 0;//可以访问基类中的公用成员 s2.age就会出错,因为age属于保护类成员数据,不能被类外访问
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:645次
    • 积分:69
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档