河南成教 / 面向对象程序设计
3[应用题]
#include
float const PI=3.14;
class point
{
private:
float x,y,area;
public:
point(float x1=0.0,float y1=0.0)
{
cout<<"point构造函数被调用!
";
}
virtual ~point()
{
cout<<"point析构函数被调用!
";}
virtual float getarea(){return 0.0;}
};
class circle:public point
{private:
float r;
public:
circle(float x1,float y1,float Nr):point(x1,y1)
{
r=Nr;
cout<<"circle构造函数被调用!
";
}
~circle(){cout<<"circle析构函数被调用!
";}
float getarea()
{
return PI*r*r;
}
};
void main()
{
point *p,p1(3,4);
p=&p1;
cout<getarea()<
circle c(1,1,1);
p=&c;
cout<getarea()<<>
}
程序运行结果为:
3[应用题]
#include
float const PI=3.14;
class point
{
private:
float x,y,area;
public:
point(float x1=0.0,float y1=0.0)
{
cout<<"point构造函数被调用!
";
}
virtual ~point()
{
cout<<"point析构函数被调用!
";}
virtual float getarea(){return 0.0;}
};
class circle:public point
{private:
float r;
public:
circle(float x1,float y1,float Nr):point(x1,y1)
{
r=Nr;
cout<<"circle构造函数被调用!
";
}
~circle(){cout<<"circle析构函数被调用!
";}
float getarea()
{
return PI*r*r;
}
};
void main()
{
point *p,p1(3,4);
p=&p1;
cout<
circle c(1,1,1);
p=&c;
cout<
}
程序运行结果为:
参考答案: