C + + Class and Object Experiment (VI)

Source: Internet
Author: User
Tags gety

Problem:

1. Design describes the point CPoint class on the plane coordinates, which satisfies the following requirements:

• has x, y coordinate information;

• A constructor with default parameter values for initializing x and y coordinate information, respectively;

• Getx and Gety functions for obtaining x and y information, setx and sety functions for setting x and y information;

2, the design of a rectangular class Crectangle, the class meets the following requirements:

• Coordinate information with two points in the lower left and upper right corner of the rectangle, the data type of the two points is cpoint;

• With parametric constructor crectangle (const CPoint &, const CPoint &), the parameters are used to set the coordinate information of the lower left and upper right corner two points respectively;

• Features Setlpoint (const CPoint &) and Setrpoint (const CPoint &) with two point coordinates set in the lower-left corner and the upper-right corner

• Has the ability to obtain perimeter (getperimeter) and gain area (Getarea).

3. In the main function, complete the following tasks:

• Dynamically create an object A_rectagnle of the Crectangle class whose initial lower-left and upper-right coordinates are (2,5), (6,8), call Getperimeter and Getarea to get the perimeter and area of the rectangle, and display the perimeter and area on the screen;

///////////////////////////////////////////////////////

Next, I gross position the code to send out:

I used the. h and. cpp files, which are declared in the. h file and defined in the. cpp file.

The total works include: Cpoint.h cpoint.cpp rectangle.h rectangle.cpp main.cpp

Cpoint.h

#ifndef _cpoint_h#define_cpoint_h#include<iostream>using namespacestd;classcpoint{ Public: CPoint (intXinty): x (x), Y (y) {}intGetX (); intGetY (); voidSetX (int); voidSety (int); Private:    intx, y;};#endif

Cpoint.cpp

" cpoint.h " int Cpoint::getx () {    return  x;} int cpoint::gety () {    return  y;} void CPOINT::SETX (int  x) {    x=x;} void Cpoint::sety (int  y) {    y=y;}

Rectangle.h

#ifndef _rectangle_h#define_rectangle_h#include"cpoint.h"classcrectangle{ Public: Crectangle (ConstCPoint &a,ConstCPoint &b): C1 (a), C2 (b) {}voidSetlpoint (ConstCPoint &); voidSetrpoint (ConstCPoint &); voidGetperimeter (); voidGetarea ();Private: CPoint c1,c2;};#endif

Rectangle.cpp

#include"rectangle.h"#include"cpoint.h"voidCrectangle::setlpoint (ConstCPoint &a) {C1=A;}voidCrectangle::setrpoint (ConstCPoint &b) {C2=b;}voidCrectangle::getperimeter () {intPerimeter=0; Perimeter= (C2. GetX ()-c1. GetX ()) *2+ (C2. GetY ()-c1. GetY ()) *2; cout<<"Perimeter is:"<< Perimeter <<Endl;}voidCrectangle::getarea () {intArea=0; Area= (C2. GetX ()-c1. GetX ()) * (C2. GetY ()-C1.    GetY ()); cout<<"Area is :"<<Area<<Endl;}

Main.cpp

 #include  rectangle.h   " int   main () { const  CPoint A (2 , 5 ), B (6 , 8  );    Crectangle  *p=new   Crectangle (A, b);    P ->setlpoint (a);    P ->setrpoint (b);    P ->getperimeter ();    P ->getarea ();    Delete p;  return  0  ;}  

OK, that's it!

C + + Class and Object Experiment (VI)

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.