您的位置:首页 > 其它

例4.6 当基类含有参数的构造函数,派生类构造函数的构造方法

2017-07-15 15:57 225 查看
// 例4.6 当基类含有参数的构造函数,派生类构造函数的构造方法。
#include<iostream>
#include<string>
using namespace std;
class Student{
public:
Student(int number1,string name1,float score1){
number = number1;
name = name1;
score = score1;
}
void print(){
cout<<"number:"<<number<<endl;
cout<<"name:"<<name<<endl;
cout<<"score:"<<score<<endl;
}
protected:
int number;  //学号
string name; //姓名
float score; //成绩
};

class UStudent:public Student{
public:
UStudent(int number1,string name1,float score1,string major1):Student(number1,name1,score1){//定义派生类构造函数时,缀上要调用的基类的构造函数及其参数
major = major1;
}
void print1(){
print();
cout<<"major:"<<major<<endl;
}
private:
string major; //专业
};

int main(){
UStudent stu(22116,"张国立",98,"计算机及应用");
stu.print1();
cout<<"-----------------------------------------"<<endl;

UStudent xi(223,"习近平",22,"管理");
xi.print1();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐