c++类 exercise

demo1

在子类中使用 基类::函数调用基类函数 实现函数重写

#include <iostream>
#include <string>
using namespace std;

// 基类 Person
class Person {
private:
    string name;
    int age;

public:
    // 构造函数
    Person(const string& name, int age) : name(name), age(age) {}

    // 打印姓名和年龄
    virtual void displayInfo() const {
        cout << "Name: " << name << ", Age: " << age;
    }
};

// 派生类 Student
class Student : public Person {
private:
    string studentID;

public:
    // 构造函数
    Student(const string& name, int age, const string& studentID)
        : Person(name, age), studentID(studentID) {}

    // 重写 displayInfo()
    void displayInfo() const override {
        Person::displayInfo(); // 调用基类的 displayInfo()
        cout << ", Student ID: " << studentID << endl;
    }
};

void demo1() {
    // 创建 Student 对象
    Student stu("Tom", 20, "S123456");

    // 显示信息
    stu.displayInfo();

    return 0;
}

demo2

有虚函数,就基类加虚析构函数,否则通过基类指针释放派生类对象时,可能会造成内存泄漏。

class Shape{
public:
    virtual void draw(){
        std::cout << "Drawing a generic shape" << std::endl;
    }
    virtual ~Shape(){}
};

class Circle:public Shape{
public:
    void draw() override{
        std::cout << "Drawing a circle." << std::endl;
    }
};


class Rectangle:public Shape{
public:
    void draw() override{
        std::cout << "Drawing a rectangle." << std::endl;
    }
};

void demo2(){
    Shape* shapes[2];
    shapes[0] = new Circle();
    shapes[1] = new Rectangle();

    // 遍历调用 draw()
    for (int i = 0; i < 2; ++i) {
        shapes[i]->draw();
    }

    // 释放内存
    for (int i = 0; i < 2; ++i) {
        delete shapes[i];
    }
}

demo3

在 C++ 中,如果派生类定义了一个与基类同名的函数(哪怕参数不同),基类的所有同名函数都会被隐藏

使用 using Calculator::add; 把基类的 add() 带进派生类作用域,就不会隐藏了。

class Calculator{
public:
    int add(int a, int b){
        return a + b;
    }
    double add(double a, double b){
        return a + b;
    }
};

class AdvancedCalculator: public Calculator{
public:
    using Calculator::add;
    int add(int a, int b, int c){
        return a + b + c;
    }
};

void demo3(){
    AdvancedCalculator test;
    std::cout << test.add(2, 3) << std::endl;
    std::cout << test.add(2.2, 3.3) << std::endl;
    std::cout << test.add(2, 3, 4) << std::endl;
}

demo4

纯虚函数 是在基类中声明但不提供实现的虚函数。包含至少一个纯虚函数的类称为 抽象基类(Abstract Base Class,ABC)。抽象基类不能被实例化,要求派生类必须实现所有纯虚函数才能被实例化。

Animal抽象基类,所以 makeSound() 必须是纯虚函数(pure virtual function),而你写的是普通虚函数。

class Animal{
public:
    virtual void makeSound() const = 0;
    virtual ~Animal()= default;
};

class Dog: public Animal{
public:
    void makeSound() const override{
        std::cout << "Woof!" << std::endl;
    }
};

class Cat: public Animal{
public:
    void makeSound() const override{
        std::cout << "Meow!" << std::endl;
    }
};

void demo4(){
    Animal* ptr[2];
    ptr[0] = new Dog;
    ptr[1] = new Cat;

    ptr[0]->makeSound(); // Woof!
    ptr[1]->makeSound(); // Meow!

    delete ptr[0];
    delete ptr[1];
}

demo5

构造 & 析构顺序说明

  • 构造:先构造基类 → 再构造派生类
  • 析构:先析构派生类 → 再析构基类
    这是 C++ 对象生命周期的固定规则。
class Vehicle{
public:
    Vehicle(){
        std::cout << "Vehicle constructed." << std::endl;
    }
    virtual ~Vehicle(){
        std::cout << "Vehicle destructed" << std::endl;
    }
};

class Car:public Vehicle{
public:
    Car(){
        std::cout << "Car constructed" << std::endl;
    }
    ~Car(){
        std::cout << "Car destructed" << std::endl;
    }
};

void demo5(){
    Car car;
}
posted @ 2025-08-02 09:41  xiaoluosibky  阅读(5)  评论(0)    收藏  举报