Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

overloading operators in C++

  • 06-12-2001 3:03pm
    #1
    Registered Users, Registered Users 2 Posts: 255 ✭✭


    Im trying to overload the +operator in C++, I know you need to have a class built first because one of the arguements must come from that.

    Its to do with adding two HugeInts, numbers bigger than 30 digits, they re added using an array.

    the class looks something like this but I don't know what its all about,

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

    Any help or insights would be appreciated..

    Hertz


Comments

  • Registered Users, Registered Users 2 Posts: 6,660 ✭✭✭Blitzkrieger


    I don't understand the question :)

    To overload a funtion it's simply a question of giving it different arguments. Eg :

    int addInts(int a, int b);
    int addInts(int a, int b, int c);

    If you use the function call :

    x = addInts(a, b, c) ;

    It will automatically use the second function.


    Oh - I see what you mean :) Never mind the above :)

    I don't know how to do it but any good book on C++ will tell you how. I believe the book I saw it in was "An Introduction to Programming with C++".

    Tbh it seems like a lot of trouble to go to. I'm such a lazy sod I'd just write a addHugeInts function/class.


  • Closed Accounts Posts: 358 ✭✭CH


    >Any help or insights would be appreciated..
    looks like you got this from C++ How to program (Deitel & Deitel), where the chapter on overloading is done very well
    HugeInt operator+(HugeInt &); 	// add another HugeInt 
    HugeInt operator+(int); 	// add an int 
    HugeInt operator+(const char*); //add an int in a char* 
    

    Here you have 3 overloaded member functions prototypes, which are overloading the + operator.
    You must write a function for each of these functions, an example for the second member function could be:
    HugeInt HugeInt::operator+(int rightHandSide)
    { 
    	return *this + HugeInt(rightHandSide); 
    }
    

    When you use: yourObject1 + SOMETHING in main, the compiler evaluates which member function to use depending on what the right operand (SOMETHING) is.

    If the right operand is an object of class HugeInt, the first member function is called: HugeInt operator+(HugeInt &);
    yourObject1 = yourObject2 + yourObject3;
    

    If the right operand is an integer, the second member function is called: HugeInt operator+(int);
    yourObject1 = yourObject1 + 5;
    

    If the right operand is a series of characters, the third member function is called: HugeInt operator+(const char*);
    yourObject1 = yourObject1 + "999";
    

    all of the above additions, will add the right operand to the private array of short ints.

    regards.


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Well I have only glanced through that book but as far as I know the hugeinrt class is an array of chars I think which means your solution for function 2 will not work, ie. you cant add an integer to an array of chars.

    What you need to do for that one is seperate the numbers of the int and then add the units to the units add carryover to the tens of hugeint then add the tens to the tens add carryover to hundreds then add hundreds to hundreds etc, etc, etc.

    I'm too lazy to do any code but its more rewarding to do it yourself anyway.


  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    below is how I did it for concatinating strings but its easy to change it for integers. Check with a library for books on C++. They got some good examples in them.


    #include <iostream>
    #include <string.h>


    class test {
    char name[20];
    public:
    test(char tname[]) {strcpy(name,tname);}
    test operator+(test t1);
    test operator=(test t1);
    void display();
    };

    test test::operator+(test t1)
    {
    test temp(" ");

    strcat(name,t1.name);
    strcpy(temp.name,name);


    return(temp);
    }

    test test::operator=(test t1)
    {
    strcpy(name,t1.name);

    return *this;
    }

    void test :: display()
    {
    cout<<name <<"\n";

    return;
    }
    int main()
    {
    test a("test"),b(" Run"),c(" ");
    char now;

    a.display();
    b.display();
    c.display();

    c = a + b;
    c.display();

    cout<<"\njust a test";
    cin>>now;

    return(0);
    }


    Hope it helps:)


Advertisement