//class Distance with m and cm as members
#include <iostream>
using namespace std;
class Distance
{
int m,cm;
public:
void read()
{
cout<<"Enter m and cm: ";
cin>>m>>cm;
}
void display()
{
cout<<m<<"m "<<cm<<"cm"<<endl;
}
Distance operator+(Distance d) //member function
{
Distance dist;
dist.m=m+d.m;
dist.cm=cm+d.cm;
if(dist.cm>=100)
{
dist.cm-=100;
dist.m++;
}
return dist;
}
};
int main()
{
Distance d1,d2,d3;
d1.read();
d2.read();
d3=d1+d2; //same as d1.operator+(d2);
d1.display();
d2.display();
d3.display();
#include <iostream>
using namespace std;
class Distance
{
int m,cm;
public:
void read()
{
cout<<"Enter m and cm: ";
cin>>m>>cm;
}
void display()
{
cout<<m<<"m "<<cm<<"cm"<<endl;
}
Distance operator+(Distance d) //member function
{
Distance dist;
dist.m=m+d.m;
dist.cm=cm+d.cm;
if(dist.cm>=100)
{
dist.cm-=100;
dist.m++;
}
return dist;
}
};
int main()
{
Distance d1,d2,d3;
d1.read();
d2.read();
d3=d1+d2; //same as d1.operator+(d2);
d1.display();
d2.display();
d3.display();
No comments:
Post a Comment