C++是一种支持面向对象编程的高级编程语言。多态性是其面向对象编程的核心特性之一,它允许同一类型的变量在不同的情况下表现出不同的行为。在C++中实现多态性有多种方法,本文将从函数的重载、虚函数、抽象类和接口等方面进行详细阐述。
一、函数的重载
函数的重载是指在同一作用域内定义多个同名但参数列表不同的函数。函数重载并不涉及到虚函数表的查找和转移,是一种静态绑定的方法。下面是一个例子:
// 函数的重载
#include <iostream>
using namespace std;
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
class Rectangle: public Shape {
public:
int area() {
return (width * height);
}
};
class Triangle: public Shape {
public:
int area() {
return (width * height / 2);
}
};
int main() {
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
// 调用Rectangle的area函数
cout << "Rectangle area: " << Rect.area() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// 调用Triangle的area函数
cout << "Triangle area: " << Tri.area() << endl;
return 0;
}
在本例中,Rectangle和Triangle两个类都继承自Shape类,都有setWidth和setHeight函数,并分别定义了自己的area函数来计算面积。通过函数的重载,程序在编译时就可以静态地确定应该调用哪个函数,实现多态性。
二、虚函数
虚函数是C++中实现多态性的一种方法。虚函数是在父类中定义的一个函数,它在子类中可以被重写,这样当使用子类对象调用该函数时,就可以选择运行子类重写的函数而不是父类中定义的函数。虚函数利用虚表实现动态绑定,可以在运行时根据对象类型确定调用的函数。
在C++中,要将一个函数声明为虚函数,只需要在函数声明前加上关键字virtual,如下所示:
class Base {
public:
virtual void print() {
cout << "Base class print" << endl;
}
};
class Derived: public Base {
public:
void print() { // 重写基类的print函数
cout << "Derived class print" <print(); // 调用由派生类重写的print函数
return 0;
}
在上面的例子中,Base类的print函数被声明为虚函数,在Derived类中被重写。在main函数中,通过基类指针bptr访问派生类对象的print函数时,程序会动态绑定并调用Derived类中的print函数。
三、抽象类
抽象类是一种特殊的类,它不能直接被实例化,而是用作派生其他类的基类。抽象类最重要的特性是其中包含至少一个纯虚函数。纯虚函数没有实现,它是一个特殊的虚函数,用来定义接口,并要求派生类必须实现该函数。在C++中,纯虚函数的声明形式如下:
virtual int foo() = 0;
抽象类的存在可以使派生类具有某种共性的特征,同时也不会影响到派生类的独特性。下面是一个使用抽象类实现多态的例子:
class Shape {
public:
virtual int getArea() = 0; // 纯虚函数
};
class Rectangle: public Shape {
public:
Rectangle(int w, int h) {
width = w;
height = h;
}
int getArea() {
return (width * height);
}
private:
int width;
int height;
};
class Triangle: public Shape {
public:
Triangle(int w, int h) {
width = w;
height = h;
}
int getArea() {
return (width * height / 2);
}
private:
int width;
int height;
};
int main() {
Rectangle Rect(5, 7);
Triangle Tri(5, 7);
Shape* shape1 = &Rect; // 基类指针指向派生类对象
Shape* shape2 = &Tri; // 基类指针指向派生类对象
cout << "Rectangle area: " <getArea() << endl;
cout << "Triangle area: " <getArea() << endl;
return 0;
}
在上面的例子中,Shape类是抽象类,它包含了一个纯虚函数getArea。Rectangle和Triangle两个类都继承自Shape类,并实现了自己的getArea函数。程序中,定义了shape1和shape2两个Shape类型的指针,用于指向Rectangle和Triangle对象,实现多态性。
四、接口
接口是一个纯虚类,其中只包含纯虚函数,用来定义一个类的行为规范。接口可以让其他类继承并实现其中的纯虚函数,从而拥有相同的行为规范。在C++中,可以通过抽象类来实现接口的功能。
下面是一个使用抽象类实现接口的例子:
class IShape {
public:
virtual int getArea() = 0;
virtual int getPerimeter() = 0;
};
class Rectangle: public IShape {
public:
Rectangle(int w, int h) {
width = w;
height = h;
}
int getArea() {
return (width * height);
}
int getPerimeter() {
return (2 * (width + height));
}
private:
int width;
int height;
};
class Triangle: public IShape {
public:
Triangle(int w, int h) {
width = w;
height = h;
}
int getArea() {
return (width * height / 2);
}
int getPerimeter() {
return (width + height + sqrt(width * width + height * height));
}
private:
int width;
int height;
};
int main() {
Rectangle Rect(5, 7);
Triangle Tri(5, 7);
IShape* shape1 = &Rect;
IShape* shape2 = &Tri;
cout << "Rectangle area: " <getArea() << ", perimeter: " <getPerimeter() << endl;
cout << "Triangle area: " <getArea() << ", perimeter: " <getPerimeter() << endl;
return 0;
}
在本例中,定义了一个IShape接口,包含了getArea和getPerimeter两个纯虚函数。Rectangle和Triangle两个类都继承自IShape接口,并实现了它的纯虚函数。程序中,定义了shape1和shape2两个IShape类型的指针,用于指向Rectangle和Triangle对象,实现多态性,同时遵循了IShape接口的行为规范。
原创文章,作者:KCQHF,如若转载,请注明出处:https://www.506064.com/n/315620.html
微信扫一扫
支付宝扫一扫