C++习题-类04

来源:互联网 发布:布瑞克农业数据终端 编辑:程序博客网 时间:2024/05/18 01:28
/*
定义一个Point类,有私有数据成员X和Y是double型的;
由Point类的构造函数完成对X和Y的初始化;
有成员函数getX()可获取数据成员X的值、
getY()可获取数据成员Y的值。
由Point类公有派生出Rectangle类,
Rectangle类中新增私有数据成员W和H是double型的;
Rectangle类的构造函数能完成对所有数据成员的初始化;
*/

#include <iostream>

using namespace std;

class Point{
    public:
        Point();
        Point(double x,double y);
        double getX();
        double getY();
    private:
        double X;
        double Y;
};

class Rectangle:public Point{
    public:
        Rectangle();
        Rectangle(double x,double y,double w,double h);
    private:
        double W;
        double H;


};

int main()
{
    Rectangle rect(1.0,2.0,3.0,4.0);
    cout << "X=" << rect.getX() <<endl;
    cout << "Y=" << rect.getY() <<endl;

    return 0;
}


Point::Point(double x,double y){
    X=x;
    Y=y;
}

double Point::getX(){
    return X;
}
double Point::getY(){
    return Y;
}

Rectangle::Rectangle(double x,double y,double w,double h):Point(x,y){
    W=w;
    H=h;
}

原创粉丝点击