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;
}

Recommended Answers

All 4 Replies

You need to return a reference to the overloaded operator.

ie. friend operator+(const DM &, const DB &);
{
     /*Declare a new class*/
    /*your code*/
    return newClass;
}

Also you need to code an overloaded assignment operator.

Thank you for your reply.
But I have not got it.
I don't understand what u said.

Can somebody help me?

>Can somebody help me?
I don't know what you've covered so far in your class, but I would use a template for this kind of variance in return type.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.