包含子对象的派生类的构造函数

 1 #include <iostream>
 2 #include <string.h>
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 class Student
 6 {
 7     public:
 8         Student(int n,string nam)
 9         {
10             num=n;
11             name=nam;
12         }
13         void display()
14         {
15             cout<<"num:"<<num<<endl<<"name:"<<name<<endl;
16         }
17         protected:
18             int num;
19             string name;
20 };
21 
22 class Student1:public Student
23 {
24     public:
25         Student1(int n,string nam,int n1,string nam1,int a ,string ad):
26             Student(n,nam),monitor(n1,nam1){
27                 age=a;
28                 addr=ad;
29             }
30             void show()
31             {
32                 cout<<"This student is:"<<endl;
33                 display();
34                 cout<<"age:"<<age<<endl;
35                 cout<<"address:"<<addr<<endl<<endl;
36             }
37             void show_monitor()
38             {
39                 cout<<endl<<"Class monitor is:"<<endl;
40                 monitor.display();
41             }
42             private:
43                 Student monitor;
44                 int age;
45                 string addr;
46 };
47 int main(int argc, char** argv) {
48     Student1 stud1(10010,"wang",10001,"li",19,"bj");
49     stud1.show();
50     stud1.show_monitor();
51     return 0;
52 }

 

转载于:https://www.cnblogs.com/borter/p/9405464.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值