Programming

Create Two Classes DM and DB to Stores the Value of Distances

Create two classes DM and DB which stores the value of distances. DM stores distances in meter and centimeters and DB in feet and inches. WAP that can read values for the class objects and add one object DM with another object DB. Use the friend function to carry out the addition.

Source Code:

#include<iostream.h>
#include<conio.h>
class DB;
class DM
{
               int m,cm;
   public:
               void get_data()
      {
cout<<"\nEnter meter value : ";
              cin>>m;
cout<<"\nEnter centimeter value: ";
               cin>>cm;
      }
      friend float sum(DM a,DB b);
};
class DB
{
              int ft,in;
   public:
               void get_data()
      {
cout<<"\nEnter feet value : ";
               cin>>ft;
cout<<"\nEnter inches value : ";
               cin>>in;
      }
      friend float sum(DM a,DB b);
};
float sum(DM a,DB b)
{
               float x,y,z;
   x=(a.m+(a.cm/100));
   y=(b.ft+(b.in/12));
   z=(x+(y*0.304)); //Since 1 ft = 0.304 m
   return z;
}
void main()
{
   DM a;
   DB b;
               cout<<"\nEnter the value in meter and centimeter: ";
               a.get_data();
   cout<<"\nEnter the value in feet and inches: ";
               b.get_data();
               sum(a,b);
   cout<<"\nThe summed value in meter is: "<<sum(a,b);
   getch();
}

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.