| | |
only difference in return type
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 42
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
#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; }
You need to return a reference to the overloaded operator.
Also you need to code an overloaded assignment operator.
C++ Syntax (Toggle Plain Text)
ie. friend operator+(const DM &, const DB &); { /*Declare a new class*/ /*your code*/ return newClass; }
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
![]() |
Similar Threads
- When is a return statement mandatory? (C)
- What is the difference? (C++)
- Pointer and function (C++)
- What's the difference? (Java)
- Domain Extensions - Does it make a difference in Search Engine Indexing? (Search Engine Optimization)
- AnsiString Template Data Return Problem Builder 6 (C++)
- Need to know the difference (C++)
- Objects (C)
Other Threads in the C++ Forum
- Previous Thread: Ignore(1000,'\n')
- Next Thread: Help with queues
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






