标签:
本实例来自《C++ Primer Plus》(第六版) 第十三章
virtual double Area() const = 0;//a pure virtual function
Circle *c = new Circle(1, 0, 1);
Circle d(1, 1, 0);
#pragma once
//基类指针指向派生类的时候,不能使用派生类新定义的函数的问题。
class BaseEllipse {
private:
double x;
double y;
public:
BaseEllipse(double x0 = 0,double y0 = 0):x(x0),y(y0){}
virtual ~BaseEllipse(){}
void Move(int nx, int ny) { x = nx; y = ny; }
virtual double Area() const = 0;//a pure virtual function
virtual void Scale(double sa, double sb){};
virtual void Scale(double sr){}
virtual void Rotate(double nang){}
};
class Ellipse :public BaseEllipse {
private:
double a;
double b;
double angle;
public:
Ellipse(double x0 = 0, double y0 = 0, double a0 = 0, double b0 = 0, double angle0 = 0) :BaseEllipse(x0, y0) { a = a0; b = b0; angle = angle0; }
virtual double Area() const { return 3.14*a*b; }
virtual void Scale(double sa, double sb) { a *= sa; b *= sb; }
virtual void Rotate(double nang) { angle += nang; }
};
class Circle :public BaseEllipse {
private:
double r;
public:
Circle(double x0=0, double y0=0, double r0=0) :BaseEllipse(x0, y0) { r = r0; }
virtual double Area() const { return 3.14*r*r; }
virtual void Scale(double sr) { r *= sr; }
};
标签:
原文地址:http://www.cnblogs.com/zzandliz/p/5062642.html