Sorce Code (distance.cpp)
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inch;
public:
void input()
{
cout<<"Enter FEET : ";
cin>>feet;
cout<<"Enter INCH : ";
cin>>inch;
}
void show()
{
cout<<feet<<" FEET and "<<inch<<" INCH"<<endl;
}
void add(Distance d1,Distance d2)
{
inch=d1.inch+d2.inch;
feet=inch/12;
feet+=d1.feet+d2.feet;
inch=inch%12;
}
};
int main()
{
clrscr();
Distance d1;
cout<<"Distance d1"<<endl<<"---------------"<<endl; d1.input();
Distance d2;
cout<<"Distance d2"<<endl<<"---------------"<<endl;
d2.input();
d1.add(d1,d2);
cout<<endl<<"The sum of d1 and d2 is : ";
d1.show();
getch();
return 0;
}
Output
Distance d1
---------------
Enter FEET : 5
Enter INCH : 6
Distance d2
---------------
Enter FEET : 6
Enter INCH : 7
The sum of d1 and d2 is : 12 FEET and 1 INCH
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inch;
public:
void input()
{
cout<<"Enter FEET : ";
cin>>feet;
cout<<"Enter INCH : ";
cin>>inch;
}
void show()
{
cout<<feet<<" FEET and "<<inch<<" INCH"<<endl;
}
void add(Distance d1,Distance d2)
{
inch=d1.inch+d2.inch;
feet=inch/12;
feet+=d1.feet+d2.feet;
inch=inch%12;
}
};
int main()
{
clrscr();
Distance d1;
cout<<"Distance d1"<<endl<<"---------------"<<endl; d1.input();
Distance d2;
cout<<"Distance d2"<<endl<<"---------------"<<endl;
d2.input();
d1.add(d1,d2);
cout<<endl<<"The sum of d1 and d2 is : ";
d1.show();
getch();
return 0;
}
Output
Distance d1
---------------
Enter FEET : 5
Enter INCH : 6
Distance d2
---------------
Enter FEET : 6
Enter INCH : 7
The sum of d1 and d2 is : 12 FEET and 1 INCH
No comments:
Post a Comment