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

C++ Please read the entire question before writing the code. Make sure you outpu

ID: 3806082 • Letter: C

Question

C++

Please read the entire question before writing the code. Make sure you output matches the example provided

Objectives :
1. Handling exceptions within a program
2. Using try/catch block
3. Creating exception class
Question :
When setting the dimensions of a geometric object, the lengths of the sides should be positive values. Write a program that repeatedly asks for the dimensions of a rectangle and displays the area. Before an error is presented, collected both the length and the width values in a new class called Rectangle using following functions:
void Rectangle::setWidth(int width);
void Rectangle::setLength(int length);

Create another class DimError , that is used by the Rectangle class. If either setWidth or setLength is called with a negative value, it throws a DimError that includes a message indicating which method was called and the reason for the error.

Additional methods required for the Rectangle class are:
A default constructor with optional length and width parameters
Rectangle::Rectangle(int len, int wid);
A method to calculate the area of the Rectangle
int Rectangle::area();

Required methods for the DimError are:
A parameterized constructor that accepts the “what” of the error.
DimError::DimError(string m);
A method to return “what” the error is.
string DimError::what();

Download lab17.cpp to get the driver function.

Output :

Enter a length: 6

Enter a width: 3

The area is: 18

Enter a length: 12

Enter a width: -3

Couldn’t set the rectangle’s dimensions:

setWidth called a negative width

Exiting…

Press any key to continue…

################################### Lab17.cpp(driver) ################################

#include<iostream>
#include<string>
using namespace std;

class DimError {
private:
string message;
public:
//implement the methods
};

class Rectangle {
private:
int length, width;
public:
//implement the methods
};

int main(void) {
Rectangle r;
try {
  while (true) {
   int l, w;
   cout << "Enter a length: ";
   cin >> l;
   cout << "Enter a width: ";
   cin >> w;
   r.setLength(l);
   r.setWidth(w);
   cout << "The area is: " << r.area() << endl;
  }
} catch (DimError de) {
  cout << "Couldn't set the rectangle's dimensions: " << endl
    << de.what() << endl;
}
cout << "Exiting..." << endl;
return 0;
}

Explanation / Answer

#include<iostream>
#include<string>
using namespace std;
class DimError {
private:
string message;
public:
//implement the methods
DimError(string s) {
message = s;
}
string what() {
return message;
}
};
class Rectangle {
private:
int length, width;
public:
//implement the methods
Rectangle() {

}
Rectangle(int len, int wid) {

setLength(len);
setWidth(wid);
}
void setLength(int l){
if(l < 0){
throw DimError("setLength called a negative width");
}
length = l;
}
void setWidth(int w){
if(w < 0){
throw DimError("setWidth called a negative width");
}
width = w;
}
int area() {
return width * length;
}
};

int main(void) {
Rectangle r;
try {
while (true) {
int l, w;
cout << "Enter a length: ";
cin >> l;
cout << "Enter a width: ";
cin >> w;
r.setLength(l);
r.setWidth(w);
cout << "The area is: " << r.area() << endl;
}
} catch (DimError de) {
cout << "Couldn't set the rectangle's dimensions: " << endl
<< de.what() << endl;
}
cout << "Exiting..." << endl;
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter a length: 6                                                                                                                                                                                                                                                        

Enter a width: 3                                                                                                                                                                                                                                                         

The area is: 18                                                                                                                                                                                                                                                          

Enter a length: 12                                                                                                                                                                                                                                                       

Enter a width: -3                                                                                                                                                                                                                                                        

Couldn't set the rectangle's dimensions:                                                                                                                                                                                                                                 

setWidth called a negative width                                                                                                                                                                                                                                         

Exiting...

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