0
点赞
收藏
分享

微信扫一扫

http项目改为/支持https的方案、无需修改后台代码

水沐由之 2024-05-31 阅读 5

         多态性时面向对象的一个重要特性,主要由重构和虚函数来实现。重构函数时一种静态的多态性,在编译时完成。虚函数则提供了一种动态多态性,在程序运行时体现。下面给出了静态关联和动态关联的4个例程,大致给出了多态性在面向对象中的应用。        

静态多态性之运算符重载

主文件入口代码:

#include "Circle.h"

int main() {
    Point p(3.5, 6.4);
    cout << "p = " << p <<endl;

    Circle c(3.5, 6.4, 2);
    cout << "c = " << c << endl;

    Point &pR = c;
    cout << pR << endl;
    return 0;
}

Point.cpp代码:

#include "Point.h"

Point::Point(float a, float b)
{
    x = a;
    y = b;
}

void Point::setPoint(float a, float b) {
    x = a;
    y = b;
}

ostream &operator<<(ostrea
举报

相关推荐

0 条评论