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:
#includeusing 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; }
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:
#includeusing 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 = ▭ // 基类指针指向派生类对象 Shape* shape2 = &circle; // 基类指针指向派生类对象 float area1 = shape1->getArea(); float area2 = shape2->getArea(); cout << "矩形的面积:" << area1 << endl; cout << "圆的面积:" << area2 << endl; return 0; }
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!