Details of this Paper

Lab8 Ex2. Object Conversion and Aggregation - fr. Program Charllenge Chap14-12. LandTract Class

Description

solution


Question

Question;Lab8 Ex2. Object Conversion and Aggregation - fr. Program Charllenge Chap14-12. LandTract ClassMake 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 #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 using namespace std,class FeetInches, // Forward Declaration// Function Prototypes for Overloaded Stream Operatorsostream &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 feetint inches, // To hold a number of inchesvoid simplify(), // Defined in FeetInches.cpppublic:// ConstructorFeetInches(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 (distance),inches = (distance - feet) * 12, }// Copy constructorFeetInches(FeetInches &right){ feet = right.feet,inches = right.inches, }// Mutator functionsvoid setFeet(int f){ feet = f, }void setInches(int i){ inches = i,simplify(), }// Multiply functionFeetInches multiply(FeetInches obj){ FeetInches temp,temp.feet = feet * obj.feet,temp.inches = inches * obj.inches,temp.simplify(),return temp, }// Accessor functionsint getFeet() const{ return feet, }int getInches() const{ return inches, }// Overloaded operator functionsFeetInches 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 &),bool operator != (const FeetInches &),// Conversion functions// Friendsfriend ostream &operator (istream &&, FeetInches &);#endifFeetInches.cpp// Implementation file for the FeetInches class#include // 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 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,elsestatus = 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,elsestatus = 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,elsestatus = 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 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 obj.feet,// Prompt the user for the inches.cout 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,elsestatus = 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,elsestatus = 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,elsestatus = true,return status,}

 

Paper#36832 | Written in 18-Jul-2015

Price : $47
SiteLock