求指教:这段代码的执行过程
请高人帮忙分析一下以下代码的执行过程,希望尽量详细一点,在下不胜感激!#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;
}
对于您的热心解答,在下先行谢过!