/*类的继承(2)*/

#include "stdafx.h"
#include <string.h>
#include <iostream.h>

class MyDate
{
public:
    MyDate()
    {
        cout << "
成员构造" << endl;
    }
    ~MyDate()
    {
        cout << "
成员构造" << endl;
    }
};

class Employee
{
protected:
    char *m_pName;
private:
    short m_nAge;
    float m_fSalary;
public:
    Employee() 
    {
        m_pName= NULL; 
        m_nAge = 0;
        m_fSalary = 0.0;
        cout << "
基类构造" << endl;
    }
    Employee(char *pName, short nAge, float fSalary)
    {
        m_pName = new char[strlen(pName)+1];
        strcpy(m_pName,pName);
        m_nAge = nAge;
        m_fSalary = fSalary;
        cout << "
基类构造(3参数)" << endl;
    }
    ~Employee()
    {
        if ( m_pName )
        {
            delete[] m_pName;
            m_pName = NULL;
        }
        cout << "
基类析构" << endl;
    }
    void Print()
    {
        if ( m_pName )
            cout << m_pName <<endl;
       
        cout << m_nAge << m_fSalary << endl;
    }
};

class Manager:public Employee
{
private:
    int m_nLevel;
    MyDate m_nBirthDay;
public:
    Manager(int nLevel)
        :Employee( "
张三",20,2000 )
    {
        m_nLevel = nLevel;
        cout << "
派生类构造" << endl;
    }
    ~Manager()
    {
        cout << "
派生类析构" << endl;
    }
};

int main(int argc, char* argv[])
{
   //
构造顺序
    //
基类====>成员======>派生类
    //
析构顺序
    //
派生类====>成员======>基类
    //
如果基类有构造函数,派生类会产生一个默认的构造

    Employee e("abc",10,3568);   
    e.Print();
   
    return 0;
}

posted on 2010-01-26 21:56  o无尘o  阅读(135)  评论(0编辑  收藏  举报