//class Distance with meter and cm as private data members
//read two Objects of the class
//add them by passing these two objects to a function member
//return the result
//finally display result object in main() function
#include <iostream>
using namespace std;
class Distance
{
int meter,cm;
public:
void read();
Distance add(Distance);
void display();
};
void Distance::read()
{
cout<<"Enter meter and cm: ";
cin>>meter>>cm;
}
Distance Distance::add(Distance d2)
{
Distance sum;
sum.cm=cm+d2.cm;
if(sum.cm>=100)
{
sum.cm=sum.cm-100;
sum.meter=1;
}
sum.meter=sum.meter+meter+d2.meter;
return sum;
}
void Distance::display()
{
cout<<"The sum is "<<meter<<" meter "<<cm<<" cm";
}
int main()
{
Distance d1,d2,d3;
d1.read();
d2.read();
d3=d1.add(d2);
d3.display();
return 0;
}
//read two Objects of the class
//add them by passing these two objects to a function member
//return the result
//finally display result object in main() function
#include <iostream>
using namespace std;
class Distance
{
int meter,cm;
public:
void read();
Distance add(Distance);
void display();
};
void Distance::read()
{
cout<<"Enter meter and cm: ";
cin>>meter>>cm;
}
Distance Distance::add(Distance d2)
{
Distance sum;
sum.cm=cm+d2.cm;
if(sum.cm>=100)
{
sum.cm=sum.cm-100;
sum.meter=1;
}
sum.meter=sum.meter+meter+d2.meter;
return sum;
}
void Distance::display()
{
cout<<"The sum is "<<meter<<" meter "<<cm<<" cm";
}
int main()
{
Distance d1,d2,d3;
d1.read();
d2.read();
d3=d1.add(d2);
d3.display();
return 0;
}
No comments:
Post a Comment