Create two classes DM and DB which store the value of distances. DM stores
distances 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 stores the results may a 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 the
object on display.
1Feet = 0.3048 Meter, 1Meter = 3.28 Feet,
1Inch = 2.54 Centimeter, 1Centimeter = 0.3937 Inch
#include <iostream.h>
#include <conio.h>
class db;
class dm
{
float mt;
float cm;
public:
void getdata()
{
cout<<"CLASS DM: \n\n";
cout<<"Enter Values for metres : ";
cin>>mt;
cout<<"Enter Values for centimetres: ";
cin>>cm;
}
void display()
{
cout<<"\nTotal value of distance in metres : "<<mt;
cout<<"\nTotal value of distance in Centimetres : "<<cm;
}
friend dm add(dm,db);
};
class db
{
float feet;
float inches;
public:
void getdata()
{
cout<<"\nCLASS DB: \n\n";
cout<<"Enter Values for feet : ";
cin>>feet;
cout<<"Enter Values for inches : ";
cin>>inches;
}
void display()
{
cout<<"\nThe value of distance in feet is "<<feet;
cout<<"\nThe value of distance in inches is "<<inches;
}
friend dm add(dm,db);
};
dm add(dm a,db b)
{
dm temp;
temp.cm = a.cm + (a.mt*100) + (b.feet*30.48) + (b.inches*2.54);
temp.mt = a.mt + (a.cm*0.01) + (b.feet*0.3048) + (b.inches*0.0254);
return(temp);
}
void main()
{
clrscr();
dm obj1;
obj1.getdata();
db obj2;
obj2.getdata();
cout<<"\nFriend Function Addition : \n";
dm extra;
extra=add(obj1,obj2);
extra.display();
getch();
}
OUTPUT :
No comments:
Post a Comment