- 0
- 0
- 约8.77千字
- 约 10页
- 2018-02-15 发布于浙江
- 举报
[计算机软件及应用]第11章高级函数
第11章高级函数
-2-
本章内容安排
重载成员函数
使用默认值
初始化对象
复制构造函数
静态成员数据与静态成员函数
友元函数与友元类
-3-
成员函数的重载
在类中,可以定义多个同名的成员函数,但参数类型或个数不同,形成成员函数的重载关系。
编译器根据调用成员函数时,所传递实参的类型决定调用哪个版本函数。
不允许只通过返回值类型的不同形成重载关系。
-4-
Rectangle类
class Rectangle
{
public:
Rectangle(int newWidth, int newHeight)
:width(newWidth), height(newHeight) { }
~Rectangle() {}
void drawShape() const;
void drawShape(int width, int height) const;
private:
int width;
int height;
};
-5-
重载成员函数的实现
void Rectangle::drawShape() const
{
drawShape(width, height);
}
void Rectangle::drawShape(int width, int height) const
{
for (i
原创力文档

文档评论(0)