INT Class

So I need to construct a class that functions like an integer. I have tried several things but I am looking for a place to start because I don't know where to go. Here is what I started with:
#ifndef INT_h
#define INT_H

class INT {

public:
//ctor
INT::INT (int){ };
//end ctor

//Mods
INT & operator= (const INT &);//assign
INT & operator+ (const INT &);//Add
INT & operator- (const INT &);//Subtract
INT & operator+= (const INT &); //Add
INT & operator-= (const INT &); ;//Subtract
INT operator++ (); //Prefix
INT operator-- ();////Prefix
INT operator++ (int); //Postfix
INT operator-- (int);//Postfix

//selector
operator int () const {return 0;}
};
#endif

Am I heading in the right direction?
Yes you are. How about / * % operators ? I can use / * % with an integer so theoretically you should overload operator for them too.

Please note for * it can be multiplication but also de-reference also. INT *ptr = new INT; *ptr = 10;

With de-reference, you should cater for reference too that is & but with & means you must have bit-wise operators too like & | ^.
Last edited on
also you should make sure you are defining what you have tested with #ifndef

change
1
2
#ifndef INT_h
#define INT_H 


to

1
2
#ifndef INT_H
#define INT_H 
I added the other modifiers as suggested and corrected the defining "#ifndef INT_H". So when I assign an operator member function, is this the correct format? how should I format if not, this is wrong but I think it is close.

INT::operator* int ()
{
return ;
};

I am just having an issue trying to get formatting down, I haven't been doing any C/C++ for about 3 years and I am foggy to say the least.

return_type scope::function(parameters)

Please note for * it can be multiplication but also de-reference also. INT *ptr = new INT; *ptr = 10;
There you are dereferencing a pointer, not an object.
Last edited on
One other observation. Your class defintion does not have any storage declared.
If you assign a value to an instance of your class, where will it be stored?
Topic archived. No new replies allowed.