3 Replies - 157 Views - Last Post: 14 July 2012 - 06:22 PM Rate Topic: -----

#1 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 17-June 12

Displaying overloading addition and subtraction results in Main

Posted 14 July 2012 - 03:03 PM

Hello, I am supposed to write a code with a class called DecimalClass. The data members are integer part and decimal part. If the object is num1 and has integerpart 100 and decimal class 33, it represents the number 100.33. Two objects need to be added(overloaded) and subtracted and the ouput displayed on main.
My problem is that I know how to make a separate function to add and stuff, and another one to show the output. But I am asked to add them(overload the objects) from main and I can only use a different function to display.If this is not possible in c++ maybe I misinterpreted. This is what I have so far. I honestly don't know where to go from here.



#include <iostream>

class decimalClass 
{
private: // list the data members 
	double integerPart;
	double decimalPart ; 
	
		
public: 
	decimalClass (int m = 0, int n = 0)
	{
		integerPart = m;

		decimalPart =n;
	};
	void show();
};

int main()
{
	decimalClass num1(23, 45);
	decimalClass num2(15, 80);
	decimalClass num3(10, 27);
	decimalClass num4(13, 67);


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying overloading addition and subtraction results in Main

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 988
  • View blog
  • Posts: 3,442
  • Joined: 19-February 09

Re: Displaying overloading addition and subtraction results in Main

Posted 14 July 2012 - 04:44 PM

Sounds like you want to overload some operators such as +.

  num1 = num2 + num3;




For that you would add this function to your class. When the compiler finds the plus sign operator between two decimalClass objects it uses this function. The parameter dc would be the object on the right hand side of the + operator (num3 above). The function body needs to be written of course.

  decimalClass operator+ (decimalClass dc);




More info : Classes (II)
Was This Post Helpful? 0
  • +
  • -

#3 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 17-June 12

Re: Displaying overloading addition and subtraction results in Main

Posted 14 July 2012 - 06:07 PM

View Post#define, on 14 July 2012 - 04:44 PM, said:

Sounds like you want to overload some operators such as +.

  num1 = num2 + num3;




For that you would add this function to your class. When the compiler finds the plus sign operator between two decimalClass objects it uses this function. The parameter dc would be the object on the right hand side of the + operator (num3 above). The function body needs to be written of course.

  decimalClass operator+ (decimalClass dc);




More info : Classes (II)


Yeah I have already written it down, except , I have one more question I don't know if you can help me regarding this. I keep getting an error with expression must have enum integral type. If you think you might I can put my code in here to see if you can help me. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 988
  • View blog
  • Posts: 3,442
  • Joined: 19-February 09

Re: Displaying overloading addition and subtraction results in Main

Posted 14 July 2012 - 06:22 PM

Yes, show the code we may be able to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1