5-1 继承与派生

来源:互联网 发布:淘宝刷单流程360 编辑:程序博客网 时间:2024/06/05 11:47

Problem Description

通过本题目的练习可以掌握继承与派生的概念,派生类的定义和使用方法,其中派生类构造函数的定义是重点。
要求定义一个基类Point,它有两个私有的float型数据成员X,Y;一个构造函数用于对数据成员初始化;有一个成员函数void Move(float xOff, float yOff)实现分别对X,Y值的改变,其中参数xOff和yOff分别代表偏移量。另外两个成员函数GetX() 、GetY()分别返回X和Y的值。
Rectangle类是基类Point的公有派生类。它增加了两个float型的私有数据成员W,H; 增加了两个成员函数float GetH() 、float GetW()分别返回W和H的值;并定义了自己的构造函数,实现对各个数据成员的初始化。
编写主函数main()根据以下的输入输出提示,完成整个程序。
Input

6个float型的数据,分别代表矩形的横坐标X、纵坐标Y、宽度W,高度H、横向偏移量的值、纵向偏移量的值;每个数据之间用一个空格间隔
Output

输出数据共有4个,每个数据之间用一个空格间隔。分别代表偏移以后的矩形的横坐标X、纵坐标Y、宽度W,高度H的值
Example Input

5 6 2 3 1 2
Example Output

6 8 2 3
Hint

输入 -5 -6 -2 -3 2 10
输出 -3 4 0 0

#include<cstdio>#include<iostream>using namespace std;class Point{private:    float x, y;public:    Point(float x1 = 0, float y1 = 0)    {        x = x1;        y = y1;    }    void Move(float xoff, float yoff)    {        x = x + xoff;        y = y + yoff;    }    float GetX()    {        return x;    }    float GetY()    {        return y;    }};class Rectangle:public Point{private:    float w, h;public:    Rectangle(float x1 = 0, float y1 = 0, float w1 = 0, float h1 = 0) : Point(x1, y1)    {        w = w1;        h = h1;    }    float GetW()    {        return w;    }    float GetH()    {        return h;    }};int main(){    float x1, y1, w1, h1;    float xoff, yoff;    cin >> x1 >> y1 >> w1 >> h1 >> xoff >> yoff;    if(w1 < 0)        w1 = 0;    if(h1 < 0)        h1 = 0;    Rectangle r1(x1, y1, w1, h1);    r1.Move(xoff, yoff);    cout << r1.GetX() << " " << r1.GetY() << " " << r1.GetW() << " " << r1.GetH() << endl;}