C++高级函数调用:深入探索与实践
1. 重载成员函数
在C++中,函数重载是一项强大的特性,成员函数也可以进行重载。通过编写多个同名但参数不同的函数,就能实现函数重载。下面通过 Rectangle 程序来演示成员函数的重载。
1.1 Rectangle 程序示例
#include <iostream>
class Rectangle
{
public:
Rectangle(int width, int height);
~Rectangle() {}
void drawShape() const;
void drawShape(int width, int height) const;
private:
int width;
int height;
};
Rectangle::Rectangle(int newWidth, int newHeight)
{
width = newWidth;
height = newHeight;
}
void Rectangle::drawShape() const
{
drawShape(width, height);
}
void Rectangle::drawShape(int width, int height) const
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
超级会员免费看
订阅专栏 解锁全文
1253

被折叠的 条评论
为什么被折叠?



