5-1 Inheritance and Derivation Time Limit: 1000 ms memory limit: 65536 K Description
Through the exercises in this topic, you can master the concepts of inheritance and derivation. The definition and usage of the derived class, the definition of the derived class constructor is the focus.
Define a base class point, which has two private float data members X and Y. A constructor is used to initialize data members. A member function void move (float xoff, float yoff) to change the x and y values respectively. The xoff and yoff parameters represent the offset respectively. The other two member functions getx () and Gety () return the values of X and Y respectively.
The rectangle class is a public derived class of the base class point. It adds two private data members W and H of the float type, and adds two member functions float geth () and float getw () to return the values of W and h respectively; it also defines its own constructor to initialize each data member.
Compile the main function main () and complete the entire program according to the following input and output prompts.
Input
Six float data, representing the X, Y, W, H, horizontal offset, and vertical offset of the rectangle respectively. Each data is separated by a space.
Output
There are 4 output data records, each of which is separated by a space. The X, Y, W, and H coordinates of the rectangle after the offset.
Sample Input
5 6 2 3 1 2
Sample output
6 8 2 3
Prompt-5-6-2-3 2 10
Output-3 4 0 0
Source
# Include <iostream> using namespace STD; Class Point // defines the Point class {private: Float X, Y; public: Point (float X1 = 0, float Y1 = 0 ): X (X1), y (Y1) {}; // define the constructor void move (float xoff, float yoff); // declare the move function float getx () const {return X;} // define the member function getx float Gety () const {return y;} // define the member function Gety}; void point: Move (float xoff, float yoff) // defines the move function {x = x + xoff; y = Y + yoff;} class rectangle: Public point // defines the rect. Angle class {private: Float W, H; public: rectangle (float X1, float Y1, float W1, float H); // declare the 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 the 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 <p1.getx () <"" <p1.gety () <"<r1.getw () <" "<r1.geth () <Endl; return 0 ;}
Sdut 5-1 Inheritance and Derivation