26.8.15

Create a 'DISTANCE' class

Create a 'DISTANCE' class with :

-     feet and inches as data members

-     member function to input distance

-     member function to output distance

-     member function to add two distance objects

Write a main function to create objects of DISTANCE class. Input two distances and output the sum.

#include<iostream.h>

#include<conio.h>

class distance

{
int feet,inch;
public:
void getdistance(); void putdistance();

void adddistance(distance d1,distance d2);
};

void distance::getdistance()

{

cout<<"\nEnter the feet : "; cin>>feet;
cout<<"\nEnter the inches :"; cin>>inch;
}

void distance::putdistance()
{
cout<<"\n\nfeet = "<<feet; cout<<"\tinch = "<< inch;

}
void distance::adddistance(distance d1,distance d2)
{
inch=d1.inch+d2.inch;
feet=inch/12;
inch=inch%12;
feet=feet+d1.feet+d2.feet;
}

int main()

{

distance d1,d2,d3; clrscr();

cout<< "\nDistance 1 \n"; d1.getdistance();

cout<< "\nDistance 2 \n"; d2.getdistance(); d3.adddistance(d1,d2);
cout<<"\nThe Sum Of two distance is : "; d3.putdistance();
getch();

return(0);
}


*********************************OUTPUT****************************

Distance 1

Enter the feet : 13

Enter the inches :11

Distance 2

Enter the feet : 11

Enter the inches :11

The Sum Of two distance is :

feet = 25   inch = 10

No comments:

Post a Comment

Comment Here