第11课: 继承与多态
继承基础
#include <iostream>
#include <string>
using namespace std;
// 基类(父类)
class Animal {
protected:
string name;
int age;
public:
Animal(string n, int a) : name(n), age(a) {}
void eat() {
cout << name << "正在吃东西" << endl;
}
void sleep() {
cout << name << "正在睡觉" << endl;
}
};
// 派生类(子类)
class Dog : public Animal {
private:
string breed;
public:
Dog(string n, int a, string b) : Animal(n, a), breed(b) {}
void bark() {
cout << name << "在叫:汪汪汪!" << endl;
}
};
class Cat : public Animal {
public:
Cat(string n, int a) : Animal(n, a) {}
void meow() {
cout << name << "在叫:喵喵喵!" << endl;
}
};
int main() {
Dog dog("旺财", 3, "金毛");
dog.eat();
dog.bark();
Cat cat("咪咪", 2);
cat.sleep();
cat.meow();
return 0;
}
继承方式
// public继承:基类的public和protected成员在派生类中保持不变
class Derived1 : public Base { };
// protected继承:基类的public和protected成员在派生类中变为protected
class Derived2 : protected Base { };
// private继承:基类的public和protected成员在派生类中变为private
class Derived3 : private Base { };
虚函数与多态
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
string color;
public:
Shape(string c) : color(c) {}
// 虚函数
virtual void draw() {
cout << "绘制形状" << endl;
}
virtual double getArea() {
return 0.0;
}
// 虚析构函数
virtual ~Shape() {
cout << "销毁Shape" << endl;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(string c, double r) : Shape(c), radius(r) {}
// 重写虚函数
void draw() override {
cout << "绘制" << color << "色圆形" << endl;
}
double getArea() override {
return 3.14159 * radius * radius;
}
~Circle() {
cout << "销毁Circle" << endl;
}
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(string c, double w, double h) : Shape(c), width(w), height(h) {}
void draw() override {
cout << "绘制" << color << "色矩形" << endl;
}
double getArea() override {
return width * height;
}
~Rectangle() {
cout << "销毁Rectangle" << endl;
}
};
int main() {
// 多态:基类指针指向派生类对象
Shape *shapes[2];
shapes[0] = new Circle("红", 5.0);
shapes[1] = new Rectangle("蓝", 4.0, 6.0);
for (int i = 0; i < 2; i++) {
shapes[i]->draw();
cout << "面积:" << shapes[i]->getArea() << endl;
}
// 释放内存
for (int i = 0; i < 2; i++) {
delete shapes[i];
}
return 0;
}
纯虚函数与抽象类
#include <iostream>
#include <string>
using namespace std;
// 抽象类:包含纯虚函数的类
class Vehicle {
protected:
string brand;
public:
Vehicle(string b) : brand(b) {}
// 纯虚函数
virtual void start() = 0;
virtual void stop() = 0;
void showBrand() {
cout << "品牌:" << brand << endl;
}
virtual ~Vehicle() {}
};
class Car : public Vehicle {
public:
Car(string b) : Vehicle(b) {}
void start() override {
cout << brand << "汽车启动:点火,挂档" << endl;
}
void stop() override {
cout << brand << "汽车停止:刹车,熄火" << endl;
}
};
class Bicycle : public Vehicle {
public:
Bicycle(string b) : Vehicle(b) {}
void start() override {
cout << brand << "自行车启动:踩踏板" << endl;
}
void stop() override {
cout << brand << "自行车停止:捏刹车" << endl;
}
};
int main() {
// Vehicle v("通用"); // 错误!不能实例化抽象类
Vehicle *v1 = new Car("奔驰");
Vehicle *v2 = new Bicycle("捷安特");
v1->showBrand();
v1->start();
v1->stop();
v2->showBrand();
v2->start();
v2->stop();
delete v1;
delete v2;
return 0;
}
多重继承
#include <iostream>
#include <string>
using namespace std;
class Flyable {
public:
virtual void fly() {
cout << "可以飞行" << endl;
}
};
class Swimmable {
public:
virtual void swim() {
cout << "可以游泳" << endl;
}
};
// 多重继承
class Duck : public Flyable, public Swimmable {
private:
string name;
public:
Duck(string n) : name(n) {}
void fly() override {
cout << name << "在飞" << endl;
}
void swim() override {
cout << name << "在游泳" << endl;
}
};
int main() {
Duck duck("唐老鸭");
duck.fly();
duck.swim();
return 0;
}
override和final关键字
#include <iostream>
using namespace std;
class Base {
public:
virtual void func1() {
cout << "Base::func1" << endl;
}
virtual void func2() final { // final:不能被重写
cout << "Base::func2" << endl;
}
};
class Derived : public Base {
public:
void func1() override { // override:明确表示重写
cout << "Derived::func1" << endl;
}
// void func2() override { } // 错误!func2是final的
};
// final类:不能被继承
class FinalClass final {
public:
void show() {
cout << "这是final类" << endl;
}
};
// class CannotDerive : public FinalClass { }; // 错误!
int main() {
Derived d;
d.func1();
d.func2();
return 0;
}
练习题
- 设计一个员工管理系统,包含Employee基类和Manager、Engineer派生类
- 创建图形类层次结构,实现多态的面积计算
- 实现一个动物园系统,使用抽象类和多态
- 设计一个支付系统,支持多种支付方式(支付宝、微信、银行卡)