5-1 继承与派生

来源:互联网 发布:知乎 济南血液病医院 编辑:程序博客网 时间:2024/06/05 16:41

5-1 继承与派生

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

通过本题目的练习可以掌握继承与派生的概念,派生类的定义和使用方法,其中派生类构造函数的定义是重点。

要求定义一个基类Point,它有两个私有的float型数据成员X,Y;一个构造函数用于对数据成员初始化;有一个成员函数void Move(float xOff, float yOff)实现分别对X,Y值的改变,其中参数xOffyOff分别代表偏移量。另外两个成员函数GetX() GetY()分别返回XY的值。

Rectangle类是基类Point的公有派生类。它增加了两个float型的私有数据成员W,H; 增加了两个成员函数float GetH() float GetW()分别返回WH的值;并定义了自己的构造函数,实现对各个数据成员的初始化。

编写主函数main()根据以下的输入输出提示,完成整个程序。

Input

 

6float型的数据,分别代表矩形的横坐标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

Author


#include<bits/stdc++.h>using namespace std;class Point{private:    double x, y;public:    Point()    {        x = 0;        y = 0;    }    void Move(double xoff, double yoff)    {        x += xoff;        y += yoff;    }    void getPoint()    {        cin>>x>>y;    }    void showPoint()    {        cout<<x<<" "<<y;    }};class Rect:public Point{private:    double w, h;public:    Rect()    {        w = 0;        h = 0;    }    void setdata()    {        getPoint();        cin>>w>>h;        if(w <= 0)            w = 0;        if(h <= 0)            h = 0;    }    void display()    {        showPoint();        cout<<" "<<w<<" "<<h<<endl;    }};int main(){    double m, n;    Rect p;    p.setdata();    cin>>m>>n;    p.Move(m, n);    p.display();    return 0;}



  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class Point  
  6. {  
  7. private:  
  8.     float x;  
  9.     float y;  
  10. public:  
  11.     Point(float x1 = 0, float y1 = 0)               //基类初始化  
  12.     {  
  13.         x = x1;  
  14.         y = y1;  
  15.     }  
  16.     void Move(float xoff, float yoff)               //移动  
  17.     {  
  18.         x = x + xoff;  
  19.         y = y + yoff;  
  20.     }  
  21.     float Getx()                                   //收集每个数据,用来输出  
  22.     {  
  23.         return x;  
  24.     }  
  25.     float Gety()  
  26.     {  
  27.         return y;  
  28.     }  
  29. };  
  30.   
  31. class Rectangle : public Point  
  32. {  
  33. private:  
  34.     float w;  
  35.     float h;  
  36. public:  
  37.     Rectangle(float x1 = 0, float y1 = 0, float w1 = 0, float h1 = 0) : Point(x1, y1)       //派生类初始化  
  38.     {  
  39.         w = w1;  
  40.         h = h1;  
  41.     }  
  42.     float Getw()  
  43.     {  
  44.         return w;  
  45.     }  
  46.     float Geth()  
  47.     {  
  48.         return h;  
  49.     }  
  50. };  
  51.   
  52. int main()  
  53. {  
  54.     float x1, y1, w1, h1, xoff, yoff;  
  55.     cin >> x1 >> y1 >> w1 >> h1 >> xoff >> yoff;  
  56.     if(w1 < 0)  
  57.         w1 = 0;  
  58.     if(h1 < 0)  
  59.         h1 = 0;  
  60.     Rectangle r1(x1, y1, w1, h1);           //Public继承,直接可以调用  
  61.     r1.Move(xoff, yoff);  
  62.     cout << r1.Getx() << " " << r1.Gety() << " " << r1.Getw() << " " << r1.Geth() << endl;  
  63.     return 0;  
  64. }  



原创粉丝点击