Question

In: Computer Science

In need of assistance in C++ code: Design and implement class Rectangle to represent a rectangle...

In need of assistance in C++ code:

Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods:

  1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor.
  2. A non-argument constructor method to create a default rectangle.
  3. Another constructor method to create a rectangle with user-specified height and width.
  4. Method getArea() that returns the area.
  5. Method getPerimeter() that returns the perimeter.
  6. Method getHeight() that returns the height.
  7. Method getWidth() that returns the width.

Now design and implement a test program to create two rectangle objects: one with default height and width, and the second is 5 units high and 6 units wide. Next, test the class methods on each object to print the information as shown below.

Sample run:

First object:

Height:     1 unit

Width:      1 unit

Area:       1 unit

Perimeter: 4 units

Second object:

Height:     5 unit

Width:      6 unit

Area:       30 units

Perimeter: 22 units

Thanks in advance!

Solutions

Expert Solution

#include <iostream>
using namespace std;


class Rectangle {
public:
// instance variables
double height;
double width;

// default constructor
Rectangle() { // need () for contructors
height = 1.0;
width = 1.0;
}

// ovarloaded constructor
Rectangle(double l, double w) {
height = l;
width = w;
}

// accessor methods/getters
double getHeight() {
return height;
}

double getWidth() {
return width;
}

// mutator/setter methods
void setHeight(double h) {
height= h;
}

void setWidth(double width) {
width = width;
}

double getArea() {
double area = height * width;
return area;
}

double getPerimeter() {
double per;
double length2 = height * 2;
double width2 = width * 2;
per = length2 + width2;
return per;
}


};
int main() {
double l,w;
cout<<"Enter height and length: ";
cin>>l;
cin>>w;
Rectangle *r1 = new Rectangle();
   cout<<"First Object:\n";
   cout<<"Height: "<<r1->getHeight()<<" unit(s)"<<endl;
   cout<<"Width: "<<r1->getWidth()<<" unit(s)"<<endl;
cout<<"Area : "<<r1->getArea()<<"unit(s)"<<endl;
cout<<"Perimeter : "<<r1->getPerimeter()<<"unit(s)"<<endl;
Rectangle *r2 = new Rectangle(l, w);
   cout<<"Second Object:\n";
   cout<<"Height: "<<r2->getHeight()<<" unit(s)"<<endl;
   cout<<"Width: "<<r2->getWidth()<<" unit(s)"<<endl;
cout<<"Area : "<<r2->getArea()<<"unit(s)"<<endl;
cout<<"Perimeter : "<<r2->getPerimeter()<<"unit(s)"<<endl;
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area...
: Design and implement class Radio to represent a radio object. The class defines the following...
: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. A private variable of type int named station to represent a station number. Set to A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio on or off. Set to...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT