/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者: 张浩
* 完成日期: 2013年06月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<<"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<<"Class monitor is:"<<endl;
monitor.display();
}
private:
Student monitor;
int age;
string addr;
};
int main()
{
Student1 stu1(10010,"wang-li",10001,"li-sun",19,"115 Beijing Road,shanghai");
stu1.show();
stu1.show_monitor();
return 0;
}
运行结果:
学习交流:从上面的程序我可以更清楚的认识和学习有子对象的派生类的构造函数,它的确使程序更简洁,高效。。。。