Write a program that defines a class named Rectangle. The Rectangle class should
ID: 3670943 • Letter: W
Question
Write a program that defines a class named Rectangle. The Rectangle class should contain the following: variables to hold the length and width of the rectangle instance constructors to allow a Rectangle to be declared in two ways: o without parameters - length & width will be set to zero o with 2 integer parameters - length and width accessor and mutator functions for length and width There should be a driver program that tests all functions of the class, including the two constructors. Your program should be contained in an Empty Project named RectangleClass. You will create 3 separate files: rectangle.h (class definition), rectangle.cpp (class functions implementations) and rectangle_driver.cpp (program with the main() function to test all functions of the class.
I'm having trouble writing my C++ program and understanding it.
Explanation / Answer
main.cpp
// This program demonstrates Rectangle class exceptions.
#include <iostream>
#include "Rectangle.h"
using namespace std;
int main()
{
int width;
int length;
// Create a Rectangle object.
Rectangle myRectangle;
// Get the width and length.
cout << "Enter the rectangle's width: ";
cin >> width;
cout << "Enter the rectangle's length: ";
cin >> length;
// Store these values in the Rectangle object.
try
{
myRectangle.setWidth(width);
myRectangle.setLength(length);
cout << "The area of the rectangle is "
<< myRectangle.getArea() << endl;
}
catch (Rectangle::NegativeSize)
{
cout << "Error: A negative value was entered. ";
}
cout << "End of the program. ";
return 0;
}
Rectangle.h
// Specification file for the Rectangle class
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width; // The rectangle's width
double length; // The rectangle's length
public:
// Exception class
class NegativeSize
{ }; // Empty class declaration
// Default constructor
Rectangle()
{ width = 0.0; length = 0.0; }
// Mutator functions, defined in Rectangle.cpp
void setWidth(double);
void setLength(double);
// Accessor functions
double getWidth() const
{ return width; }
double getLength() const
{ return length; }
double getArea() const
{ return width * length; }
};
#endif
Rectangle.cpp
// Implementation file for the Rectangle class.
#include "Rectangle.h"
//***********************************************************
// setWidth sets the value of the member variable width. *
//***********************************************************
void Rectangle::setWidth(double w)
{
if (w >= 0)
width = w;
else
throw NegativeSize();
}
//***********************************************************
// setLength sets the value of the member variable length. *
//***********************************************************
void Rectangle::setLength(double len)
{
if (len >= 0)
length = len;
else
throw NegativeSize();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.