954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

only difference in return type

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;
}
bluebird
Junior Poster in Training
58 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

kenji
Junior Poster
145 posts since May 2008
Reputation Points: 11
Solved Threads: 11
 

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

bluebird
Junior Poster in Training
58 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Can somebody help me?

bluebird
Junior Poster in Training
58 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You