Home Backend Development C++ How to implement polymorphism and inheritance features in C++?

How to implement polymorphism and inheritance features in C++?

Aug 27, 2023 pm 02:39 PM
c++ inherit Polymorphism

How to implement polymorphism and inheritance features in C++?

How to implement polymorphism and inheritance features in C?

In C, polymorphism and inheritance are two important features that can improve the readability and reusability of code. This article describes how to implement polymorphism and inheritance features in C and provides code examples.

1. Inheritance Features

Inheritance is one of the basic concepts in object-oriented programming. It allows us to create new classes and inherit properties and methods from existing classes.

In C, use the keyword "class" to define a class and implement inheritance through the ":" operator. When creating a derived class, you can choose to use public inheritance, private inheritance, or protected inheritance.

Code example:

#include <iostream>
using namespace std;

// 基类
class Shape {
public:
    virtual float getArea() = 0; // 纯虚函数
};

// 派生类
class Rectangle : public Shape {
public:
    float width;
    float height;
    
    float getArea() {
        return width * height;
    }
};

int main() {
    Rectangle rect;
    rect.width = 10;
    rect.height = 5;
    
    float area = rect.getArea();
    cout << "矩形的面积:" << area << endl;
    
    return 0;
}
Copy after login

In the above code, we create a base class Shape, which defines a pure virtual function getArea(). Then, we created a derived class Rectangle, which inherits the Shape class and implements the getArea() function. In the main function, we create a Rectangle object and calculate the area of ​​the rectangle.

2. Polymorphism Characteristics

Polymorphism refers to the different manifestations of objects. The same function can show different behaviors according to the types of different objects. In C, polymorphism is achieved through virtual functions and pointers or references to base classes.

Virtual functions need to be declared in the base class and overridden in the derived class. When a base class pointer or reference points to a derived class object, the function in the derived class is called.

Code example:

#include <iostream>
using namespace std;

// 基类
class Shape {
public:
    virtual float getArea() = 0; // 纯虚函数
};

// 派生类1
class Rectangle : public Shape {
public:
    float width;
    float height;
    
    float getArea() {
        return width * height;
    }
};

// 派生类2
class Circle : public Shape {
public:
    float radius;
    
    float getArea() {
        return 3.14 * radius * radius;
    }
};

int main() {
    Rectangle rect;
    rect.width = 10;
    rect.height = 5;
    
    Circle circle;
    circle.radius = 4;
    
    Shape* shape1 = &rect; // 基类指针指向派生类对象
    Shape* shape2 = &circle; // 基类指针指向派生类对象
    
    float area1 = shape1->getArea();
    float area2 = shape2->getArea();
    
    cout << "矩形的面积:" << area1 << endl;
    cout << "圆的面积:" << area2 << endl;
    
    return 0;
}
Copy after login

In the above code, we created two derived classes, Rectangle and Circle, both of which inherit from the base class Shape and implement the getArea() function. In the main function, we use the base class pointers shape1 and shape2 to point to the Rectangle and Circle objects respectively, and call the getArea() function through them to achieve polymorphism.

Summary:

By inheriting features, we can create classes with common properties and methods in C and achieve code reuse. Through the polymorphic feature, we can call the corresponding function based on the type of the actual object in the case of a base class pointer or reference. This improves code flexibility and scalability.

The above is the detailed content of How to implement polymorphism and inheritance features in C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Concurrency-safe design of data structures in C++ concurrent programming? Concurrency-safe design of data structures in C++ concurrent programming? Jun 05, 2024 am 11:00 AM

Concurrency-safe design of data structures in C++ concurrent programming?

C++ object layout is aligned with memory to optimize memory usage efficiency C++ object layout is aligned with memory to optimize memory usage efficiency Jun 05, 2024 pm 01:02 PM

C++ object layout is aligned with memory to optimize memory usage efficiency

How to implement the Strategy Design Pattern in C++? How to implement the Strategy Design Pattern in C++? Jun 06, 2024 pm 04:16 PM

How to implement the Strategy Design Pattern in C++?

Similarities and Differences between Golang and C++ Similarities and Differences between Golang and C++ Jun 05, 2024 pm 06:12 PM

Similarities and Differences between Golang and C++

How to implement a custom comparator in C++ STL? How to implement a custom comparator in C++ STL? Jun 05, 2024 am 11:50 AM

How to implement a custom comparator in C++ STL?

How do inheritance and polymorphism affect class coupling in C++? How do inheritance and polymorphism affect class coupling in C++? Jun 05, 2024 pm 02:33 PM

How do inheritance and polymorphism affect class coupling in C++?

How to copy a C++ STL container? How to copy a C++ STL container? Jun 05, 2024 am 11:51 AM

How to copy a C++ STL container?

What are the underlying implementation principles of C++ smart pointers? What are the underlying implementation principles of C++ smart pointers? Jun 05, 2024 pm 01:17 PM

What are the underlying implementation principles of C++ smart pointers?

See all articles