Answer to Question #213904 in C++ for Maryam

Question #213904

let's we have a class named Distance having two private data members such as feet(integer),

inches(float), one input function to input values to the data members, one Display function to

show the distance. The distance 5 feet and 6.4 inches should be displayed as 5’- 6.4”. Then

add the two objects of Distance class and then display the result (the + operator should be

overloaded). You should also take care of inches if it's more than 12 then the inches should

be decremented by 12 and feet to be incremented by 1.


1
Expert's answer
2021-07-05T07:40:18-0400
#include<iostream>
using namespace std;
class Distance{
	private:
		
	int feet;
	float inches;
	public:
		void input(){
			cout<<"Enter feet of the distace"<<endl;
			cin>>feet;
			cout<<"Enter inches of the distace"<<endl;
			cin>>inches;
			
		}
		void display(){
			cout<<"The distance entered is\t"<<feet<<"'"<<"-"<<"\""<<inches<<endl;
			
		}
		Distance operator +(Distance &d){
			Distance dist;
			dist.feet = feet + d.feet;
         dist.inches= inches + d.inches;
         if(dist.inches>12){
         	dist.inches -=  12;
         	dist.feet += 1; 
		 }
		 return dist;
			
		}
		
		
	
};
int main(){
	Distance di;
	di.input();
	di.display();
	Distance d1;
	d1.input();
	d1.display();
	Distance d3;
	d3 =  di + d1;
	d3.display();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS