C++
派生类
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
Animal()
{}
void set_weight(int w)
{
m_nWeightBase=w;
}
int get_weight()
{
return m_nWeightBase;
}
void set_age(int y)
{
m_nAgeBase=y;
}
private:
int m_nWeightBase;
protected:
int m_nAgeBase;
};
class Cat:private Animal
{
private:
string m_strName;
public:
Cat(string n)
{
m_strName=n;
}
void set_print_age()
{
set_age(5);
cout<<m_strName<<", age = "<<Animal::m_nAgeBase<<endl;
}
void set_print_weight()
{
set_weight(6);
cout<<m_strName<<", weight = "<<Animal::get_weight()<<endl;
}
int main()
{
Cat cat("Persian"); //定义派生类对象cat
cat.set_print_age();
cat.set_print_weight(); //派生类对象调用自己的公有函数
return 0;
}