10.2 继承与多态

文章目录

继承

继承的作用是代码复用。派生类自动获得基类的除私有成员外的一切。基类描述一般特性,派生类提供更丰富的属性和行为。在构造派生类时,其基类构造函数先被调用,然后是派生类构造函数。在析构时顺序刚好相反。

cpp 复制代码
// 基类 LinearList
class LinearList{
    int *buffer;
    int size;
public:
    LinearList(int num){
        size = (num>10)?num:10;
        buffer = new int[size];
    }
    ~LinearList(){
        delete []buffer;
    }
    bool insert(int x, int i);  // 线性表第i个元素后插入新元素,返回成功或失败
    bool remove(int &x, int i); // 删除线性表第i个元素,返回成功或失败
    int element(int i) const; // 返回线性表第i个元素, const表示不会修改调用它的对象的任何非静态成员数据
    int search(int x) const; // 查找值为x的元素并返回其序号
    int length() const; //返回线性表的长度
};
// 派生类 Queue
class Queue:private LinearList{  // 基类的公有和保护成员均变成派生类的私有成员
public:
    bool enQueue(int x){  // 元素x入队,返回操作成功或失败
        return insert(x, length());
    }
   bool deQueue(int &x){  // 元素出队, x带回队头元素
        return remove(x, 1);
    }
};
// 派生类 Stack
class Stack:private LinearList{
public:
    bool push(int x){  // 元素x入栈,返回操作成功或失败
        return insert(x, 1); 
    }
    bool pop(int &x){  // 元素出栈,x带回栈顶元素
        return remove(x, 1);
    }
};

多态

多态的作用是使用一个接口调用多种方法,具体调用哪个函数,是在程序运行时决定的。实现多态,需要在派生类中定义与基类成员函数完全相同的方法签名(返回值、函数名、形参都完全一样)。作用在普通成员函数上,称为重置或覆盖。作用在虚成员函数上,称为实现。虚函数的前面都有virtual关键字,纯虚函数名末尾还有"=0"的标记,纯虚函数仅有定义,没有函数实现,当作接口使用。含有纯虚函数的类称为抽象类,不能创建对象,只能被继承。只有类的成员函数才能是虚函数,静态成员函数不能是虚函数,构造函数不能是虚函数,析构函数可以是虚函数。

cpp 复制代码
#include <iostream>
#include <math.h>
// 基类
class Figure{
public:
    virtual double getArea() = 0; // 纯虚函数
}; // 千万不要忘记这个分号
// 派生类 Rectangle
class Rectangle:public Figure{
protected:
    double height;
    double width;
public:
    Rectangle(){}
    Rectangle(double height, double width){
        this->height = height;
        this->width = width;
    }
    double getArea(){
        return height*width;
    }
};
// 派生类 Triangle
class Triangle:public Figure{
    double la;
    double lb;
    double lc;
public:
    Triangle(double la, double lb, double lc){
        this->la = la;
        this->lb = lb;
        this->lc = lc;
    }
    double getArea(){
        double s = (la+lb+lc)/2.0;
        return sqrt(s*(s-la)*(s-lb)*(s-lc));
    }
};
// 主函数
void main(){
    Figure *figures[2] = {new Triangle(2,3,3), new Rectangle(5,8)};
    for(int i=0; i<2; i++){
        std::cout << "figures[" << i << "] area=" << figures[i]->getArea() << std::endl;
        delete figures[i];
    }
}
相关推荐
W23035765732 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
.Ashy.2 小时前
2026.4.11 蓝桥杯软件类C/C++ G组山东省赛 小记
c语言·c++·蓝桥杯
minji...3 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
CoderCodingNo5 小时前
【GESP】C++三级真题 luogu-B4499, [GESP202603 三级] 二进制回文串
数据结构·c++·算法
hetao17338376 小时前
2026-04-09~12 hetao1733837 的刷题记录
c++·算法
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 136. 只出现一次的数字 | C++ 哈希表&异或基础解法
c++·算法·leetcode
汉克老师7 小时前
GESP2024年6月认证C++三级( 第二部分判断题(1-10))
c++·数组·位运算·补码·gesp三级·gesp3级
无限进步_8 小时前
【C++】只出现一次的数字 II:位运算的三种解法深度解析
数据结构·c++·ide·windows·git·算法·leetcode
小贾要学习8 小时前
【Linux】TCP网络通信编程
linux·服务器·网络·c++·网络协议·tcp/ip
哎嗨人生公众号9 小时前
手写求导公式,让轨迹优化性能飞升,150ms变成9ms
开发语言·c++·算法·机器人·自动驾驶