Home c++ Implement the Class Rectangle C++

Implement the Class Rectangle C++

Author

Date

Category

Help please with the realization of the Rectangle class. It is necessary to create two private fields (values ​​integer for the sides of the rectangle), methods for setting the values ​​of the parties and the INT Square () method, the calculating area of ​​the rectangle. In the Main function, declare several objects of this class.

The problem is, I do not understand how using the methods to set the values ​​of the parties (I repeat, the values ​​of the parties are the private fields of the class), but then, using these values, find the area of ​​the figure.


Answer 1

# include & lt; iostream & gt;
Class Rectangle.
{
  INT LENGTH;
  int height;
Public:
  / * Default designer initializes
   * Length and Height fields values ​​0
   * /
  Rectangle ()
    : Length (0), Height (0)
  {}
  / * Designer with parameters initializes
   * Length and Height fields values
   * _Length and _Height transmitted as
   * parameters, respectively
   * /
  Rectangle (int _length, int _height)
    : Length (_Length), Height (_Height)
  {}
  / * Setting function Length field
   * _Length value transmitted
   * as a parameter
   * /
  Void SetLength (int _Length) {length = _LENGTH; }
  / * Installation function function HEIGHT field
   * _Height value transmitted
   * as a parameter
   * /
  Void setHeight (int _height) {height = _height; }
  / * Functions of obtaining field values,
   * Returning Length or Height
   * respectively
   * /
  INT GETLENGTH () {Return Length; }
  int getHeigth () {Return Height; }
  / * Function, calculating the figure of the figure.
   * Returns the value obtained by multiplication
   * Length and Height field values ​​on each other
   * /
  INT SQARE () {Return Length * Height; }
};
Int Main (int argc, char * argv [])
{
  Rectangle R (); // The default designer is called.
  STD :: COUT & LT; & LT; r.getlength (); & lt; & lt; "" & lt; & lt; R.GetHeigth () & lt; & lt; STD :: ENDL; // "0 0"
  R.SetLength (2); // Set the length equal to 2
  R.setHeight (3); // Install the height equal
  STD :: COUT & LT; & LT; r.getlength (); & lt; & lt; "" & lt; & lt; R.GetHeigth () & lt; & lt; STD :: ENDL; // "2 3"
  STD :: COUT & LT; & LT; R.SQARE () & lt; & lt; STD :: ENDL; // Returns Square: "6"
  Rectangle R1 (5, 4); // Constructor is called with parameters
  STD :: COUT & LT; & LT; R1.getLength (); & lt; & lt; "" & lt; & lt; R1.GetHeigth () & lt; & lt; STD :: ENDL; // "5 4"
  STD :: COUT & LT; & LT; R1.SQARE () & lt; & lt; STD :: ENDL; // Returns Square: "20"
}

In the description of any designer, for example

rectangle ()
  : Length (0), Height (0)
{}

string: Length (0), Height (0) is an initialization list.
Such an entry is equivalent to the following

rectangle ()
{
  Length = 0;
  Height = 0;
}

p.s. There may be small blots, because The code did not compile

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions