Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

overloading operators in c++

Posted on 2003-03-19
5
Medium Priority
?
210 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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

577 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