继承和多态

#include <iostream>
#include <cmath>

using namespace std;

class Shape {
protected:
    double perimeter; // 周长
    double area;      // 面积

public:
    // 默认构造函数
    Shape() : perimeter(0), area(0) {}

    // 析构函数
    virtual ~Shape() {}

    // 拷贝构造函数
    Shape(const Shape &other) : perimeter(other.perimeter), area(other.area) {}

    // 拷贝赋值运算符
    Shape &operator=(const Shape &other) {
        if (this != &other) {
            perimeter = other.perimeter;
            area = other.area;
        }
        return *this;
    }

    // 获取周长(虚函数)
    virtual double getPerimeter() const {
        return perimeter;
    }

    // 获取面积(虚函数)
    virtual double getArea() const {
        return area;
    }
};

class Rectangle : public Shape {
private:
    double width;  // 宽
    double height; // 高

public:
    // 构造函数
    Rectangle(double w, double h) : width(w), height(h) {
        perimeter = 2 * (width + height);
        area = width * height;
    }

    // 析构函数
    ~Rectangle() {}

    // 拷贝构造函数
    Rectangle(const Rectangle &other) : Shape(other), width(other.width), height(other.height) {}

    // 拷贝赋值运算符
    Rectangle &operator=(const Rectangle &other) {
        if (this != &other) {
            Shape::operator=(other);
            width = other.width;
            height = other.height;
        }
        return *this;
    }

    // 获取周长
    double getPerimeter() const override {
        return perimeter;
    }

    // 获取面积
    double getArea() const override {
        return area;
    }
};

class Circle : public Shape {
private:
    double radius; // 半径

public:
    // 构造函数
    Circle(double r) : radius(r) {
        perimeter = 2 * M_PI * radius; // 周长
        area = M_PI * radius * radius;  // 面积
    }

    // 析构函数
    ~Circle() {}

    // 拷贝构造函数
    Circle(const Circle &other) : Shape(other), radius(other.radius) {}

    // 拷贝赋值运算符
    Circle &operator=(const Circle &other) {
        if (this != &other) {
            Shape::operator=(other);
            radius = other.radius;
        }
        return *this;
    }

    // 获取周长
    double getPerimeter() const override {
        return perimeter;
    }

    // 获取面积
    double getArea() const override {
        return area;
    }
};

// 全局函数,输出图形的周长和面积
void printShapeInfo(const Shape *shape) {
    cout << "Perimeter: " << shape->getPerimeter() << endl;
    cout << "Area: " << shape->getArea() << endl;
}

int main() {
    // 创建矩形对象
    Rectangle rect(5.0, 3.0);
    cout << "Rectangle:" << endl;
    printShapeInfo(&rect); // 传递矩形对象

    // 创建圆对象
    Circle circ(4.0);
    cout << "\nCircle:" << endl;
    printShapeInfo(&circ); // 传递圆对象

    return 0;
}
#include <iostream>
#include <stdexcept>

template <typename T>
class CircularQueue {
private:
    T* data;          // 存储队列元素的数组
    int front;        // 队头索引
    int rear;         // 队尾索引
    int capacity;     // 队列的容量
    int count;        // 当前元素数量

public:
    // 构造函数
    CircularQueue(int size) : capacity(size), front(0), rear(-1), count(0) {
        data = new T[capacity];
    }

    // 析构函数
    ~CircularQueue() {
        delete[] data;
    }

    // 拷贝赋值运算符
    CircularQueue& operator=(const CircularQueue& other) {
        if (this != &other) {
            delete[] data; // 释放当前数组
            capacity = other.capacity;
            front = other.front;
            rear = other.rear;
            count = other.count;
            data = new T[capacity];
            for (int i = 0; i < count; i++) {
                data[(front + i) % capacity] = other.data[(front + i) % capacity];
            }
        }
        return *this;
    }

    // 访问第一个元素
    T frontElement() const {
        if (empty()) {
            throw std::out_of_range("队列为空,无法访问队头元素");
        }
        return data[front];
    }

    // 访问最后一个元素
    T backElement() const {
        if (empty()) {
            throw std::out_of_range("队列为空,无法访问队尾元素");
        }
        return data[rear];
    }

    // 检查队列是否为空
    bool empty() const {
        return count == 0;
    }

    // 返回当前元素数量
    int size() const {
        return count;
    }

    // 向队列尾部插入元素
    void push(const T& value) {
        if (count == capacity) {
            throw std::overflow_error("队列已满,无法入队");
        }
        rear = (rear + 1) % capacity; // 循环更新队尾索引
        data[rear] = value;
        count++;
    }

    // 原位构造元素
    template <typename... Args>
    void emplace(Args&&... args) {
        if (count == capacity) {
            throw std::overflow_error("队列已满,无法入队");
        }
        rear = (rear + 1) % capacity; // 循环更新队尾索引
        new(&data[rear]) T(std::forward<Args>(args)...); // 原位构造
        count++;
    }

    // 删除首个元素
    void pop() {
        if (empty()) {
            throw std::underflow_error("队列为空,无法出队");
        }
        front = (front + 1) % capacity; // 循环更新队头索引
        count--;
    }
};

int main() {
    CircularQueue<int> queue(5); // 创建一个容量为5的循环队列

    // 测试入队
    queue.push(1);
    queue.push(2);
    queue.push(3);
    queue.push(4);
    queue.push(5);

    // 测试队列状态
    std::cout << "队头元素: " << queue.frontElement() << std::endl;
    std::cout << "队尾元素: " << queue.backElement() << std::endl;
    std::cout << "当前队列大小: " << queue.size() << std::endl;

    // 测试出队
    queue.pop();
    std::cout << "出队后队头元素: " << queue.frontElement() << std::endl;

    // 测试原位构造
    queue.emplace(6);
    std::cout << "新入队元素: " << queue.backElement() << std::endl;

    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值