0
点赞
收藏
分享

微信扫一扫

6-1 多重继承派生类构造函数

小飞侠熙熙 2022-04-29 阅读 199
c++

6-1 多重继承派生类构造函数

分数 20

根据所给的基类Student和Teacher,定义Graduate类

类定义:

#include <iostream>
#include <string>
using namespace std;
class Teacher                          
 {public:                                 
   Teacher(string nam,int a,string t)      
    {name=nam;
     age=a;
     title=t;}
   void display()                         
     {cout<<"name:"<<name<<endl;
      cout<<"age"<<age<<endl;
      cout<<"title:"<<title<<endl;
     }
  protected:                          
    string name;
    int age;
    string title;                      
};

class Student                         
 {public:
   Student(string nam,char s,float sco)
     {name1=nam;
      sex=s;
      score=sco;}                        
   void display1()                      
    {cout<<"name:"<<name1<<endl;
     cout<<"sex:"<<sex<<endl;
     cout<<"score:"<<score<<endl;
    }
  protected:                             
   string name1;
   char sex;
   float score;                           
 };
 
 /* 请在这里填写答案 */

/* 请在这里填写答案 */

裁判测试程序样例:

int main( )
 {Graduate grad1("Wang-li",24,'f',"assistant",89.5,1234.5);
  grad1.show( );
  return 0;
}

输出样例:

name:Wang-li
age:24
sex:f
score:89.5
title:assistant
wages:1234.5
class Graduate:public Teacher, public Student
{
protected:
	float wages;
public:
	Graduate(string name,int age,char sex,string title,float score,float wages):Teacher(name,age,title),
		Student(name,sex,score)
	{
		this->wages = wages;
	}
	void show()
	{
		cout << "name:" << this->name<< endl;//vs2019 如果这里直接用this->name 会报错  pta可以直接用
		cout << "age:" << this->age << endl;
		cout << "sex:" << this->sex << endl;
		cout << "score:" << this->score << endl;
		cout << "title:" << this->title << endl;
		cout << "wages:" << this->wages << endl;
	}
};
举报

相关推荐

0 条评论