- 11
- 20
- 练习:简单的继承
- 未分类
- 编辑 | 删除
- #include <iostream>
- using namespace std;
- class ClsA {
- public:
- void show() {
- cout << "this is A" << endl;
- }
- void doSthA() {
- cout << "do sth A" << endl;
- }
- };
- class ClsB : public ClsA {
- public:
- void show() {
- cout << "this is B" << endl;
- }
- void doSthB() {
- cout << "do sth B" << endl;
- }
- };
- int main() {
- ClsA a;
- ClsB b;
- a.show();
- a.doSthA();
- // a.doSthB(); 这行必须注释掉,因为a对象中没有定义doSthB这个方法
- b.show();
- b.doSthA();
- b.doSthB();
- }