In: Computer Science
For some reason I am having a hard time getting this program to compile properly. Could you help me debug it? Write the prototypes and functions to overload the given operators in the code
//main.cpp
//This program shows how to use the class rectangleType.
#include <iostream>
#include "rectangleType.h"
using namespace std;
int main() {
rectangleType rectangle1(23,
45);
//Line 1
rectangleType rectangle2(12,
10);
//Line 2
rectangleType
rectangle3;
//Line 3
rectangleType
rectangle4;
//Line 4
cout << "Line 5: rectangle1:
";
//Line 5
rectangle1.print();
//Line 6
cout <<
endl;
//Line 7
cout << "Line 8: rectangle2:
";
//Line 8
rectangle2.print();
//Line 9
cout <<
endl;
//Line 10
rectangle3 = rectangle1 +
rectangle2;
//Line 11
cout << "Line 12: rectangle3:
";
//Line 12
rectangle3.print();
//Line 13
cout <<
endl;
//Line 14
rectangle4 = rectangle1 *
rectangle2;
//Line 15
cout << "Line 16: rectangle4:
";
//Line 16
//print rectangl through << stream
extraction operator; //Line 17
cout << rectangle4;
cout <<
endl;
//Line 18
if (rectangle1 ==
rectangle2)
//Line 19
cout << "Line 20:
rectangle1 and "
<< "rectangle2 are equal." <<
endl; //Line
20
else
//Line 21
cout << "Line 22:
rectangle1 and "
<< "rectangle2 are not equal."
<<
endl;
//Line 22
return 0;
}
//rectangleType.h
#include <iostream>
using namespace std;
class rectangleType {
//Overload the stream insertion and extraction operators
public:
void setDimension(double l, double w);
double getLength() const;
double getWidth() const;
double area() const;
double perimeter() const;
void print() const;
//Overload the operator +
//Overload the operator *
//Overload the operator ==
//Overload the operator !=
rectangleType();
rectangleType(double l, double w);
private:
double length;
double width;
};
//rectangeType.cpp
#include "rectangleType.h"
void rectangleType::setDimension(double l, double w)
{
if (l >= 0)
length = l;
else
length = 0;
if (w >= 0)
width = w;
else
width = 0;
}
double rectangleType::getLength() const
{
return length;
}
double rectangleType::getWidth()const
{
return width;
}
double rectangleType::area() const
{
return length * width;
}
double rectangleType::perimeter() const
{
return 2 * (length + width);
}
void rectangleType::print() const
{
cout << "Length = " << length
<< "; Width
= " << width;
}
rectangleType::rectangleType(double l, double w)
{
setDimension(l, w);
}
rectangleType::rectangleType()
{
length = 0;
width = 0;
}
rectangleType rectangleType::operator+ (const rectangleType&
rectangle) const
{
//to-do:Task 1
}
rectangleType rectangleType::operator* (const rectangleType&
rectangle) const
{
//to do: Task 2
}
bool rectangleType::operator== (const rectangleType&
rectangle) const
{
//to-do: Task 3
}
bool rectangleType::operator!= (const rectangleType&
rectangle) const
{
//to-do: Task 4
}
ostream& operator<< (ostream& osObject, const
rectangleType& rectangle)
{
//to-do: Task 5
}
Program Code [C++]
rectangleType.h
#include <iostream>
using namespace std;
class rectangleType {
//Overload the stream insertion and extraction operators
friend ostream& operator<<(ostream& osObject, const rectangleType& rectangle);
public:
void setDimension(double l, double w);
double getLength() const;
double getWidth() const;
double area() const;
double perimeter() const;
void print() const;
// Writing prototypes for overloading given
operators in statement
//Overload the operator +
rectangleType operator+(const rectangleType&
rectangle) const;
//Overload the operator *
rectangleType operator*(const rectangleType& rectangle) const;
//Overload the operator ==
bool operator==(const rectangleType& rectangle) const;
//Overload the operator !=
bool operator!=(const rectangleType& rectangle) const;
rectangleType();
rectangleType(double l, double w);
private:
double length;
double width;
};
rectangleType.cpp
#include "rectangleType.h"
void rectangleType::setDimension(double l, double w)
{
if (l >= 0)
length = l;
else
length = 0;
if (w >= 0)
width = w;
else
width = 0;
}
double rectangleType::getLength() const
{
return length;
}
double rectangleType::getWidth()const
{
return width;
}
double rectangleType::area() const
{
return length * width;
}
double rectangleType::perimeter() const
{
return 2 * (length + width);
}
void rectangleType::print() const
{
cout << "Length = " << length
<< "; Width = " << width;
}
rectangleType::rectangleType(double l, double w)
{
setDimension(l, w);
}
rectangleType::rectangleType()
{
length = 0;
width = 0;
}
rectangleType rectangleType::operator+ (const rectangleType&
rectangle) const
{
rectangleType rec;
rec.length = this->length + rectangle.length;
rec.width = this->width + rectangle.width;
return rec;
}
rectangleType rectangleType::operator* (const rectangleType&
rectangle) const
{
rectangleType rec;
rec.length = this->length * rectangle.length;
rec.width = this->width * rectangle.width;
return rec;
}
bool rectangleType::operator== (const rectangleType&
rectangle) const
{
return ((this->length == rectangle.length)
&& (this->width == rectangle.width));
}
bool rectangleType::operator!= (const rectangleType&
rectangle) const
{
return ((this->length != rectangle.length) || (this->width !=
rectangle.width));
}
ostream& operator<< (ostream& osObject, const
rectangleType& rectangle)
{
osObject << "Length = " <<
rectangle.getLength() << "; Width = " <<
rectangle.getWidth();
}
main.cpp
//This program shows how to use the class rectangleType.
#include <iostream>
#include "rectangleType.h"
using namespace std;
int main() {
rectangleType rectangle1(23, 45); //Line 1
rectangleType rectangle2(12, 10); //Line 2
rectangleType rectangle3; //Line 3
rectangleType rectangle4; //Line 4
cout << "Line 5: rectangle1: "; //Line 5
rectangle1.print(); //Line 6
cout << endl; //Line 7
cout << "Line 8: rectangle2: "; //Line 8
rectangle2.print(); //Line 9
cout << endl; //Line 10
rectangle3 = rectangle1 + rectangle2; //Line 11
cout << "Line 12: rectangle3: "; //Line 12
rectangle3.print(); //Line 13
cout << endl; //Line 14
rectangle4 = rectangle1 * rectangle2; //Line 15
cout << "Line 16: rectangle4: "; //Line 16
//print rectangl through << stream extraction operator;
//Line 17
cout << rectangle4;
cout << endl; //Line 18
if (rectangle1 == rectangle2) //Line 19
cout << "Line 20: rectangle1 and "
<< "rectangle2 are equal." << endl; //Line 20
else //Line 21
cout << "Line 22: rectangle1 and "
<< "rectangle2 are not equal."
<< endl; //Line 22
return 0;
}
Sample Output:-
----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!