overloading operators in c++

I need to know how to take two strings ex("18" and "12") and be able to add them together to get 30.
PoKe_SmOtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

n_fortynineCommented:
use atoi() in your overload + function.
0
CuperousCommented:
This explains the overloaded function and the atoi function
In a Source file, you can write:
#include <stdio.h> //this class is for the gets() function
#include "col.h" //the file containing your class
#include <iostream.h> //Input Output Stream

void main()
{
int x; // declare an integer variable
char str1[10], str2[10]; // declare two strings
gets(str1); //use gets() function to get the first string
gets(str2);//use gets() function to get the second string
Coverload y(str1); // declare and send the first string
                  // using the overloaded construct
x=y+str2;          // Use the overloaded operator to add
cout<<x;           // print the result on the screen
}

In a header file, you can write:

#include <stdlib.h> //this class is for the atoi() function
class Coverload
{
protected:
     char str[10]; //declare string
public:
     Coverload(char string[]) // Overloaded Constructor
     {
     for(int i=0;i<10;i++)  // Copy the string sent
     str[i]=string[i];      
     }
     friend int operator+(Coverload obj, char string2[]);    //Overloaded Operator to add;
};

int operator+( Coverload obj, char string2[])
{
     int total_value=0; // declare the total to equal 0
     total_value += atoi(string2); //converts string2
                                     //to an integer and
                                    //adds it to the total
     total_value += atoi(obj.str); //takes the value of
                                     //the string in obj2
                                    //and converts it into
                                   //an integer and adds
                                  //it to the total
     return total_value; // returns the value          
}



I hope this is useful, please comment, if you have anymore problems. I'll be checking your problem again. And please grade it after testing it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MirkoLMVCommented:
A solution may be the atoi function (array-to-integer), which is a library function declared in the stdlib.h file :
 cout << (atoi("18") + atoi("12")) ;
The desired conversion effect may be obtained with functions like :

int ArrayToInteger (char *s) { int i, n, sign ;
 for (i = 0 ; s[i] == ' '|| s[i] == '\n'|| s[i] == '\t' ; i++) ;
 sign = 1 ;
 if (s [i] = '+' || s[i] == "-') sign (s[i++] == '+')?1:-1 ;
 for (n = 0 ; s[i] >= '0' && s[i] <= '9' ; i++)
  n = 10 * n + s[i] - '\0' ;
 return sign * n ; }
This code is from books for C programming subjects.

An elegant solution is, of course, an operator overloading :
 int operator+ (char*, char*) ;
 (of course, this prototype is for a global, non-class function).

Good luck !
0
MuugokszhiionCommented:
I'm not totally sure what you mean, Poke_Smot, but if you're trying to make it do simple addition in math, then a program similar to this may help:

---------------------------------------------------------------------------------------------
// This program will add two numbers inputted by the user!
#include <iostream.h>
#include <math.h>

int Blah(int x, int y);
int x, y, z;

int main()
{
    cout << "Enter a number please." <<endl;
    cin >> x;
    cout << "Enter one more number." <<endl;
    cin >> y;
    cout << "The sum is " << Blah(x, y) <<endl;
    return 0;
}

int Blah (int x, int y)
{
    z = x + y;
    return z;
}
---------------------------------------------------------------------------------------------

Or you could just skip the simple function 'Blah', resembling the following....

---------------------------------------------------------------------------------------------

#include <iostream.h>
#include <math.h>

int Blah(int x, int y);
int x, y, z;

int main()
{
    cout << "Enter a number please." <<endl;
    cin >> x;
    cout << "Enter one more number." <<endl;
    cin >> y;
    z = x + y;
    cout << "The sum is " << z <<endl;
    return 0;
}
---------------------------------------------------------------------------------------------

Both do the same thing. Hope this helps.
0
tinchosCommented:

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Cuperous

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.