hello everybody!
As far as I understand, the functions that are only different in return type are not allowed.
But how can I write the program for the following question?
*****************************************
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 that stores the result may be 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.
****************************************
Here is my program. It does not work.
please help me. Thanks in advance.
#include<iostream.h>
class DB;
class DM
{
float me,cm;
public:
DM()
{}
DM(float m,float c)
{
me=m;
cm=c;
}
friend DB operator+(DM,DB);
};
class DB
{
float ft;
float inches;
public:
DB()
{}
DB(float f,float in)
{
ft=f;
inches=in;
}
void display()
{
cout<<"\nfeet:"<<ft;
cout<<"\ninches:"<<inches;
}
friend DB operator+(DM,DB);
};
DB operator+(DM m, DB b)
{
DB b2;
m.cm+=m.me*10;
b.inches+=b.ft*12;
b2.inches=m.cm*0.3937+b.inches;
while(b2.inches>=12)
{
b2.ft++;
b2.inches-=12;
}
return b2;
}
int main()
{
DM dm1 (1,9);
DB db1(1,1);
DB db2;
db2=dm1+db1;
db2.display();
return 0;
}