程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 虛基類釋義

虛基類釋義

編輯:關於C語言

#include "stdafx.h"

#include<iostream>
#include<string>

using namespace std;

class person{//聲明基類
protected:
 int age;
 char sex;
 string name;
public:
 person(int a,char s,string nam){
  age=a;
  sex=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<<sex<<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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved