//C++ program to use 'this' pointer to return an object of a class from a member function of the class
#include <iostream>
using namespace std;
class Distance
{
int m,cm;
public:
void getDistance();
Distance addDistance(Distance);
void displaysum();
};
void Distance::getDistance()
{
cout<<"Enter m and cm: ";
cin>>m>>cm;
}
Distance Distance::addDistance(Distance d2)
{
cm=cm+d2.cm;
if(cm>=100)
{
cm=cm-100;
m=m+d2.m+1;
}
else
{
m=m+d2.m;
}
return *this;
}
void Distance::displaysum()
{
cout<<"The sum is "<<m<<"m "<<cm<<"cm";
}
int main()
{
Distance d1,d2,d3;
d1.getDistance();
d2.getDistance();
d3=d1.addDistance(d2);
d3.displaysum();
return 0;
}
#include <iostream>
using namespace std;
class Distance
{
int m,cm;
public:
void getDistance();
Distance addDistance(Distance);
void displaysum();
};
void Distance::getDistance()
{
cout<<"Enter m and cm: ";
cin>>m>>cm;
}
Distance Distance::addDistance(Distance d2)
{
cm=cm+d2.cm;
if(cm>=100)
{
cm=cm-100;
m=m+d2.m+1;
}
else
{
m=m+d2.m;
}
return *this;
}
void Distance::displaysum()
{
cout<<"The sum is "<<m<<"m "<<cm<<"cm";
}
int main()
{
Distance d1,d2,d3;
d1.getDistance();
d2.getDistance();
d3=d1.addDistance(d2);
d3.displaysum();
return 0;
}
No comments:
Post a Comment