Need help w/simple program

This is a discussion on Need help w/simple program within the C++ Programming forums, part of the General Programming Boards category; Could you please tell me what I am doing wrong? I am trying to overload the + operator, and output ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    74

    Need help w/simple program

    Could you please tell me what I am doing wrong? I am trying to overload the + operator, and output the sum of the 5 numbers (a, b, c, d, e).

    Code:
    #include <iostream.h>
    #include <math.h>
    
    class BigInteger{
    private:
    	
    public:
    	double a, b, c, d, e;
    	void initial();
    	//sum(BigInteger &one, BigInteger &two);
    	friend BigInteger operator+(BigInteger);
    };
    
    void BigInteger::initial()
    {
    	cout << "Please enter 5 integers: " ;
    	cin >> a >> b >> c >> d >> e;
    }
    
    BigInteger operator+(BigInteger &num)
    {
    	//BigInteger num;
    	num = ;         // what do I put here???
    	return num;
    }
    
    int main()
    {
    	BigInteger one, two;
    	one.initial();
    	
    	cout << one.a << one.b << one.c << one.d << one.e <<endl;
    	
    	return 0;
    
    }

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Code:
    BigInteger operator+(BigInteger &num)
    {
    	//BigInteger num;
    	num = ;         // what do I put here???
    	return num;
    }
    Well itīs up to you to deside! What du you want it do when you add???

    I would guess that you want to add all data-members in the class and return the result. One solution may be

    Code:
    BigInteger operator+(BigInteger &num)
    {
                    BigInteger result_of_addition;
                    result_of_addition.a = a + num.a;
                    result_of_addition.b = b + num.b;
                    result_of_addition.c = c + num.c;
                    result_of_addition.d = d + num.d;
                    result_of_addition.e = e + num.e;
    	return result_of_addition;
    }
    Your question isnīt easy to answer, but if you describe it more in detail it certainly would be easier for me to help you.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks Ripper....I want to enter 5 integers (a thru e), then add them together and print the result.

  4. #4
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Code:
    //...
    BigInteger BigInteger :: operator++() { //changed from friend function to member function
    return a+b+c+d+e;
    }
    //...
    int main() {
    BigInteger ob1;
    ob1.initial();
    cout<<++ob1;
    return 0;
    }
    The above may be wrong cos im incompetent
    Well I tried.
    I AM WINNER!!!1!111oneoneomne

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Code:
    ...
    BigInteger BigInteger :: operator++() { //changed from friend function to member function
    return a+b+c+d+e;
    }
    ...
    hmmm well it depends again how you want it to behave. For instance if I would overload the ++ operator I would try it to mimic the built in types. My version would go something like this

    Code:
    const BigInteger & BigInteger :: operator++() 
    { 
    a++; b++; c++; d++; e++;
    return *this;
    }
    This would increment the data-members by one and return a const refernce (more efficient then returing a copy of it). Now it is possible to e.i
    Code:
    ...
    BigInteger one, two;
    one.initial();
    two = ++one;
    ...
    Also I would encourage you to use the constructor insteed of your initial member-function. The constructor is there so you can initialize the data-members.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21