/*
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 文件名称:11-阅读程序1
* 作 者:孙红蕾
* 完成日期:2013 年 5 月 10 日
* 版 本 号:v1.0
*
* 输入描述:无
* 问题描述:阅读下面的程序,写出程序运行结果,上机时再运行,对比结果是否有
* 差异,仔细领会继承,以及基类、派生类中构造函数的写法及执行过程
* 程序输出:按要求输出
* 算法设计:略
*/
#include <iostream>
#include <string>
using namespace std;
//声明基类
class Student
{
public:
Student(int n,string nam) //基类构造函数
{
num=n;
name=nam;
}
void display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
}
protected:
int num;
string name;
};
//声明公用派生类
class Student1:public Student
{
public:
Student1(int n,string nam,int n1,string nam1,int a,string ad):Student(n,nam),monitor(n1,nam1) //派生类构造函数
{
age=a;
addr=ad;
}
void show()
{
cout<<"This student is:"<<endl;
display();
cout<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl<<endl;
}
void show_monitor()
{
cout<<endl;
cout<<"Class monitor is:"<<endl;
monitor.display();
}
private:
Student monitor;
int age;
string addr;
} ;
int main()
{
Student1 stud1(10010,"Wang-li",10001,"Li-sun",19,"115 Beijing Road ,Shanghai");
stud1.show();
stud1.show_monitor();
return 0;
}
运行结果: