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.
#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();
}
Comments
Leave a comment