Question
Create LandTract Class that utilize the FeetInches class created in the GitHub starter file (written below) and write the following application. Make a LandTract class
Create LandTract Class that utilize the FeetInches class created in the GitHub starter file (written below) and write the following application.
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.
Sample Test Run:
Running /home/ubuntu/workspace/comsc200/Week06/testLandTract.cpp _______ LandTract Class Test Menu __________ m - menu n - new LandTract d - display the LandTract attributes q - Quit Enter your choice: m _______ LandTract Class Test Menu __________ m - menu n - new LandTract d - display the LandTract attributes q - Quit Enter your choice: n Enter Tract Width in Feet, then Inches: Enter Feet: 5 Enter Inches: 6 Enter Tract Length in Feet, then Inches: Enter Feet: 7 Enter Inches: 8 Enter your choice: d the width is 5 feet, 6 inches the length is 7 feet, 8 inches the area is 42 feet, 0 inches Enter your choice: m _______ LandTract Class Test Menu __________ m - menu n - new LandTract d - display the LandTract attributes q - Quit Enter your choice: q Program exit!
Submit:
Files
- appLandTract.cpp
- LandTract.h
- FeetInches.h
The Github starter file :
1. TestLand.cpp
#include | |
#include | |
#include "FeetInches.h" | |
#include "LandTract.h" | |
using namespace std; | |
void menu(); | |
int main() | |
{ | |
bool stay = true; // Boolean to stay in this menu | |
string choice, word, text; // User input for choices and input | |
// int feet, inches; | |
FeetInches width, length; | |
LandTract myProperty; | |
auto menu = [] ( ) { | |
cout << "_______ LandTract Class Test Menu __________ " | |
<< " m - menu " | |
<< " n - new LandTract " | |
<< " d - display the LandTract attributes " | |
<< " q - Quit "; | |
}; | |
menu(); | |
while(stay) { // Main menu while starts | |
cout << " Enter your choice: "; | |
cin >> choice; // Take in user choice from menu | |
cin.ignore(); | |
char ch = choice[0]; | |
switch(ch) { // menu switch start | |
case 'n': | |
case 'N': // new LandTract | |
{ | |
cout << " Enter Tract Width in Feet, then Inches: "; | |
// fill the blank for inputting width | |
cout << " Enter Tract Length in Feet, then Inches: "; | |
// fill the blank for inputting length | |
cout << endl; | |
break; | |
} | |
case 'd': | |
case 'D': // Display this LandTract | |
{ | |
// Display the area. | |
cout << " the width is " << myProperty.getWidth() | |
<< " the length is " << myProperty.getLength() | |
<< " the area is " << myProperty.getArea() | |
<< " "; | |
break; | |
} | |
case 'm': case 'M': | |
menu(); break; | |
case 'q': case 'Q': // quit | |
stay = false; | |
break; | |
default: // Invalid! Tells user to try again | |
cout << " Invalid command! Try again! "; | |
} | |
} | |
cout << " Program exit!"; | |
} |
2. FeetInches.h
#ifndef FEETINCHES_H | |
#define FEETINCHES_H | |
#include | |
#include | |
using namespace std; | |
// (Friend step 1) Forward Declaration | |
class FeetInches; | |
// (Friend step 2) Forward Declaration of Friend Function | |
ostream &operator << (ostream &, const FeetInches &); | |
istream &operator >> (istream &, FeetInches &); | |
class FeetInches { | |
int feet; // To hold a number of feet | |
int inches; // To hold a number of inches | |
void simplify();// header only Definition below | |
public: | |
// argument constructor | |
FeetInches(int f = 0, int i = 0) | |
{ feet = f; inches = i; simplify(); } | |
// Added definition outside | |
FeetInches(string s); | |
// copy constructor - must 1. pass ref var. | |
FeetInches (const FeetInches &fi) | |
{ feet = fi.feet; inches = fi.inches; simplify(); } | |
// assignment operator | |
FeetInches operator= (const FeetInches &right) | |
{ feet = right.feet; inches = right.inches; simplify(); } | |
// Mutator | |
void setFeet(int f) { feet = f; } | |
void setInches(int i) { inches = i; simplify(); } | |
// Accessor | |
int getFeet() const { return feet; } | |
int getInches() const { return inches; } | |
// Overloaded operator functions | |
FeetInches operator + (const FeetInches &); // Overloaded + | |
FeetInches operator - (const FeetInches &); // Overloaded - | |
FeetInches operator ++ (); // Prefix ++ | |
FeetInches operator ++ (int); // Postfix ++ | |
bool operator > (const FeetInches &); // Overloaded > | |
bool operator < (const FeetInches &); // Overloaded < | |
bool operator == (const FeetInches &); // Overloaded == | |
// Object Type Converter declaration | |
operator int() { return feet; } | |
operator double() { | |
double temp = feet; temp += (inches / 12.0); return temp; } | |
// (Friends Step 3) Friend Function Declaration | |
friend ostream &operator << (ostream &, const FeetInches &); | |
friend istream &operator >> (istream &, FeetInches &); | |
}; | |
void FeetInches::simplify(){ | |
if (inches >= 12) { | |
feet += (inches / 12); | |
inches = inches % 12; } | |
else if (inches < 0) { | |
feet -= ((abs(inches) / 12) + 1); // quotion for feets | |
inches = 12 - (abs(inches) % 12); } // remainder for inches | |
if( feet < 0 && inches >0 ) { feet += 1; inches -= 12; } | |
} | |
FeetInches FeetInches::operator + (const FeetInches &right) { | |
FeetInches temp; | |
temp.inches = inches + right.inches; | |
temp.feet = feet + right.feet; | |
temp.simplify(); | |
return temp; | |
} | |
ostream &operator << (ostream &strm, const FeetInches &rhs) { | |
strm << rhs.feet << "\' " << rhs.inches << "\" "; | |
return strm; | |
} | |
istream &operator >> (istream &strm, FeetInches &rhs) { | |
char c; // 3'5" | |
strm >> rhs.feet; | |
strm >> c; | |
strm >> rhs.inches; | |
rhs.simplify(); | |
return strm; | |
} | |
// string argument Constructor | |
FeetInches::FeetInches(string s) { | |
stringstream ss(s); | |
ss >> feet; | |
char peekchar = ss.peek(); | |
if( ss && peekchar == '\'' ) { | |
ss.get(); | |
ss >> inches; | |
} | |
} | |
#endif |
3. LantdTrac.h (data member only)
class LandTract { | |
private: | |
FeetInches width, length; | |
// private helper if necessary | |
public: | |
// bare minimum compiler scilencer, modify accordingly | |
FeetInches getWidth() { return width; } | |
FeetInches getLength() { return length; } | |
FeetInches getArea() { return 0; } | |
// all the rest of necessary public member methods, go here | |
}; |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started