继承的优点—>减少重复代码
class A :public B;
A类称为子类或派生类
B类称为父类或者基类
派生类中的成员,包含两大部分
一类是从基类继承过来的,一类是自己增加的成员。
从基类继承过来的表现为共性,而新增的成员表现了个性
代码示例
#include <iostream>
using namespace std;
class Persona
{
public:
void head()
{
cout << "head" <<endl;
}
void hand()
{
cout << "hand" <<endl;
}
};
class Man:public Persona
{
public:
void strong()
{
cout << "man is strong" <<endl;
}
void tall()
{
cout << "man is taller than women" <<endl;
}
};
class OldMan:public Man
{
public:
void old()
{
cout <<"old man is old" <<endl;
}
};
void test()
{
OldMan A;
A.head();
A.hand();
A.strong();
A.tall();
A.old();
}
int main()
{
test();
return 0;
}
输出结果