Home Uncategorized Write a main function to create objects of DISTANCE class. Input two...

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

SHARE

AIM: Write a c++ main function to create objects of distance class then input two distances and output the sum.

THEORY:

Develop a class to measure distance as feet (should be int), inches (should be float). Include member functions to set and get attributes. Include constructors. Develop functions to add two distances.

SOURCE CODE:

#include<iostream>
using namespace std;
class dist
{
   public:
   int feet,inch,x,y,z;
   void input()
   {
      cout<<“enter feet and inches:”<<“\n”;
      cin>>feet>>inch;
   }
   void show()
   {
      cout<<“The distance is “;
      cout<<feet<<” feet “<<inch<<” inch\n”;
   }
   void sum(dist x,dist y)
   {
      feet=x.feet+y.feet;
      inch=x.inch+y.inch;
      if(inch>=12)
      {
         feet=feet+1;
         inch=inch-12;
      }
   }
};
int main()
{
   dist x,y,z;
   x.input();
   y.input();
   z.sum(x,y);
   z.show();
}

OUTPUT:
Enter feet and inches:
6     3
Enter feet and inches:
3     11
The distance is: 10 feet and 2 inches

Back to Programs.