第十四章:C++虚函数、继承和多态详解

第十四章:C++虚函数、继承和多态详解

1. 概述

C++的虚函数、继承和多态是面向对象编程中非常重要和核心的概念。虚函数允许在派生类中重新定义基类的函数,通过继承和多态可以实现统一的接口和不同对象的不同行为。本文将详细讲解C++中虚函数、继承和多态,并结合代码和实际案例进行演示。

2. 虚函数

2.1 虚函数的定义和使用

虚函数是在基类中声明的函数,用virtual关键字修饰。它允许派生类中重新定义该函数,以实现多态的效果。以下是一个示例:

class Shape {
    
    
public:
    virtual void draw() const {
    
    
        cout << "Drawing a shape." << endl;
    }
};

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

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

int main() {
    
    
    Shape* shape1 = new Circle();
    Shape* shape2 = new Rectangle();

    shape1->draw();
    shape2->draw();

    delete shape1;
    delete shape2;

    return 0;
}

运行结果:

Drawing a circle.
Drawing a rectangle.

在上述代码中,Shape类的draw()函数被声明为虚函数。派生类CircleRectangle分别重新定义了这个虚函数,并提供了自己的实现。

main()函数中,我们通过基类指针shape1shape2来存储CircleRectangle对象。通过调用draw()函数,根据实际所指的派生类对象来调用正确的函数实现。

2.2 纯虚函数

纯虚函数是一种特殊的虚函数,它没有实现。在基类中使用纯虚函数强制要求派生类提供自己的实现。以下是一个示例:

class Shape {
    
    
public:
    virtual void draw() const = 0;
};

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

int main() {
    
    
    Shape* shape1 = new Circle();

    shape1->draw();

    delete shape1;

    return 0;
}

运行结果:

Drawing a circle.

在上述代码中,Shape类的draw()函数被声明为纯虚函数,使用= 0来标记。由于纯虚函数没有实现,因此无法直接创建基类的对象。

派生类Circle提供了对纯虚函数的实现。通过基类指针shape1来存储Circle对象,并调用draw()函数。

3. 继承和多态

3.1 继承和多态的基本概念

继承是面向对象编程中的重要特性,通过继承可以创建新类来拥有已存在类的属性和方法。而多态则允许使用基类的指针或引用来操作派生类的对象,实现不同对象的不同行为。以下是一个示例:

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

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

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

int main() {
    
    
    Animal* animal1 = new Dog();
    Animal* animal2 = new Cat();

    animal1->makeSound();
    animal2->makeSound();

    delete animal1;
    delete animal2;

    return 0;
}

运行结果:

Woof!
Meow!

在上述代码中,Animal类作为基类声明了纯虚函数makeSound()。派生类DogCat分别提供了自己对该函数的实现。

main()函数中,我们通过基类指针animal1animal2来存储DogCat对象。通过调用makeSound()函数,根据实际所指的派生类对象来调用正确的函数实现。

3.2 多态的应用场景

多态的主要应用场景是在函数参数、返回值和容器的使用中。通过使用基类的指针或引用,可以实现对派生类对象的统一操作。以下是一个示例:

void performSound(const Animal& animal) {
    
    
    animal.makeSound();
}

int main() {
    
    
    Dog dog;
    Cat cat;

    performSound(dog);
    performSound(cat);

    return 0;
}

运行结果:

Woof!
Meow!

在上述代码中,performSound()函数接受一个基类Animal的引用作为参数。通过传递派生类对象(例如DogCat),函数会根据实际所指的对象来调用正确的函数实现。

这种技术被称为"向上转型",即将派生类对象当作基类对象使用。它不仅提高了代码的灵活性,还使得程序更具可扩展性和可维护性。

4. 总结

本文详细介绍了C++中虚函数、继承和多态的概念和用法。通过使用虚函数,我们可以在派生类中重新定义基类的函数,实现多态的效果。

继承和多态允许我们通过基类的指针或引用来操作派生类的对象,实现对不同对象的统一操作和不同行为。在函数参数、返回值和容器的使用中,可以应用多态来使代码更加灵活和可扩展。

猜你喜欢

转载自blog.csdn.net/qq_51447496/article/details/132241697