构造函数
#include <iostream>#define PI 3.14159
using namespace std;
class Point
{
int x,y;
public:
Point();
Point(int, int);
void change(int,int);
void display();
};
class Circle:public Point
{
int r;
double s;
public:
Circle();
Circle(int,int,int);
void change(int,int,int);
double area();
void display();
};
class Cylinder:public Circle
{
int h;
double v;
public:
Cylinder();
Cylinder(int,int,int,int);
void change(int,int,int,int);
double area();
double V();
void display();
};
//****************************************************************************************************//
Point::Point():x(0),y(0){}
Point::Point(int xx,int yy):x(xx),y(yy){}
void Point::change(int xx,int yy)
{
x = xx;
y = yy;
}
void Point::display()
{
cout<<"x = "<<x<<endl
<<"y = "<<y<<endl;
}
//*************************Circle*******************************/
Circle::Circle():Point(),r(0),s(0){}//
Circle::Circle(int xx,int yy,int rr):Point(xx,yy),r(rr),s(0){}
void Circle::change(int xx,int yy,int rr)
{
Point::change(xx,yy);
r = rr;
}
double Circle::area()
{
return s = PI*r*r;
}
void Circle::display()
{
Point::display();
cout<<"r = "<<r<<endl
<<"s = "<<area()<<endl;
}
//*************************Cylinder*******************************/
Cylinder::Cylinder():Circle(),h(0),v(0){}
Cylinder::Cylinder(int xx,int yy,int rr,int hh):Circle(xx,yy,rr),h(hh),v(0){}
void Cylinder::change(int xx,int yy,int rr,int hh)
{
Circle::change(xx,yy,rr);
h = hh;
}
double Cylinder::area()
{
return Circle::area();
}
double Cylinder::V()
{
return v = area()*h;
}
void Cylinder::display()
{
Circle::display();
cout<<"h = "<<h<<endl;
cout<<"v = "<<V()<<endl;
}
//****************************************************************************//
void main()
{
Cylinder cld(4,5,2,4);
cld.display();
cld.change(12,15,57,32);
cout<<"更改之后:"<<endl;
cld.display();
system("pause");
}
为什么程序中有Point();和Point(int, int);两个构造函数,去掉Point();程序就出错,这是为什么?Point();有什么作用啊