Hi everyone!
I'm trying to creat a class calling INT, which is equivalent to type int.
I think we must creat every operator for this class like: +, -, *, /, %, pow(x,y),...
I've never seen how does type int look like?
May someone give me a introduction to type int?
Is my idea correct about above operators?
Thanks in advanced!

Recommended Answers

All 7 Replies

>>I've never seen how does type int look like?
Neither did I. int is a primitive or built-in type. This means you won't find anywhere in the standard libraries an actual definition of int (like "class int { ... }") because the definition of the type is built into the compiler. Since classes are defined as containing data of other types, there has to be some starting types that are built-in otherwise there would be nothing to build other classes from, and these types are called primitives (int, float, char, double, bool, short, etc., etc.).

>>May someone give me a introduction to type int?
The type int stores an integer number (i.e. 1, 2, 3, 29, 342, etc... no floating point values or characters). It has a sign bit as well so negative numbers can be represented too. You can perform arithmetic operations on it (like +, -, *, /, etc.) and a few other math functions (like pow). It length in bytes varies from one system to another but mostly it is 4 bytes on 32bits systems and 8 bytes on 64bits system. I don't know what else to say about it.

>>Is my idea correct about above operators?
I think the point of this exercise is only to get you to know how to overload operators. This wiki will tell you all you need to know about that. In real life, it would be completely retarded to actually do this, but it's a good basic exercise to get to know the syntax of operator overloading. Basically, to create an INT class, you would just have a class with one data member of type "int" and overload all operators to do the same operation on the underlying int data members.

I think we must creat every operator for this class like: +, -, *, /, %, pow(x,y),...

Just two would suffice.

struct INT
{
    INT() {}
    INT( int value ) : builtin_int(value) {}

    operator int& () { return builtin_int ; }
    operator int () const { return builtin_int ; }

    int builtin_int ;
};

Hi vijayan121

I am learning cpp. Can you please explain why we need to over write only these two () operator?

I mean why ++, --, +, - are working? every time we use ++ or -- or any other operator does it invoke the &() operator?

Thanks a mill.

I mean why ++, --, +, - are working? every time we use ++ or -- or any other operator does it invoke the &() operator?

Yes. There is an implicit conversion from an INT to an int& ;

void foo( INT& i )
{
   ++i ;
   // evaluates to ++ (i.operator int&()) ;
   // in effect ++i.builtin_int ; 
}

So for a non-const INT , all int operations are possible; and are applied directly on the member variable builtin_int.

For a const INT , only those int operations that are possible on an r-value are available.

See: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.12

Hello again!
I came back and really graceful and I'm really grateful.
This is my effort:

#include "stdafx.h"
#include <iostream>
using namespace std;
class INT
{
    int x;
public:
    INT(){};
    INT(int);
    ~INT();
    INT operator-();
    INT operator+(INT);
    INT operator-(INT);
    INT operator*(INT);
    INT operator/(INT);
    friend ostream& operator<<(ostream& os, const INT&out); // output
    friend istream& operator>>(istream& os, INT&in); //input

};
INT::INT(int y)
{
    x=y;
}
INT::~INT(){}
INT INT::operator-()
{
    INT z;
    z.x=-(this->x);
    return z;
}
INT INT::operator+(INT y)
{
    INT z;
    z.x=this->x+y.x;
    return z;
}
INT INT::operator-(INT y)
{
    INT z;
    z.x=this->x-y.x;
    return z;
}
INT INT::operator*(INT y)
{
    INT z;
    z.x=(this->x)*y.x;
    return z;
}
INT INT::operator/(INT y)
{
    INT z;
    try {
        if(y.x==0)  throw "division by zero";
        else
              z.x=(this->x)/y.x;
     }
    catch (char* str)
    {
      cout << "Exception: " << str << endl;
    }
    return z;

}
ostream& operator<<(ostream& os, const INT&out) // output
{
       return (os<<out.x);
}
istream& operator>>(istream& os, INT&in) // input
{
    os >> in.x;
    return os;
}

int _tmain(int argc, _TCHAR* argv[])
{
    INT a=10, b=5;
    INT c=-(a+b);
    cout<<c<<endl;
    system("PAUSE");
    return 0;
}

The thing is when using operator division:

INT a=1, b=0, c;
c=a/b;

What I got is:
Exception: division by zero
-858993460
Press any key to continue . . .

Ok.
Then I tried this (in an another console application)

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a=1, b =0, c;
    c=a/b;
    cout<<c<<endl;
    system("PAUSE");
    return 0;
}

and got the result like: 1. #INF

Now what should I do to my first program to get the same result like the second one gave me?
Thanks in advanced!

Member Avatar for embooglement

Given what you've posted as as your second program, that output shouldn't happen. But I'm going to assume that somewhere along the lines, c and at least one of a and b were doubles or floats. In which case, you're getting an infinite value, which is what you get mathematically when you divide by 0. However, integer types aren't capable of representing an "infinite" value. Floating point values, however, have special values for infinite, not-a-number, and such. So, to answer your question, to get your first program to have that output, rewrite your class to be a FLOAT or DOUBLE. However, that kind of defeats the purpose of writing an int class. I'd say stick with throwing the exception, that seems perfectly reasonable to me.

another thing, is that you might want to create a conversion operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.