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

Assume you’re implementing class Rectangle. A Rectangle in 2D is described by th

ID: 3755039 • Letter: A

Question

Assume you’re implementing class Rectangle.  A Rectangle in 2D is described by the x, y position of its upper lower left corner along with a width and height.  Width and height may only be positive real numbers.  Given this description, provide an implementation for setWidthand setHeight.  In particular, if the new value for width or height violates the positive real number requirement, throw an exception.

class Rectangle {

private:

int xPos;

int yPos;

int height;

int width;

public:

//all other functionality

//provide an implementation of the following two member functions

//and throw exceptions where appropriate.

void setWidth(int newWidth);

void setHeight(int newHeight);

};

3.  Taking into consideration the setWidth and setHeight member functions from question 2, rewrite the code below to properly use a try…catch block to handle any thrown exceptions. [10]

            Rectangle r;

            r. setWidth(-10);

            r.setHeight(20);

Explanation / Answer

If you have any doubts, please give me comment...

#include<iostream>

using namespace std;

class Rectangle

{

private:

int xPos;

int yPos;

int height;

int width;

public:

//all other functionality

//provide an implementation of the following two member functions

//and throw exceptions where appropriate.

void setWidth(int newWidth){

try{

if(newWidth<0){

throw 1;

}

width = newWidth;

}catch(int x){

cout<<"new width must be a real nummber"<<endl;

}

}

void setHeight(int newHeight){

try{

if(newHeight<0){

throw 1;

}

height = newHeight;

}catch(int x){

cout<<"new height must be a real nummber"<<endl;

}

}

};

int main(){

Rectangle r;

r.setWidth(-10);

r.setHeight(20);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote