SlideShare a Scribd company logo
1 of 10
C++ Linux
Need help with lab assignment. Instructions and code is posted below (FeetInches.cpp,
FeetInches.h, main.cpp)
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() {
double temp = feet;
temp += (inches / 12.0);
return temp;
}
//*************************************************************
// Conversion function to convert a FeetInches object *
// to an int. *
//*************************************************************
FeetInches::operator int() {
return feet;
}
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();
}
// Mutator functions
void setFeet(int f) {
feet = f;
}
void setInches(int i) {
inches = i;
simplify();
}
// Accessor functions
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 ==
// Conversion functions
operator double();
operator int();
// Friends
friend ostream& operator <<(ostream&, const FeetInches&);
friend istream& operator >>(istream&, FeetInches&);
};
#endif
Main.cpp
// This program demonstrates the the FeetInches class's
// conversion functions.
#include <iostream>
#include "FeetInches.h"
using namespace std;
int main() {
double d; // To hold double input
int i; // To hold int input
// Define a FeetInches object.
FeetInches distance;
// Get a distance from the user.
cout << "Enter a distance in feet and inches:n";
cin >> distance;
// Convert the distance object to a double.
d = distance;
// Convert the distance object to an int.
i = distance;
// Display the values.
cout << "The value " << distance;
cout << " is equivalent to " << d << " feetn";
cout << "or " << i << " feet, rounded down.n";
return 0;
}
FeetInches Modification. Download the source code of Chapter 14 from code archive at
MyClasses, and modify the FeetInches class so it overloads the following operators: <= >= ! =
Demonstrate the class's capabilities in a simple program.

More Related Content

Similar to C++ Linux Need help with lab assignment- Instructions and code is post.docx

(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf
f3apparelsonline
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
ssuser454af01
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
stn_tkiller
 
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdfpublic void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
isenbergwarne4100
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
armyshoes
 
c++ Lecture 3
c++ Lecture 3c++ Lecture 3
c++ Lecture 3
sajidpk92
 

Similar to C++ Linux Need help with lab assignment- Instructions and code is post.docx (20)

C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
Chaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscoreChaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscore
 
(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
 
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdfpublic void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
 
Arduino section programming slides
Arduino section programming slidesArduino section programming slides
Arduino section programming slides
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Maintaining sanity in a large redux app
Maintaining sanity in a large redux appMaintaining sanity in a large redux app
Maintaining sanity in a large redux app
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for PerformanceWeb Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performance
 
Chaining et composition de fonctions avec lodash / underscore
Chaining et composition de fonctions avec lodash / underscoreChaining et composition de fonctions avec lodash / underscore
Chaining et composition de fonctions avec lodash / underscore
 
Workshop Swift
Workshop Swift Workshop Swift
Workshop Swift
 
ES6 - Level up your JavaScript Skills
ES6 - Level up your JavaScript SkillsES6 - Level up your JavaScript Skills
ES6 - Level up your JavaScript Skills
 
c++ Lecture 3
c++ Lecture 3c++ Lecture 3
c++ Lecture 3
 
Abus at a glance
Abus at a glanceAbus at a glance
Abus at a glance
 

More from RichardjOZTerryp

below is some of the work i began on LispEvaluate on java- but i'm hav.docx
below is some of the work i began on LispEvaluate on java- but i'm hav.docxbelow is some of the work i began on LispEvaluate on java- but i'm hav.docx
below is some of the work i began on LispEvaluate on java- but i'm hav.docx
RichardjOZTerryp
 

More from RichardjOZTerryp (20)

c- Explain several methods for how zoonotic disease-causing microbes c.docx
c- Explain several methods for how zoonotic disease-causing microbes c.docxc- Explain several methods for how zoonotic disease-causing microbes c.docx
c- Explain several methods for how zoonotic disease-causing microbes c.docx
 
C++ 5-6-2 Basic derived class member override Define a member function.docx
C++ 5-6-2 Basic derived class member override Define a member function.docxC++ 5-6-2 Basic derived class member override Define a member function.docx
C++ 5-6-2 Basic derived class member override Define a member function.docx
 
By now everyone has heard news reports about balloons in the sky- Were.docx
By now everyone has heard news reports about balloons in the sky- Were.docxBy now everyone has heard news reports about balloons in the sky- Were.docx
By now everyone has heard news reports about balloons in the sky- Were.docx
 
Businesses that are overleveraged carry a large amount of debt and are.docx
Businesses that are overleveraged carry a large amount of debt and are.docxBusinesses that are overleveraged carry a large amount of debt and are.docx
Businesses that are overleveraged carry a large amount of debt and are.docx
 
Business requirements refine the Logical design phase from a functiona.docx
Business requirements refine the Logical design phase from a functiona.docxBusiness requirements refine the Logical design phase from a functiona.docx
Business requirements refine the Logical design phase from a functiona.docx
 
Briefly explain the following as applied to database design- What is D.docx
Briefly explain the following as applied to database design- What is D.docxBriefly explain the following as applied to database design- What is D.docx
Briefly explain the following as applied to database design- What is D.docx
 
bottles of wine every day- The daily production in Italy is either 200.docx
bottles of wine every day- The daily production in Italy is either 200.docxbottles of wine every day- The daily production in Italy is either 200.docx
bottles of wine every day- The daily production in Italy is either 200.docx
 
Bones Structure and Function Lab Identify the following structures in.docx
Bones Structure and Function Lab Identify the following structures in.docxBones Structure and Function Lab Identify the following structures in.docx
Bones Structure and Function Lab Identify the following structures in.docx
 
Bone and Structure Lab1-) Identify types of bones based upon their sha.docx
Bone and Structure Lab1-) Identify types of bones based upon their sha.docxBone and Structure Lab1-) Identify types of bones based upon their sha.docx
Bone and Structure Lab1-) Identify types of bones based upon their sha.docx
 
bond issue-What will be the total interest payments over the five-year.docx
bond issue-What will be the total interest payments over the five-year.docxbond issue-What will be the total interest payments over the five-year.docx
bond issue-What will be the total interest payments over the five-year.docx
 
Below is the Tycho crater on the surface of the Moon- The left image p.docx
Below is the Tycho crater on the surface of the Moon- The left image p.docxBelow is the Tycho crater on the surface of the Moon- The left image p.docx
Below is the Tycho crater on the surface of the Moon- The left image p.docx
 
Below b a graph showing alinear regression of Ser genotypes and dorsa.docx
Below b a graph showing alinear regression of  Ser genotypes and dorsa.docxBelow b a graph showing alinear regression of  Ser genotypes and dorsa.docx
Below b a graph showing alinear regression of Ser genotypes and dorsa.docx
 
Below is an impact crater on the surface of the Earth- This is the Met.docx
Below is an impact crater on the surface of the Earth- This is the Met.docxBelow is an impact crater on the surface of the Earth- This is the Met.docx
Below is an impact crater on the surface of the Earth- This is the Met.docx
 
Being a taker can be potentially beneficial because- Select one- A- th.docx
Being a taker can be potentially beneficial because- Select one- A- th.docxBeing a taker can be potentially beneficial because- Select one- A- th.docx
Being a taker can be potentially beneficial because- Select one- A- th.docx
 
below is some of the work i began on LispEvaluate on java- but i'm hav.docx
below is some of the work i began on LispEvaluate on java- but i'm hav.docxbelow is some of the work i began on LispEvaluate on java- but i'm hav.docx
below is some of the work i began on LispEvaluate on java- but i'm hav.docx
 
Below is an idealized diagram of several oceanic plates and a continen.docx
Below is an idealized diagram of several oceanic plates and a continen.docxBelow is an idealized diagram of several oceanic plates and a continen.docx
Below is an idealized diagram of several oceanic plates and a continen.docx
 
Begin on the left side of the map and trace over 30oN latitude with yo.docx
Begin on the left side of the map and trace over 30oN latitude with yo.docxBegin on the left side of the map and trace over 30oN latitude with yo.docx
Begin on the left side of the map and trace over 30oN latitude with yo.docx
 
Before June production is recorded- the Work in Process inventory acco.docx
Before June production is recorded- the Work in Process inventory acco.docxBefore June production is recorded- the Work in Process inventory acco.docx
Before June production is recorded- the Work in Process inventory acco.docx
 
Because human wants are insatiable and unlimited while available resou.docx
Because human wants are insatiable and unlimited while available resou.docxBecause human wants are insatiable and unlimited while available resou.docx
Because human wants are insatiable and unlimited while available resou.docx
 
Cash budgets are based on cash accounting rather than accrual accounti.docx
Cash budgets are based on cash accounting rather than accrual accounti.docxCash budgets are based on cash accounting rather than accrual accounti.docx
Cash budgets are based on cash accounting rather than accrual accounti.docx
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

C++ Linux Need help with lab assignment- Instructions and code is post.docx

  • 1. C++ Linux Need help with lab assignment. Instructions and code is posted below (FeetInches.cpp, FeetInches.h, main.cpp) 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); }
  • 2. } //********************************************** // 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 *
  • 3. // 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) {
  • 4. 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; }
  • 5. //************************************************************* // 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. *
  • 6. //******************************************************** 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() { double temp = feet; temp += (inches / 12.0); return temp; } //************************************************************* // Conversion function to convert a FeetInches object *
  • 7. // to an int. * //************************************************************* FeetInches::operator int() { return feet; } 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
  • 8. public: // Constructor FeetInches(int f = 0, int i = 0) { feet = f; inches = i; simplify(); } // Mutator functions void setFeet(int f) { feet = f; } void setInches(int i) { inches = i; simplify(); } // Accessor functions int getFeet() const { return feet; } int getInches() const { return inches; } // Overloaded operator functions
  • 9. 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 == // Conversion functions operator double(); operator int(); // Friends friend ostream& operator <<(ostream&, const FeetInches&); friend istream& operator >>(istream&, FeetInches&); }; #endif Main.cpp // This program demonstrates the the FeetInches class's // conversion functions. #include <iostream> #include "FeetInches.h" using namespace std; int main() { double d; // To hold double input
  • 10. int i; // To hold int input // Define a FeetInches object. FeetInches distance; // Get a distance from the user. cout << "Enter a distance in feet and inches:n"; cin >> distance; // Convert the distance object to a double. d = distance; // Convert the distance object to an int. i = distance; // Display the values. cout << "The value " << distance; cout << " is equivalent to " << d << " feetn"; cout << "or " << i << " feet, rounded down.n"; return 0; } FeetInches Modification. Download the source code of Chapter 14 from code archive at MyClasses, and modify the FeetInches class so it overloads the following operators: <= >= ! = Demonstrate the class's capabilities in a simple program.