C++ Main.cpp______________________________ #include <iostream> #include <iomanip
ID: 3708257 • Letter: C
Question
C++
Main.cpp______________________________
#include <iostream>
#include <iomanip>
#include "rectangleType.h"
using namespace std;
int main()
{
rectangleType oldYard(20.00, 10.00);
rectangleType newYard;
cout << fixed << showpoint << setprecision(2);
cout << "Line 10: Area of oldYard = "<< oldYard.area() << endl;
newYard = oldYard.doubleDimensions();
cout << "Line 12: Area of newYard = " << newYard.area() << endl;
//Incrementing oldYard using ++ operator overloading
cout<<"Incrementing oldYard rectangle object : ";
oldYard++;
cout<<"Rectangle details after incrementing : ";
//Print rectangle description using << operator overloading
cout<<oldYard;
cout << " Area of oldYard after incrementing= "<< oldYard.area() << endl;
//compare rectangles using == operator overloading
if(oldYard==newYard)
cout<<"Two rectangles are the same ";
else
cout<<"Two rectangles are not the same ";
system("pause");
return 0;
}
rectangleType.cpp_________________________
#include "rectangleType.h"
//Default constructor
rectangleType::rectangleType()
{
}
//Parameerized consgtructor
rectangleType::rectangleType(double _length,double _width)
{
length = _length;
width = _width;
}
//Set Dimention function
void rectangleType::setDimension(double _length,double _width)
{
length = _length;
width = _width;
}
//get length function
double rectangleType::getLength()
{
return length;
}
//get width function
double rectangleType::getWidth()
{
return width;
}
// area function
double rectangleType::area()
{
double area;
area = length*width;
return area;
}
// perimeter function
double rectangleType::perimeter()
{
double perimeter;
perimeter = 2*(length+width);
return perimeter;
}
// doubleDimensions function
rectangleType rectangleType::doubleDimensions()
{
length = 2 * length;
width = 2 * width;
return *this;
}
//print function
void rectangleType::print()
{
cout<<"Rectanlge Details: ";
cout << "Length : " << length << " Width : " << width;
cout << "Area : " << area() << " Perimeter : " << perimeter()<<endl;
}
//++ operator overloading
rectangleType rectangleType::operator++()
{
length++;
width++;
return *this;
}
//-- operator overloading
rectangleType rectangleType::operator--()
{
length--;
width--;
return *this;
}
//- operator overloading
rectangleType rectangleType::operator-(rectangleType& other)
{
if(length<other.getLength() || width<other.getWidth()) {
cout<<"Do not perform the operation";
return *this;
}
else
{
length = length - other.getLength();
width = width - other.getWidth();
return *this;
}
}
//== operator overloading
bool rectangleType::operator==(rectangleType& other)
{
if(area()==other.area())
return true;
else
return false;
}
//!= operator overloading
bool rectangleType::operator!=(rectangleType& other)
{
if(area()!=other.area())
return true;
else
return false;
}
//> operator overloading
bool rectangleType::operator>(rectangleType& other)
{
if(area()>other.area())
return true;
else
return false;
}
//< operator overloading
bool rectangleType::operator<(rectangleType& other)
{
if(area()<other.area())
return true;
else
return false;
}
//Input and Output operator overloading
ostream & operator<<( ostream &output, const rectangleType &r )
{
output<<"Rectanlge Details: ";
output << "Length : " << r.length << " Width : " << r.width;
return output;
}
istream &operator>>( istream &input, rectangleType &r )
{
input >> r.length >>r.width;
return input;
}
rectangleType.h_____________________
#ifndef RECTANGLETYPE_H
#define RECTANGLETYPE_H
#include<iostream>
using namespace std;
#include "rectangleType.h"
class rectangleType
{
public:
rectangleType();
rectangleType(double _length,double _width);
void setDimension(double _length,double _width);
double getLength();
double getWidth();
double area();
double perimeter();
void print();
rectangleType doubleDimensions();
//All operator overloading functions declaration
rectangleType operator++();
rectangleType operator--();
rectangleType operator-(rectangleType& other);
bool operator==(rectangleType& other);
bool operator!=(rectangleType& other);
bool operator>(rectangleType& other);
bool operator<(rectangleType& other);
friend ostream& operator<<( ostream &output, const rectangleType &r );
friend istream &operator>>( istream &input, rectangleType &r );
private:
double length;
double width;
};
#endif // RECTANGLETYPE_H
Programming Exercise (13-1) Due Tomorrow at 1 1 PM HST main.cpp rectangleType.cpprectangleType.h Back Overloading Operators 1 #include 3 #include "rectangleType.h 4 using namespace std 5 int main) Instructions Test Results This chapter uses the class rectangleType to illustrate how to overload the operators+ 3/6 passed = ‘ !s, ->, and se . In this exercise, 7 rectangleType oldYard (2.00, 10.00); 8 rectangleType newYard Code Pattern first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts 1 to 3. coutExplanation / Answer
#include <iostream>
#include <iomanip>
#include "rectangleType.h"
using namespace std;
int main()
{
rectangleType oldYard(20.00, 10.00);
rectangleType newYard;
cout << fixed << showpoint << setprecision(2);
cout << "Line 10: Area of oldYard = "<< oldYard.area() << endl;
newYard = oldYard.doubleDimensions();
cout << "Line 12: Area of newYard = " << newYard.area() << endl;
//Incrementing oldYard using ++ operator overloading
cout<<"Incrementing oldYard rectangle object : ";
oldYard++;
cout<<"Rectangle details after incrementing : ";
//Print rectangle description using << operator overloading
cout<<oldYard;
cout << " Area of oldYard after incrementing= "<< oldYard.area() << endl;
//compare rectangles using == operator overloading
if(oldYard==newYard)
cout<<"Two rectangles are the same ";
else
cout<<"Two rectangles are not the same ";
system("pause");
return 0;
}
rectangleType.cpp_________________________
#include "rectangleType.h"
//Default constructor
rectangleType::rectangleType()
{
}
//Parameerized consgtructor
rectangleType::rectangleType(double _length,double _width)
{
length = _length;
width = _width;
}
//Set Dimention function
void rectangleType::setDimension(double _length,double _width)
{
length = _length;
width = _width;
}
//get length function
double rectangleType::getLength()
{
return length;
}
//get width function
double rectangleType::getWidth()
{
return width;
}
// area function
double rectangleType::area()
{
double area;
area = length*width;
return area;
}
// perimeter function
double rectangleType::perimeter()
{
double perimeter;
perimeter = 2*(length+width);
return perimeter;
}
// doubleDimensions function
rectangleType rectangleType::doubleDimensions()
{
length = 2 * length;
width = 2 * width;
return *this;
}
//print function
void rectangleType::print()
{
cout<<"Rectanlge Details: ";
cout << "Length : " << length << " Width : " << width;
cout << "Area : " << area() << " Perimeter : " << perimeter()<<endl;
}
//++ operator overloading
rectangleType rectangleType::operator++()
{
length++;
width++;
return *this;
}
//-- operator overloading
rectangleType rectangleType::operator--()
{
length--;
width--;
return *this;
}
//- operator overloading
rectangleType rectangleType::operator-(rectangleType& other)
{
if(length<other.getLength() || width<other.getWidth()) {
cout<<"Do not perform the operation";
return *this;
}
else
{
length = length - other.getLength();
width = width - other.getWidth();
return *this;
}
}
//== operator overloading
bool rectangleType::operator==(rectangleType& other)
{
if(area()==other.area())
return true;
else
return false;
}
//!= operator overloading
bool rectangleType::operator!=(rectangleType& other)
{
if(area()!=other.area())
return true;
else
return false;
}
//> operator overloading
bool rectangleType::operator>(rectangleType& other)
{
if(area()>other.area())
return true;
else
return false;
}
//< operator overloading
bool rectangleType::operator<(rectangleType& other)
{
if(area()<other.area())
return true;
else
return false;
}
//Input and Output operator overloading
ostream & operator<<( ostream &output, const rectangleType &r )
{
output<<"Rectanlge Details: ";
output << "Length : " << r.length << " Width : " << r.width;
return output;
}
istream &operator>>( istream &input, rectangleType &r )
{
input >> r.length >>r.width;
return input;
}
#ifndef RECTANGLETYPE_H
#define RECTANGLETYPE_H
#include<iostream>
using namespace std;
#include "rectangleType.h"
class rectangleType
{
public:
rectangleType();
rectangleType(double _length,double _width);
void setDimension(double _length,double _width);
double getLength();
double getWidth();
double area();
double perimeter();
void print();
rectangleType doubleDimensions();
//All operator overloading functions declaration
rectangleType operator++();
rectangleType operator--();
rectangleType operator-(rectangleType& other);
bool operator==(rectangleType& other);
bool operator!=(rectangleType& other);
bool operator>(rectangleType& other);
bool operator<(rectangleType& other);
friend ostream& operator<<( ostream &output, const rectangleType &r );
friend istream &operator>>( istream &input, rectangleType &r );
private:
double length;
double width;
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.