/*类的继承(1)*/

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

class A
{
public:
    A()
    {
        m_nData = 10;
        m_nPublic_A = 1;
        m_nProtected_A = 2;
        m_nPrivate_A = 3;
    }
    //
成员函数是在类内
    void FunA()
    {
        int nTemp = m_nProtected_A;
    }
    void Show(int i)
    {
        cout << "
基类的SHOW" << endl;
    }
public:
    int m_nData;
    int m_nPublic_A;
protected:
    int m_nProtected_A;
private:
    int m_nPrivate_A;
};

//
继承方式 
//
影响的是 A的成员 在派生类C中或则是全局函数中的 访问方式
//
派生类的成员函数可以访问基类的公有成员和保护类成员
//
派生类拥有基类的方法和数据成员

class B : public A
{
public:
    B()
    {
        m_nData = 20;
    }
    void FunB()
    {
        m_nPublic_A = 1;
        m_nProtected_A = 2;
        //m_nPrivate_A = 3;
       
        m_nPublic_B = 4;
        m_nProtected_B = 5;
        m_nPrivate_B = 6;
    }
   
    //
如果派生类的函数名和基类的函数名同名,参数无所谓
    //
两个函数构成隐藏关系

    void Show()
    {
        A::Show(1);
        cout << "
派生类的SHOW" << endl;
    }
    //
在一个作用域中,两个函数函数名一样,参数类型,个数,顺序不一样,
    //
两个函数构成重载关系

    void Show(char*)
    {
        cout << "
派生类的SHOW(char*)" << endl;
    }
public:
    int m_nData;
    int m_nPublic_B;
protected:
    int m_nProtected_B;
private:
    int m_nPrivate_B;
};


class C : private B
{
public:
    void FunC()
    {
        //m_nPublic_A = 1;
        //m_nProtected_A = 2;
        //m_nPrivate_A = 3;
    }
};

int main(int argc, char* argv[])
{
    //
在派生类中可隐藏基类的任何函数,成员
    B theB;
    theB.Show();
    theB.Show("Hello");
    theB.A::Show(1);
    int nData = theB.A::m_nData;
    int nData1 = theB.m_nPublic_A;
    return 0;
}

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