C++ program to pass OBJECT as argument

//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
//finally display result object in main() function

#include <iostream>
using namespace std;
class Distance
{
    int meter,cm;
    public:
        void read();
        void add(Distance,Distance);
        void display();
};
void Distance::read()
{
    cout<<"Enter meter and cm: ";
    cin>>meter>>cm;
}
void Distance::add(Distance d1,Distance d2)
{
    cm=d1.cm+d2.cm;
    if(cm>=100)
    {
        cm=cm-100;
        meter=1;
    }
    meter=meter+d1.meter+d2.meter;
}
void Distance::display()
{
    cout<<"The sum is "<<meter<<" meter "<<cm<<" cm";
}
int main()
{
    Distance d1,d2,d3;
    d1.read();
    d2.read();
    d3.add(d1,d2);
    d3.display();
    return 0;
}

No comments:

Post a Comment