Sunday 29 March 2015

Passing Objects by Value

#Include <iostream.h>
class distance
{
float feet;
float inches;
public:
void init(float ft, float in)
{
feet = ft;
inches = in;
}
void read(void)
{
cout<<”Enter Feet”;cin>>feet;
cout<<”Enter inches”;cin>>inches;
}
void show(void)
{
cout<<feet<<”-“<<inches<<endl;
}
void add(distance d1, distance d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
if(inches > 12.0)
{
feet = feet + 1.0;
inches = inches – 12.0;
}
}
};
void main()
{
distance d1,d2,d3;
d2.init(11.0,6.25);
d1.read();
cout<<”d1 = “;
d1.show();
cout<<”d2=”;
d2.show();
d3.add(d1,d2);
cout<<”d3 = d1 + d2”;
d3.show();
}

No comments:

Post a Comment