Create two classes DM and DB which store the value of distances. DM stores distance 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 by keeping following in mind. Use friendly function to carry out the addition. The object that stores the results may be a DM or DB object, Depending on the unit in which the results are required.
Q.) Create two classes DM and DB which store the value of distances. DM stores
distance 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 by keeping following in mind. Use friendly function to carry out
the addition. The object that stores the results may be a DM or DB object,
Depending on the unit in which the results are required.
#include <iostream>
using namespace std;
class DM;
class DB
{
float feet, inches;
public:
void init()
{
cout << "Enter The Distance in Feet :"; cin >> feet;
cout << "Enter the Distance in inches : "; cin >> inches;
}
void display()
{
cout << "After Addition : \n";
cout << "Feet : " << feet << " Inches : " << inches << endl;
}
friend DB add(DM, DB);
};
class DM
{
float meter, centimeter;
public:
void init()
{
cout << "Enter The Distance in Meter :";
cin >> meter;
cout << "Enter the Distance in CentiMeter : ";
cin >> centimeter;
}
friend DB add(DM, DB);
};
DB add(DM obj1, DB obj2)
{
DB obj3;
obj3.feet = obj2.feet + (obj1.meter * 3.2); obj3.inches = obj2.inches + (obj1.centimeter * 0.39); obj3.feet = obj3.feet + ((int)obj3.inches / 12); obj3.inches = (int)obj3.inches % 12;
return obj3;
}
int main()
{
DB obj1, obj2; DM obj3;
obj1.init();
obj3.init();
obj2 = add(obj3, obj1); obj2.display();
return 0;
}
==========================================================================
God Bless You ......
Comments
Post a Comment