5-1 inheritance and derivation
Time limit:1000ms Memory limit:65536k
Topic Description
The practice of this topic allows you to master the concepts of inheritance and derivation, the definition and use of derived classes, where the definition of a derived class constructor is the focus.
Requires defining a base class point, which has two private float-type data members x,y; A constructor is used to initialize data members; a member function void Move (float xOff, float yoff) implements a change to the X,y value respectively, Where the parameters Xoff and Yoff represent the offsets, respectively. The other two member functions Getx (), GetY () return values for x and Y, respectively.
The rectangle class is a public derived class of the base class point. It adds two float-type private data member w,h; Added two member functions float Geth (), float GETW () return the values of W and H respectively, and define their own constructors to initialize the individual data members.
Write the main function main () complete the program according to the following input and output prompts. input
6 float data representing the horizontal axis x, ordinate y, width w, height h, horizontal offset value, and longitudinal offset value of the rectangle respectively, with a space interval between each data
There are 4 output data, with a space interval between each data. Represents the value of the horizontal axis x, ordinate y, width w, and height H of the rectangle after the offset.
5 6 2 3 1 2
Sample Output
6 8 2 3
TipsInput-5-6-2-3 2 10
Output-3 4 0 0 sources
#include <iostream> using namespace std;
class Point//Declaration point Class {private:float x, y;
Public:point (float x1=0, float y1=0): x (x1), Y (y1) {};//define constructor void Move (float xoff, float yoff);//Declare Move function float Getx () const {return x;} Define member functions Getx float Gety () Const{return y;}
Define member function Gety};
void point:: Move (float Xoff, float yoff)//define move function {x = x + Xoff;
y = y + yoff;
Class Rectangle:public point//defines Rectangle classes {private:float W, H;
Public:rectangle (float x1, float y1, float W1, float h);/declare Rectangle function float GETW () Const{return W;}
Float Geth () Const{return h;}};
Rectangle:: Rectangle (float x1, float y1, float W1, float H1): Point (x1, y1)//define Rectangle function {w=w1 >= 0? w1:0; H=h1 >= 0?
h1:0;
int main ()//main function {float x, y, W, H, Xoff, Yoff;
cin>>x>>y>>w>>h>>xoff>>yoff;
Point P1 (x, y);
Rectangle R1 (x, Y, W, h); P1.
Move (Xoff, Yoff); cout <<p 1. Getx () << "" <<p1. Gety () << "" <<r1. GETW () << "" <<r1.
Geth () << Endl;
return 0;
}