Question

Chapter 14 starting out with c++ from control structures through objects

9. Feet Inches Modification Modify the FeetInches class discussed in this chapter so it overloads the following operators: Demonstrate the classs capabilities in a simple program

0 0
Add a comment Improve this question Transcribed image text
Answer #1


source code:

#include<iostream>
using namespace std;
//clas dist
class dist
{
private:
int feet;
float inches;
  
public:
dist():feet(0),inches(0){}
  
dist(int f, float in): feet(f),inches(in){}
  
void getdist()
{
cout<<"feet=";
cin>>feet;
cout<<"inches=";
cin>>inches;
}
  
friend bool operator <=(dist d1, dist d2);// <= operator overloading
friend bool operator >=(dist d1, dist d2);// >=operator overloading
friend bool operator !=(dist d1, dist d2);// != operator overloading
  
};

bool operator <=(dist d1,dist d2)
{

float dd1=d1.feet+d1.inches/12;
float dd2=d2.feet+d2.inches/12;
return dd1<=dd2?true:false;
}

bool operator >=(dist d1,dist d2)
{

float dd1=d1.feet+d1.inches/12;
float dd2=d2.feet+d2.inches/12;
return dd1>=dd2?true:false;
}

bool operator !=(dist d1,dist d2)
{

float dd1=d1.feet+d1.inches/12;
float dd2=d2.feet+d2.inches/12;
return dd1!=dd2?true:false;
}


int main()
{
dist d1(7,6.56);
dist d2;
d2.getdist();
if(d1<=d2)
cout<<"d1 is smaller than d2"<<endl;
if(d1>=d2)
cout<<"d1 is greater than d2"<<endl;
if(d1!=d2)
cout<<"both d1 and d2 are not equal"<<endl;
return 0;
}

output:

sh-4.25 gt+ -o main *.cpp sh-4.25 main feet-2 inches-g d1 is greater than d2 both di and d2 are not equal sh-4.2$

Add a comment
Know the answer?
Add Answer to:
Chapter 14 starting out with c++ from control structures through objects Feet Inches Modification Modify the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT