- #1
yungman
- 5,755
- 293
This is a program from Gaddis that has two exception classes inside the Rect class. It is not clear for me how it works. Ivor book declares the exception classes as individual class, not inside another class. So I cannot compare and see what I missed in Gaddis book. Here is the program right out of the Gaddis book:
I have 3 main questions:
1) Look at line 26 and 31. throw NegativeWidth(w) and throw NegativeLength(len). These are Constructors of the two classes, not object of NegativeWidth and NegativeLength . Are we supposed to throw OBJECT only?
2) Also, look at line 49 and 53. They declared object by (Rectangle::NegativeWidth e) and (Rectangle::NegativeLength e). But the program already declared Rectangle myRect. Why not use the NegativeWidth in myRect instead instead of starting a new object using (Rectangle::NegativeWidth e)
3) In Ivor book, it would put class NegativeWidth and class NegativeLength as derived class of Rectangle. This makes things so much simpler. Is what Gaddis way of declaring class inside another class a good way to do things? I just want to confirm because you guys had criticized Gaddis' sample programs in his later chapters already.
Thanks
C++:
#include <iostream>
using namespace std;
class Rectangle
{
private: double width, length;
public:
class NegativeWidth//throw class object if w = -ve
{ private: int value;
public:
NegativeWidth(int val) { value = val; }
int getValue() const { return value; }
};
class NegativeLength//throw class object if len = -ve
{ private: int value;
public:
NegativeLength(int val) {value = val; }
int getValue() const { return value; }
};
Rectangle()
{ width = 0.0;
length = 0.0;
}
void setWidth(double w)
{
if (w >= 0) width = w;
else throw NegativeWidth(w);//How is this an object?
}
void setLength(double len)
{
if (len >= 0)length = len;
else throw NegativeLength(len);//How is this an object?
}
double getWidth() const { return width;}
double getLength() const{ return length;}
double getArea() const { return width * length;}
};
int main()
{ int width, length;
Rectangle myRect;
cout << " Enter the rectangle's width: ";
cin >> width;
cout << "\n Enter the rectangle's length: ";
cin >> length;
try
{ myRect.setWidth(width);
myRect.setLength(length);
cout<<"The area of the rectangle is "<<myRect.getArea()<<"\n\n";
}
catch (Rectangle::NegativeWidth e)//why not using myRect that is already defined?
{//why not using myRect that is already defined?
cout<<"Error: "<<e.getValue()<<" is an invalid width.\n\n";
}
catch (Rectangle::NegativeLength e)
{
cout<<"Error: " << e.getValue()<<" is an invalid length.\n\n";
}
cout << "End of the program.\n";
return 0;
}
I have 3 main questions:
1) Look at line 26 and 31. throw NegativeWidth(w) and throw NegativeLength(len). These are Constructors of the two classes, not object of NegativeWidth and NegativeLength . Are we supposed to throw OBJECT only?
2) Also, look at line 49 and 53. They declared object by (Rectangle::NegativeWidth e) and (Rectangle::NegativeLength e). But the program already declared Rectangle myRect. Why not use the NegativeWidth in myRect instead instead of starting a new object using (Rectangle::NegativeWidth e)
3) In Ivor book, it would put class NegativeWidth and class NegativeLength as derived class of Rectangle. This makes things so much simpler. Is what Gaddis way of declaring class inside another class a good way to do things? I just want to confirm because you guys had criticized Gaddis' sample programs in his later chapters already.
Thanks
Last edited: