Sdut 5-1 Inheritance and derivation

Source: Internet
Author: User
Tags gety
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;
 }




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.