27 Consider the following class. class FeetInches public: int feet, To hold a nu
ID: 3843505 • Letter: 2
Question
27 Consider the following class. class FeetInches public: int feet, To hold a number of feet int inches ll To hold a number of inches a) Inside the class, add a function for overloading addi- tion (H) operator. This function will create a new ob- whose data members will be the sum of this and parameter object of the same class (e.g., feet mem- ber of new object will be the sum of the feet of this object and feet of parameter object. Similarly, inches member of new object will be the sum of inches of this object and inches of parameter object. The func- tion will return the new object.Explanation / Answer
#include<conio.h>
#include<iostream.h>
class FeetInches
{
public:int feet,inches;
public:FeetInches(){}
public:FeetInches(int ft,int inch)
{
feet=ft;
inches=inch;
}
public:void displayDistance()
{
cout << "Feet : " << feet << " Inches:" << inches <<endl;
}
public: FeetInches operator+(const FeetInches& fi)
{
FeetInches ft_in;
ft_in.feet = this->feet + fi.feet;
ft_in.inches = this->inches + fi.inches;
return ft_in;
}
public:friend bool operator ==(FeetInches &ob1, FeetInches &ob2)
{
if(ob1.feet == ob2.feet)
{
if(ob1.inches == ob2.inches)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public :friend ostream & operator << (ostream &dout, FeetInches &ob3)
{
dout<<"Data of OBJECT3"<<endl;
dout<<"Feet :"<<ob3.feet<<endl;
dout<<"Inches :"<<ob3.inches<<endl;
return(dout);
}
};
void main()
{
FeetInches fi1(2,3);
FeetInches fi2(4,5);
FeetInches fi3;
fi1.displayDistance();
fi2.displayDistance();
fi3=fi1+fi2;
fi3.displayDistance ();
if(fi1 == fi2)
{
cout<<"They are equal"<<endl<<endl;
}
else
{
cout<<" They are Unequal"<<endl<<endl;
}
cout<<endl;
cout<<fi3;
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.