Saturday 2 February 2013

passing objects as parameters in c++

//DISTANCE TRAVELLED PER HOUR
#include<iostream.h>
#include<conio.h>
class travel
{
private:
int km,hr;
public:

travel()    //constructor    used to avoid garbage value picking problems
{
km=hr=0;
}

void set()
{
cout<<"enter km travelled  ";
cin>>km;
cout<<"enter hours  ";
cin>>hr;
}

void show()
{
cout<<"\n\n you travelled "<<km<<" kilometer "<<" in "<<hr<<" hours\n\n";
}

void total(travel p)      //value of t2 is tranferring to the object p
{
travel temp;
temp.km=km+p.km;       //km is of the object t1 ie calling object
temp.hr=hr+p.hr;
cout<<"\n\nyou travelled "<<temp.km<<" kilometers in "<<temp.hr<<" hours";
}
};

void main()
{
travel t1,t2;
t1.set();
t1.show();
t2.set();
t2.show();
t1.total(t2);
getch();
}





0 comments:

Post a Comment