标题:构造函数
取消只看楼主
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
结帖率:100%
已结贴  问题点数:10 回复次数:0 
构造函数
#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();有什么作用啊
搜索更多相关主题的帖子: display include public change double 
2013-11-10 22:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-423342-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.608126 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved