?
Solved

overloading operators in c++

Posted on 2003-03-19
5
Medium Priority
?
207 Views
Last Modified: 2012-05-04
I need to know how to take two strings ex("18" and "12") and be able to add them together to get 30.
0
Comment
Question by:PoKe_SmOt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8170546
use atoi() in your overload + function.
0
 

Accepted Solution

by:
Cuperous earned 80 total points
ID: 8171438
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
 
LVL 2

Expert Comment

by:MirkoLMV
ID: 8177708
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
 

Expert Comment

by:Muugokszhiion
ID: 8621756
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
 
LVL 9

Expert Comment

by:tinchos
ID: 9510641

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question