only difference in return type

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 42
Reputation: bluebird is an unknown quantity at this point 
Solved Threads: 0
bluebird bluebird is offline Offline
Light Poster

only difference in return type

 
0
  #1
Mar 22nd, 2009
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.

  1. #include<iostream.h>
  2.  
  3. class DB;
  4. class DM
  5. {
  6. float me,cm;
  7. public:
  8. DM()
  9. {}
  10.  
  11.  
  12. DM(float m,float c)
  13. {
  14. me=m;
  15. cm=c;
  16. }
  17. friend DB operator+(DM,DB);
  18.  
  19. };
  20.  
  21. class DB
  22. {
  23. float ft;
  24. float inches;
  25.  
  26. public:
  27. DB()
  28. {}
  29.  
  30. DB(float f,float in)
  31. {
  32. ft=f;
  33. inches=in;
  34. }
  35. void display()
  36. {
  37. cout<<"\nfeet:"<<ft;
  38. cout<<"\ninches:"<<inches;
  39. }
  40.  
  41.  
  42.  
  43.  
  44. friend DB operator+(DM,DB);
  45.  
  46.  
  47. };
  48.  
  49.  
  50. DB operator+(DM m, DB b)
  51. {
  52. DB b2;
  53. m.cm+=m.me*10;
  54. b.inches+=b.ft*12;
  55. b2.inches=m.cm*0.3937+b.inches;
  56. while(b2.inches>=12)
  57. {
  58. b2.ft++;
  59. b2.inches-=12;
  60. }
  61. return b2;
  62. }
  63.  
  64. int main()
  65. {
  66. DM dm1 (1,9);
  67. DB db1(1,1);
  68. DB db2;
  69. db2=dm1+db1;
  70. db2.display();
  71.  
  72. return 0;
  73. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 10
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: only difference in return type

 
0
  #2
Mar 22nd, 2009
You need to return a reference to the overloaded operator.
  1. ie. friend operator+(const DM &, const DB &);
  2. {
  3. /*Declare a new class*/
  4. /*your code*/
  5. return newClass;
  6. }
Also you need to code an overloaded assignment operator.
Last edited by kenji; Mar 22nd, 2009 at 4:44 pm.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 42
Reputation: bluebird is an unknown quantity at this point 
Solved Threads: 0
bluebird bluebird is offline Offline
Light Poster

Re: only difference in return type

 
0
  #3
Mar 23rd, 2009
Thank you for your reply.
But I have not got it.
I don't understand what u said.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 42
Reputation: bluebird is an unknown quantity at this point 
Solved Threads: 0
bluebird bluebird is offline Offline
Light Poster

Re: only difference in return type

 
0
  #4
Mar 24th, 2009
Can somebody help me?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: only difference in return type

 
1
  #5
Mar 24th, 2009
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC