C is a language that supports object-oriented programming, and a major feature of object-oriented programming is polymorphism. Polymorphism refers to the different behaviors produced by different objects when performing the same operation. In C, polymorphism is achieved through function overloading and the use of virtual functions. The following will explore polymorphism in C to help readers better grasp this concept.
1. Overloading of functions
Overloading of functions means multiple functions with the same name are defined in the same scope, but their parameter types, number of parameters or return value types are different. . In this way, when the function is called, the compiler will automatically choose to call the corresponding function based on the parameters passed. For example:
void print(int a){ cout<<"int: "<<a<<endl; } void print(double b){ cout<<"double: "<<b<<endl; } int main(){ print(10); //调用print(int a)函数 print(3.14); //调用print(double b)函数 return 0; }
The function print is overloaded and defined for int type and double type parameters respectively. When the print function is called in the main function, the compiler selects the corresponding function to call based on the type of parameters passed. This is an example of polymorphism achieved through function overloading.
2. Virtual function
Virtual function is a function defined in the parent class, which can be overridden by subclasses. Define the function as a virtual type in the base class. When the subclass inherits the function, it also needs to be defined as a virtual function. The function name and parameters must be exactly the same. At runtime, virtual functions are dynamically bound according to the actual running object type to achieve polymorphism. For example:
class Shape{ public: virtual void area(){ //定义虚函数area cout<<"This is a shape"<<endl; } }; class Circle:public Shape{ public: void area(){//重写虚函数area cout<<"This is a circle"<<endl; } }; class Rectangle:public Shape{ public: void area(){//重写虚函数area cout<<"This is a rectangle"<<endl; } }; int main(){ Shape *shape; Circle circle; Rectangle rectangle; shape = &circle; shape->area();//调用circle类中的虚函数area shape = &rectangle; shape->area();//调用rectangle类中的虚函数area return 0; }
In the above code, a Shape class is defined, which contains a virtual function area. The Circle and Rectangle classes inherit the Shape class and rewrite the virtual function area in it. In the main function, a pointer to the Shape type is defined, assigned to the addresses of the Circle object and the Rectangle object, and the area functions are called respectively. Since the area function is a virtual function and is dynamically bound according to the actual running object type, the output results are "This is a circle" and "This is a rectangle" respectively. This is an example of virtual functions implementing polymorphism.
3. Pure virtual function
Pure virtual function means that the function body of the virtual function is empty, and subclasses must rewrite the function to inherit the class. When there are pure virtual functions in a base class, the class is called abstract class. For example:
class Shape{ public: virtual void area() = 0;//定义纯虚函数area }; class Circle:public Shape{ public: void area(){ cout<<"This is a circle"<<endl; } }; class Rectangle:public Shape{ public: void area(){ cout<<"This is a rectangle"<<endl; } }; int main(){ Shape *shape; Circle circle; Rectangle rectangle; shape = &circle; shape->area();//调用circle类中的虚函数area shape = &rectangle; shape->area();//调用rectangle类中的虚函数area return 0; }
In the above code, the pure virtual function area is defined in the Shape class. The Circle and Rectangle classes must inherit this function to inherit the Shape class, otherwise a compilation error will be reported. In the main function, polymorphism is achieved by pointing pointers to Circle and Rectangle objects.
Summary:
It can be seen that there are many ways to implement polymorphism in C, among which function overloading and virtual functions are the two most commonly used methods. By implementing polymorphism, the flexibility and reusability of the code can be greatly improved, and the code can also be easier to read and maintain. In actual programming, you should focus on understanding the nature and uses of polymorphism, and master the methods and techniques of using polymorphism, so as to strengthen your programming abilities.
The above is the detailed content of Exploring polymorphism in C++. For more information, please follow other related articles on the PHP Chinese website!