public继承的含义:
class Person {};
class Student : public Person {};
void eta(const Person& p) {}
void study(const Student& s) {}
int main() {
Person p;
Student s;
eta(p);
eta(s);
study(s);
study(p);
return 0;
}
/*
* 报错信息
main.cpp:11:11: error: invalid initialization of reference of type ‘const Student&’ from expression of type ‘Person’
11 | study(p);
| ^
In file included from main.cpp:3:
item_32.h:14:27: note: in passing argument 1 of ‘void study(const Student&)’
14 | void study(const Student& s) {}
*/
通过public继承建模,应该慎重思考父类和子类的关系,考虑下面两个例子
// 建模方式1
class Bird {
public:
virtual void fly() {}
};
class Penguin : public Bird {};
// 建模方式2
class Bird {};
class FlyingBird : public Bird {
public:
virtual void fly() {}
};
class Penguin : public Bird {};
// 建模方式3
class Bird {
public:
virtual void fly() {}
};
class Penguin : public Bird {
public:
virtual void fly() { error(); }
};
// 建模方式4
class Bird {};
class Penguin : public Bird {};
class Rectangle {
public:
virtual void setHeight(int newHeight);
virtual void setWidth(int newWidth);
virtual int height() const;
virtual int width() const;
};
class Square : public Rectangle {};
void makeBigger(Rectangle& r) {
int oldHeight = r.height();
r.setHeight(r.width() + 10);
assert(r.height() == oldHeight);
}
Square s;
makeBigger(s); // 结果不符合预期