Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Declare a class named Rectangle . Rectangle will have two private data fields

ID: 3543542 • Letter: 1

Question

1.      Declare a class named Rectangle. Rectangle will have two private data fields:

                                                                                          height

int width;                                        

int height;

                                                                                                         


The width can range in value from 0

            

                                                                                          height

    

             Declare a class named Rectangle. Rectangle will have two private data fields: Implement the default constructor to initialize the width and height to 1. Implement the parametrized constructor Implement setWidth/setHeight methods Implement all get methods.

Explanation / Answer

#include<iostream>

using namespace std;


class Rectangle{

private:

int height, width;

public:

Rectangle(){

height = 1;

width = 1;

}

Rectangle(int w, int h){

height = h;

width = w;

}

int getHeight(){

return height;

}

bool setHeight(int h){

if(h < 0 || h > 24) return false;

height = h;

return true;

}


int getWidth(){

return width;

}

bool setWidth(int w){

if(w < 0 || w > 80) return false;

width = w;

return true;

}

int getArea(){

return height * width;

}

int getPerimeter(){

return 2 * (height + width);

}

};


int main(){

Rectangle obj2;

int x = obj2.getHeight();

obj2.setHeight(x);

int y = obj2.getHeight();

obj2.setHeight(y);

int area = obj2.getArea();

int perimeter = obj2.getPerimeter();

cout << "After default constructor, height = "<< obj2.getHeight() << ", width = " << obj2.getWidth() << " area = " << area << " perimeter = " << perimeter << endl << endl;


Rectangle obj1(1, 2);

x = obj1.getHeight();

obj1.setHeight(x);

y = obj1.getHeight();

obj1.setHeight(y);

area = obj1.getArea();

perimeter = obj1.getPerimeter();

cout << "After parameterized constructor, height = "<< obj1.getHeight() << ", width = " << obj1.getWidth() << " area = " << area << " perimeter = " << perimeter << endl << endl;


return 0;

}