1 Replies - 322 Views - Last Post: 05 October 2012 - 09:49 PM Rate Topic: -----

#1 ZenithMkII  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 06-September 12

Operator overloading for user defined class.

Posted 05 October 2012 - 09:36 PM

So I wanted to make a class HugeInt:
#ifndef HUGEINT_H
#define HUGEINT_H

#include <iostream>

using namespace std;

class HugeInt {
	friend ostream &operator<<(ostream &,const HugeInt);
	public:
		HugeInt(long = 0); //conversion default constructor
		HugeInt( const char *); // constructor
		HugeInt operator+(const HugeInt &); //add another HugeInt
		HugeInt operator+(int); //add an int
		HugeInt operator+(const char *); //add an int in a char *
		HugeInt operator*(const HugeInt &); //add another HugeInt
		HugeInt operator*(int); //add an int
		HugeInt operator*(const char *); //add an int in a char *
	private:
		short integer[30];
};

#endif
Where you can use strings or int to initialize it. The number would be stored in an array, so if the number 2300 was stored in the integer array it would look like [0][0][0][0]...[0][2][3][0][0]. How would I go about multiplying 2 arrays and dealing with the carry overs? I'm going to overload the * operator so that I may be able to multiply two HugeInt variables. No need for a great solution, just some guidance. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Operator overloading for user defined class.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,531
  • Joined: 05-May 12

Re: Operator overloading for user defined class.

Posted 05 October 2012 - 09:49 PM

What have you researched? What have you tried? Do you remember how you did multiplication in grade school? The same algorithm will work equally as well since you so there was no need for a great solution.

For the overloads that take strings, convert the strings to HugeInts then just call your HugeInt operator. For the overloads that take ints, covert the ints HugeInts as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1