个人技术分享

         多态性时面向对象的一个重要特性,主要由重构和虚函数来实现。重构函数时一种静态的多态性,在编译时完成。虚函数则提供了一种动态多态性,在程序运行时体现。下面给出了静态关联和动态关联的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