Lab8 Ex2. Object Conversion and Aggregation – fr. Program Charllenge Chap14-12. LandTract Class
Make a LandTract class that is composed of two FeetInches objects, one for the tract s length and one for the width. The class should have a member function that returns the tract s area. Demonstrate the class in a program that asks the user to enter the dimensions for two tracts of land. The program should display the area of each tract of land and indicate whether the tracts are of equal size.
To calculate the area, the FeetInches objects have to be converted into double (in feet). You will need to impletement the object conversion inside the FeetInches class to double in order to compute the area in square feet.
FeetInches::operator double()
The final output of this exercise shall display the area in square feet, and drop the fraction.
Starter: lab8_ex2.zip (lab8_ex2.cpp, FeetInches.h, FeetInches.cpp)
You need to add the FeetInches object conversion to double in FeetInches class.
Submit: lab8_ex2.zip (lab8_ex2.cpp, FeetInches.h, FeetInches.cpp, LandTract.h, LandTract.cpp)
lab8_ex2.cpp
// Lab8 Ex2 Aggregation fr. Ch14-12: LandTract Class
#include <iostream>
#include “FeetInches.cpp”
#include “LandTract.cpp”
using namespace std;
int main()
{
// Create two FeetInches objects for width
// and length.
FeetInches width(125, 6);
FeetInches length(275, 0);
// Create a LandTract Class.
LandTract land(width, length);
// Display the area.
cout << “The area of the tract is “
<< land.getArea() << endl;
return 0;
}
FeetInches.h,
// Specification file for the FeetInches class
#ifndef FEETINCHES_H
#define FEETINCHES_H
#include <iostream>
using namespace std;
class FeetInches; // Forward Declaration
// Function Prototypes for Overloaded Stream Operators
ostream &operator << (ostream &, const FeetInches &);
istream &operator >> (istream &, FeetInches &);
// The FeetInches class holds distances or measurements
// expressed in feet and inches.
class FeetInches
{
private:
int feet; // To hold a number of feet
int inches; // To hold a number of inches
void simplify(); // Defined in FeetInches.cpp
public:
// Constructor
FeetInches(int f = 0, int i = 0)
{ feet = f;
inches = i;
simplify(); }
// New Constructor added for this assignment.
// This constructor takes double argument and
// converts it to feet and inches. Example:
// 2.5 would be converted to 2 feet 6 inches.
FeetInches(double distance)
{ feet = static_cast<int>(distance);
inches = (distance – feet) * 12; }
// Copy constructor
FeetInches(FeetInches &right)
{ feet = right.feet;
inches = right.inches; }
// Mutator functions
void setFeet(int f)
{ feet = f; }
void setInches(int i)
{ inches = i;
simplify(); }
// Multiply function
FeetInches multiply(FeetInches obj)
{ FeetInches temp;
temp.feet = feet * obj.feet;
temp.inches = inches * obj.inches;
temp.simplify();
return temp; }
// Accessor functions
int getFeet() const
{ return feet; }
int getInches() const
{ return inches; }
// Overloaded operator functions
FeetInches operator + (const FeetInches &);
FeetInches operator – (const FeetInches &);
FeetInches operator ++ (); // Prefix ++
FeetInches operator ++ (int); // Postfix ++
bool operator > (const FeetInches &);
bool operator < (const FeetInches &);
bool operator == (const FeetInches &);
// New operators
bool operator >= (const FeetInches &);
bool operator <= (const FeetInches &);
bool operator != (const FeetInches &);
// Conversion functions
// Friends
friend ostream &operator << (ostream &, const FeetInches &);
friend istream &operator >> (istream &, FeetInches &);
};
#endif
FeetInches.cpp
// Implementation file for the FeetInches class
#include <cstdlib> // Needed for abs()
#include “FeetInches.h”
//************************************************************
// Definition of member function simplify. This function *
// checks for values in the inches member greater than *
// twelve or less than zero. If such a value is found, *
// the numbers in feet and inches are adjusted to conform *
// to a standard feet & inches expression. For example, *
// 3 feet 14 inches would be adjusted to 4 feet 2 inches and *
// 5 feet -2 inches would be adjusted to 4 feet 10 inches. *
//************************************************************
void FeetInches::simplify()
{
if (inches >= 12)
{
feet += (inches / 12);
inches = inches % 12;
}
else if (inches < 0)
{
feet -= ((abs(inches) / 12) + 1);
inches = 12 – (abs(inches) % 12);
}
}
//**********************************************
// Overloaded binary + operator. *
//**********************************************
FeetInches FeetInches::operator + (const FeetInches &right)
{
FeetInches temp;
temp.inches = inches + right.inches;
temp.feet = feet + right.feet;
temp.simplify();
return temp;
}
//**********************************************
// Overloaded binary – operator. *
//**********************************************
FeetInches FeetInches::operator – (const FeetInches &right)
{
FeetInches temp;
temp.inches = inches – right.inches;
temp.feet = feet – right.feet;
temp.simplify();
return temp;
}
//*************************************************************
// Overloaded prefix ++ operator. Causes the inches member to *
// be incremented. Returns the incremented object. *
//*************************************************************
FeetInches FeetInches::operator ++ ()
{
++inches;
simplify();
return *this;
}
//***************************************************************
// Overloaded postfix ++ operator. Causes the inches member to *
// be incremented. Returns the value of the object before the *
// increment. *
//***************************************************************
FeetInches FeetInches::operator ++ (int)
{
FeetInches temp(feet, inches);
inches++;
simplify();
return temp;
}
//************************************************************
// Overloaded > operator. Returns true if the current object *
// is set to a value greater than that of right. *
//************************************************************
bool FeetInches::operator > (const FeetInches &right)
{
bool status;
if (feet > right.feet)
status = true;
else if (feet == right.feet && inches > right.inches)
status = true;
else
status = false;
return status;
}
//************************************************************
// Overloaded < operator. Returns true if the current object *
// is set to a value less than that of right. *
//************************************************************
bool FeetInches::operator < (const FeetInches &right)
{
bool status;
if (feet < right.feet)
status = true;
else if (feet == right.feet && inches < right.inches)
status = true;
else
status = false;
return status;
}
//*************************************************************
// Overloaded == operator. Returns true if the current object *
// is set to a value equal to that of right. *
//*************************************************************
bool FeetInches::operator == (const FeetInches &right)
{
bool status;
if (feet == right.feet && inches == right.inches)
status = true;
else
status = false;
return status;
}
//********************************************************
// Overloaded << operator. Gives cout the ability to *
// directly display FeetInches objects. *
//********************************************************
ostream &operator<<(ostream &strm, const FeetInches &obj)
{
strm << obj.feet << ” feet, ” << obj.inches << ” inches”;
return strm;
}
//********************************************************
// Overloaded >> operator. Gives cin the ability to *
// store user input directly into FeetInches objects. *
//********************************************************
istream &operator >> (istream &strm, FeetInches &obj)
{
// Prompt the user for the feet.
cout << “Feet: “;
strm >> obj.feet;
// Prompt the user for the inches.
cout << “Inches: “;
strm >> obj.inches;
// Normalize the values.
obj.simplify();
return strm;
}
//*************************************************************
// Conversion function to convert a FeetInches object *
// to a double. *
//*************************************************************
// FeetInches::operator double()
//*************************************************************
// Conversion function to convert a FeetInches object *
// to an int. *
//*************************************************************
// FeetInches:: operator int()
// New operators
//******************************************************
// Overloaded >= operator. Returns true if the current *
// object is set to a value greater than or equal to *
// that of right. *
//******************************************************
bool FeetInches::operator >= (const FeetInches &right)
{
bool status;
if ((*this > right) || (*this == right))
status = true;
else
status = false;
return status;
}
//******************************************************
// Overloaded <= operator. Returns true if the current *
// object is set to a value less than or equal to *
// that of right. *
//******************************************************
bool FeetInches::operator <= (const FeetInches &right)
{
bool status;
if ((*this < right) || (*this == right))
status = true;
else
status = false;
return status;
}
//******************************************************
// Overloaded != operator. Returns true if the current *
// object is set to a value not equal to that of *
// right. *
//******************************************************
bool FeetInches::operator != (const FeetInches &right)
{
bool status;
if (*this == right)
status = false;
else
status = true;
return status;
}