// 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 ==
// Friends
friend ostream &operator << (ostream &, const FeetInches &);
friend istream &operator >> (istream &, FeetInches &);
//===============================================//
// overloaded operators to be added Q3.a)
bool operator >= (const FeetInches &);
bool operator <= (const FeetInches &);
bool operator != (const FeetInches &);
// Copy constructor Q3.b)
FeetInches(FeetInches &right)
{
feet = right.feet;
inches = right.inches;
}
};
#endif
// Implementation file for the FeetInches class
#include <cstdlib> // Needed for abs()
//************************************************************
// 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 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;
}
//================================================//
// operators to be added Q3.a)
bool FeetInches::operator >= (const FeetInches &right)
{
bool status;
if ((*this > right) || (*this == right))
status = true;
else
status = false;
return status;
}
bool FeetInches::operator <= (const FeetInches &right)
{
bool status;
if ((*this < right) || (*this == right))
status = true;
else
status = false;
return status;
}
bool FeetInches::operator != (const FeetInches &right)
{
bool status;
if (*this == right)
status = false;
else
status = true;
return status;
}
// Q3.c)
ostream &operator<<(ostream &strm, const FeetInches &obj)
{
strm << obj.feet << " feet and " << obj.inches << " inches";
return strm;
}
int main()
{
FeetInches first, second;
cout<< "Enter a distance in feet and inches"<<endl;
cin>>first;
cout<<"Enter another distance in feet and inches\n";
cin>>second;
// test Q3.a) overloaded operators
if (second<= first){printf("<= \n");}
// test Q3.b) copy constructor
FeetInches third(second);
//test Q3.c) overloaded ostream
cout << third << endl ;
// test Q3.b) copy constructor
FeetInches box_length(10,2);
//test Q3.c) overloaded ostream
cout << box_length << endl ;
return 0;
}
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}