例子C++版本
-
示例通过一个简单的图形类(Shape)及其派生类(Circle 和 Rectangle)来展示如何在C++中实现面向对象的特性
示例展示了C++面向对象编程的几个基本特性: -
封装:类的内部数据和方法被封装起来,外部只能通过提供的接口(成员函数)访问。
-
继承:通过 Shape 类作为基类,Circle 和 Rectangle 类继承了基本的接口。
-
多态:通过基类指针调用派生类实现的函数,展现了运行时多态。
#include <iostream>
#include <cmath>
using namespace std;
// 基类:形状
class Shape
{
public:
virtual void draw() const = 0; // 纯虚函数,要求派生类实现
virtual double area() const = 0; // 纯虚函数,要求派生类实现
virtual ~Shape() {} // 虚析构函数,保证派生类正确析构
};
// 派生类:圆形
class Circle : public Shape
{
private:
double radius;
public:
Circle(double r) : radius(r) {}
void draw() const override
{
cout << "Drawing a Circle." << endl;
}
double area() const override
{
return M_PI * radius * radius; // 圆的面积公式
}
};
// 派生类:矩形
class Rectangle : public Shape
{
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
void draw() const override
{
cout << "Drawing a Rectangle." << endl;
}
double area() const override
{
return width * height; // 矩形的面积公式
}
};
int main()
{
// 使用指针管理不同的形状对象,展示多态
Shape *shapes[2];
shapes[0] = new Circle(5.0); // 创建一个半径为5的圆
shapes[1] = new Rectangle(4.0, 6.0); // 创建一个宽4,高6的矩形
if (NULL == shapes[0] || NULL == shapes[0])
{
perror("create failed");
return -1;
}
for (int i = 0; i < 2; ++i)
{
shapes[i]->draw(); // 调用多态方法
cout << "Area: " << shapes[i]->area() << endl; // 输出面积
}
// 清理内存
for (int i = 0; i < 2; ++i)
{
delete shapes[i];
}
return 0;
}
-
基类 Shape:包含了两个纯虚函数 draw() 和 area(),这意味着 Shape 是一个抽象类,不能直接实例化,必须由派生类实现这两个函数。
-
派生类 Circle 和 Rectangle:分别继承自 Shape 类,并实现了 draw() 和 area() 方法。Circle 类通过圆的半径计算面积,而 Rectangle 类通过宽度和高度计算面积。
-
多态:在 main() 函数中,使用 Shape* 类型的数组来存储不同的 Shape 对象。通过指针调用 draw() 和 area() 方法时,程序会根据对象的实际类型(圆形或矩形)执行相应的实现,这就是多态。
-
内存管理:通过 new 动态分配内存,并在使用完毕后通过 delete 释放内存,防止内存泄漏。
转化C版本
- C 语言本身不直接支持面向对象编程(OOP),但是我们可以通过结构体和函数指针来模拟面向对象的特性。下面是将上述 C++ 面向对象代码转化为 C 语言的实现:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// 形状接口 (基类)
typedef struct Shape
{
void (*draw)(struct Shape *); // 绘制图形的函数指针
double (*area)(struct Shape *); // 计算面积的函数指针
} Shape;
// 圆形结构体 (派生类)
typedef struct
{
Shape shape; // 继承自Shape
double radius; // 圆的半径
} Circle;
// 矩形结构体 (派生类)
typedef struct
{
Shape shape; // 继承自Shape
double width; // 矩形宽度
double height; // 矩形高度
} Rectangle;
// 圆形的绘制函数
void Circle_draw(Shape *s)
{
Circle *c = (Circle *)s;
printf("Drawing a Circle.\n");
}
// 圆形的面积计算函数
double Circle_area(Shape *s)
{
Circle *c = (Circle *)s;
return M_PI * c->radius * c->radius;
}
// 矩形的绘制函数
void Rectangle_draw(Shape *s)
{
Rectangle *r = (Rectangle *)s;
printf("Drawing a Rectangle.\n");
}
// 矩形的面积计算函数
double Rectangle_area(Shape *s)
{
Rectangle *r = (Rectangle *)s;
return r->width * r->height;
}
// 创建圆形对象
Circle *Circle_create(double r)
{
Circle *c = (Circle *)malloc(sizeof(Circle));
if (c)
{
c->shape.draw = Circle_draw;
c->shape.area = Circle_area;
c->radius = r;
}
return c;
}
// 创建矩形对象
Rectangle *Rectangle_create(double w, double h)
{
Rectangle *r = (Rectangle *)malloc(sizeof(Rectangle));
if (r)
{
r->shape.draw = Rectangle_draw;
r->shape.area = Rectangle_area;
r->width = w;
r->height = h;
}
return r;
}
int main()
{
// 使用函数指针调用多态方法
Shape *shapes[2];
shapes[0] = (Shape *)Circle_create(5.0); // 半径为5的圆形;
shapes[1] = (Shape *)Rectangle_create(4.0, 6.0); // 宽4,高6的矩形;
if (NULL == shapes[0] || NULL == shapes[0])
{
perror("create failed");
return -1;
}
for (int i = 0; i < 2; ++i)
{
shapes[i]->draw(shapes[i]); // 调用多态方法
printf("Area: %f\n", shapes[i]->area(shapes[i])); // 输出面积
}
// 清理内存
for (int i = 0; i < 2; ++i)
{
free(shapes[i]);
}
return 0;
}
- 结构体模拟类:
Shape 结构体模拟基类,包含两个函数指针 draw 和 area,分别对应绘制图形和计算面积的功能。
Circle 和 Rectangle 结构体模拟派生类,通过包含一个 Shape 类型的结构体来实现继承。 - 函数指针模拟方法:
draw 和 area 是在 Shape 结构体中声明的函数指针,具体的实现(如 Circle_draw 或 Rectangle_area)由派生类来提供。
在每个派生类(如 Circle 和 Rectangle)的初始化函数中,将对应的函数指针指向具体的实现。 - 初始化函数:
Circle_create 和 Rectangle_create:这两个函数负责动态分配内存并初始化对象。我们通过 malloc 为 Circle 和 Rectangle 结构体分配内存,并为它们的函数指针赋值。这样就实现了对象的创建和初始化。 - 多态:
在 main 函数中,使用 Shape* 类型的指针数组 shapes 来管理不同类型的对象。通过调用函数指针,程序会根据对象的类型执行相应的方法,从而实现了多态 - 内存管理:在 main 函数结束后,我们使用 free 函数释放了 Circle 和 Rectangle 对象占用的内存,以防止内存泄漏。
总结
- 通过结构体和函数指针,模拟了 C++ 中的面向对象特性,如封装、继承和多态。
- 因为C语言本身不直接直接面向对象机制,无法直接访问到自身对象数据,所以对this对象的操作要通过传参的方式实现。