继承初体验

/* 

* Copyright (c) 2013, 烟台大学计算机学院                     

* All rights reserved.                     

* 文件名称:test.cpp                     

* 作者:邱学伟                    

* 完成日期:2013 年 5 月 9 日                     

* 版本号:v1.0                                        

* 输入描述:无                     

* 问题描述: 1. 掌握类的派生与继承的概念;2. 学会定义子类,重点是类的构造函数与析构函数                 

* 程序输出:定义一个继承类

* 问题分析:                    

* 算法设计:略                     

*/         

#include <iostream>

#include <Cstring>

using namespace std;

class Student

{

    public:

    Student(int n,string nam)

    {

        num=n;

        name=nam;

    }

    void display()

    {

        cout<<"num:"<<num<<endl<<"nume:"<<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;

       add=ad;

    }

    void show()

    {

        cout<<"This student is:"<<endl;

        display();

        cout<<"age:"<<age<<endl;

        cout<<"addr:"<<add<<endl;

    }

    void monitor_show()

    {

        cout<<endl<<"Class monitor is:"<<endl;

        monitor.display( );



    }

private:

Student monitor;

int age;

string add;

};

int main()

{

    Student1 stu1(10001,"li si",10002,"zhang san",21,"1 beijinglu");

    stu1.show( );                       //输出学生的数据

    stu1.monitor_show();                //输出子对象的数据

    return 0;

}


继承初体验

你可能感兴趣的:(继承)