#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(int a, string b):num(a),name(b){};
void display();
protected:
int num;
string name;
};
void Student :: display()
{
cout<<"num:"<<num<<endl<<"name:"<<name<<endl;
}
<span style="BACKGROUND-COLOR: #ff9900">class Student1:public Student
</span>{
private:
Student monitor;
int age;
string addr;
public:
<span style="BACKGROUND-COLOR: #ff9966">Student1(int a , string b, int num1, string name1, int ag, string ad):Student(a,b),monitor(num1,name1)
</span> {
age = ag;
addr = ad;
}
void show();
void show_monitor();
};
void Student1::show()
{
cout<<"This student is:"<<endl;
display(); //public函数调用另一个public函数
cout<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl;
}
void Student1::show_monitor()
{
cout<<"The monitor is:"<<endl;
monitor.display(); //public函数调用子对象的public函数
}
int main()
{
<span style="BACKGROUND-COLOR: #ff9900">Student1 *stud1 ;
</span> <span style="color:#000000;BACKGROUND-COLOR: #ff9900">stud1= new Student1(101,"wang",105,"sun",19,"Beijing");
</span> Student1 *stud2;
stud2 = new Student1(110,"li",200,"qiang",19,"Nanjing");
stud1->show();
stud1->show_monitor();
stud2->show();
stud2->show_monitor();
getchar();
delete stud2;
delete stud1;
return 0;
}