C++类各种性质

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

// 基类:动物(展示封装、继承、虚函数)
class Animal {
protected:
    string name;
    int age;

public:
    // 构造函数重载(Overloading)
    Animal() : name("Unknown"), age(0) {}
    Animal(string n, int a) : name(n), age(a) {}

    // 虚函数(多态基础)
    virtual void makeSound() const {
        cout << name << " makes a generic animal sound." << endl;
    }

    // 纯虚函数(抽象类特性)
    virtual void move() = 0;

    // 静态成员(类级别共享)
    static int animalCount;

    // 友元函数(访问私有成员)
    friend void displayAnimalInfo(const Animal& a);

    // 运算符重载(<<)
    friend ostream& operator<<(ostream& os, const Animal& a);

    // 虚析构函数(确保正确释放派生类资源)
    virtual ~Animal() {
        cout << "Animal destructor: " << name << endl;
        animalCount--;
    }
};

// 静态成员初始化
int Animal::animalCount = 0;

// 友元函数实现
void displayAnimalInfo(const Animal& a) {
    cout << "Friend function access: " << a.name << ", Age: " << a.age << endl;
}

// 运算符重载实现
ostream& operator<<(ostream& os, const Animal& a) {
    os << "Animal: " << a.name << " (" << a.age << " years old)";
    return os;
}

// 派生类:狗(展示继承、多态、override)
class Dog : public Animal {
private:
    string breed;

public:
    // 构造函数
    Dog(string n, int a, string b) : Animal(n, a), breed(b) {
        animalCount++;
    }

    // 重写基类虚函数(override关键字)
    void makeSound() const override {
        cout << name << " says: Woof! Woof!" << endl;
    }

    // 实现纯虚函数
    void move() override {
        cout << name << " is running on four legs" << endl;
    }

    // 方法重载(Overloading)
    void setAge(int newAge) { age = newAge; }
    void setAge(double newAge) { age = static_cast<int>(newAge); }

    // 新增方法
    void fetch() {
        cout << name << " is fetching a stick" << endl;
    }

    // 运算符重载(+)
    Dog operator+(const Dog& other) {
        return Dog(name + "-" + other.name, (age + other.age)/2, breed);
    }

    ~Dog() {
        cout << "Dog destructor: " << name << endl;
    }
};

// 派生类:鸟(展示多继承)
class Bird : public Animal {
public:
    Bird(string n, int a) : Animal(n, a) {
        animalCount++;
    }

    void makeSound() const override {
        cout << name << " says: Tweet! Tweet!" << endl;
    }

    void move() override {
        cout << name << " is flying" << endl;
    }

    // 新增方法
    void buildNest() {
        cout << name << " is building a nest" << endl;
    }
};

int main() {
    // 多态演示
    Animal* animals[3];
    animals[0] = new Dog("Buddy", 3, "Golden Retriever");
    animals[1] = new Bird("Tweety", 2);
    animals[2] = new Dog("Max", 5, "Husky");

    cout << "=== Polymorphism Demo ===" << endl;
    for (int i = 0; i < 3; ++i) {
        animals[i]->makeSound();
        animals[i]->move();
        cout << endl;
    }

    // 重载演示
    Dog myDog("Rex", 2, "Labrador");
    myDog.setAge(3);       // 调用int版本
    myDog.setAge(3.5);     // 调用double版本

    // 运算符重载演示
    Dog dog1("A", 2, "Poodle");
    Dog dog2("B", 3, "Poodle");
    Dog dog3 = dog1 + dog2;
    cout << "\n=== Operator Overloading Demo ===" << endl;
    cout << "New dog: " << dog3 << endl;

    // 友元函数和静态成员演示
    cout << "\n=== Friend Function & Static Member ===" << endl;
    displayAnimalInfo(*animals[0]);
    cout << "Total animals created: " << Animal::animalCount << endl;

    // 清理内存
    for (auto& a : animals) {
        delete a;
    }

    return 0;
}

 

posted on 2025-04-26 09:45  fag888  阅读(24)  评论(0)    收藏  举报

导航