当前位置:编程学习 > C/C++ >>

虚基类释义

#include "stdafx.h"

#include<iostream>
#include<string>

using namespace std;

class person{//声明基类
protected:
 int age;
 char 易做图;
 string name;
public:
 person(int a,char s,string nam){
  age=a;
  易做图=s;
  name=nam;
 }
};
class teacher:virtual public person
{
protected:
 string title;
public:
 teacher(int a,char s,string nam,string t):person(a,s,nam){
  title=t;
 }
};
class student:virtual public person
{
protected:
 float score;
public:
 student(int a,char s,string nam,float sc):person(a,s,nam){
  score=sc;
 }
};
class graduate:public teacher,public student
{
protected:
 float wdge;
public:
 graduate(int a,char s,string nam,string t,float sc,float wd):person(a,s,nam),teacher(a,s,nam,t),student(a,s,nam,sc){
  wdge=wd;
 }
 void show(){
  cout<<name<<endl;
  cout<<age<<endl;
  cout<<易做图<<endl;
  cout<<title<<endl;
  cout<<score<<endl;
  cout<<wdge<<endl;
 }
};
int main(){
 graduate gr(22,f,"k;asdjf;daf","klsdaf",89.5,79.5);
 gr.show();
 return 0;
}

输出结果:

k;asdjf;daf
22
f
klsdaf
89.5
79.5
Press any key to continue

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,