请各位大神和高人为小弟指点迷津,帮忙分析一下以下代码的执行过程,小弟在编译器上把代码跑了一遍,但还是不太明白,希望得到您的详细解释。
#include <iostream>
using namespace std;
class Shape
{
public: Shape(){}
virtual ~Shape() {}
virtual float Area() const=0;
virtual Shape * Clone() const=0;
};
class Circle:public Shape
{
public: Circle(float r):radius(r) {}
virtual ~Circle() {}
virtual float Area() const
{
cout<<"Circle's Area ="<<radius*radius*3.1415926<<endl;
return radius*radius*3.1415926f;
}
virtual Circle * Clone() const {return new Circle(*this); }
private:
Circle(const Circle& rhs)
{ cout<<"Copy Construct Circle!"<<endl;
radius=rhs.radius;
}
private: float radius;
};
class Rectangle:public Shape
{
public:
Rectangle(float w,float h):width(w),height(h) {}
virtual ~Rectangle() {}
virtual float Area() const
{ cout<<"Rectangle's Area ="<<width*height<<endl;
return width*height;
}
virtual Rectangle * Clone() const
{return new Rectangle(*this); }
private:
Rectangle(const Rectangle& rhs)
{ cout<<"Copy Construct Rectangle!"<<endl; width=rhs.width;height=rhs.height;
}
private:
float width;
float height;
};
int main(int argc, char* argv[])
{
Shape * p1=new Circle(2.5);
p1->Area();
Shape * p2=new Rectangle(5,11);
p2->Area();
Shape *p3=p1->Clone();
p3->Area();
Shape *p4=p2->Clone();
p4->Area();
delete p4;
delete p3;
delete p2;
delete p1;
return 0;
}
对于您的热心解答,小弟不胜感激!