close

When one class inherits from another, the derived class inherits the variables and functions of the base class. 

These variables and functions become part of the derived class.

for example :

 

class Person // base class 
{
public:
    std::string m_strName;
    int m_nAge;
    bool m_bIsMale;
 
    std::string GetName() { return m_strName; }
    int GetAge() { return m_nAge; }
    bool IsMale() { return m_bIsMale; }
 
    Person(std::string strName = "", int nAge = 0, bool bIsMale = false)
        : m_strName(strName), m_nAge(nAge), m_bIsMale(bIsMale)
    {
    }
};

class BaseballPlayer : public Person // BaseballPlayer publicly inheriting Person
{
public:
    double m_dBattingAverage;
    int m_nHomeRuns;
 
    BaseballPlayer(double dBattingAverage = 0.0, int nHomeRuns = 0)
       : m_dBattingAverage(dBattingAverage), m_nHomeRuns(nHomeRuns)
    {
    }
};

int main()
{
   
    BaseballPlayer cJoe;
    cJoe.m_strName = "Joe";
    std::cout << cJoe.GetName() << std::endl;
 
    return 0;
}


In the base class , only area of public  and area of protected can be inheritance , 

the area of private can not be inheritance.

The public area in the base class is also the public area in the derived class , 

but the protected area will become private area in the derived class.


ref : http://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JerryCheng 的頭像
    JerryCheng

    KwCheng's blog

    JerryCheng 發表在 痞客邦 留言(0) 人氣()