problem of 2 classes DM and DB-Create two classes DM and DB

Create two classes DM and DB which store the value of distances. DM stores in meters and centimeters and DB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object of DB. Use a friend function to carry out the addition operation. The object that stores the results maybe DM object or DB object depending on the units in which the results are required. The display should be in the format of feet and inches or meters and centimeters depending on object on display.

#include<iostream.h>
#include<conio.h>
class db;
class dm
{
double mt;
double cm;
public:
void getdata(void);
void display(void);
friend dm add(dm,db);
};
class db
{
int feet;
float inches;
public:
void getdata(void);
void display(void);
friend dm add(dm,db);
};
void dm::getdata(void)
{
cout<<"Enter distance in meter: ";
cin>>mt;
cout<<"Enter distance in centimeter: ";
cin>>cm;
}
void dm::display(void)
{
cout<<"Distance in mt is: "<<mt;
cout<<"Distance in cm is: "<<cm;
}
void db::getdata(void)
{
cout<<"Enter distance in feet: ";
cin>>feet;
cout<<"Enter distance in inches: ";
cin>>inches;
}
void db::display(void)
{
cout<<"Distance in feet is: "<<feet;
cout<<"Distance in inches is: "<<inches;
}
dm add(dm a, db b)
{
dm temp;
db te;
temp.mt=a.mt+a.cm/100+b.inches+0.0254+b.feet*0.3048;
temp.cm=temp.mt*100;
te.feet=temp.mt*3.2808;
te.inches=temp.mt*39.37;
return(temp);
}
void main()
{
clrscr();
dm a;
a.getdata();
db b;
b.getdata();
clrscr();
dm c;
c=add(a,b);
c.add(a,b);
c.display();
b.display();
getch();
}

This Post Has 2 Comments

  1. Margherita

    A wonderful article that I have shared. How did I make my child love to read:
    [LINK REMOVED]

    1. Author

      Thank You 🙂

Leave a Reply