answersLogoWhite

0


Best Answer

#include "stdafx.h"

#include<iostream>

#include<conio.h>

using namespace std;

class Add{

public:

char *rep;

Add(){}

Add(char *tem){

rep = new char[strlen(tem)+1] ;

strcpy(rep,tem);

}

Add operator + (const Add &rhs)

{

char *temp;

temp= new char[strlen(rep) + strlen(rhs.rep)+1];

temp = strcat(rep,rhs.rep);

return Add(temp);

}

};

int main()

{

cout<<"hello";

Add obj1("pradeep");

Add obj2("bansal");

Add obj3;

obj3 = obj1+obj2;

cout<<obj3.rep;

getch();

return 0;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include


int main()

{

std

::

string a

=

"Hello "

;

std

::

string b

=

"World"

;

std

::

string c

=

a

+

b

;

return( 0 );

}

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

The C++ standard library already provides string concatenation in <string>. If you wish to cater for a user-defined string T, use the following implementation:

T operator+ (const T& str1, const T& str2) {

T s { str1 }; // copy the first string

s += str2; // append the second string

return s;

}

Note that you must provide an implementation for the T::operator+= member function. Expressing non-member functions in terms of member functions is standard practice and helps ensure consistency between the two.

For a string object, the implementation of T::operator+= should be similar to the following:

T& T::operator+= (const T& str) {

if (reserve() < str.size()) resize (size() + str.size());

for (auto c : str) append (c);

return *this;

}

The exact implementation will depend upon how your string is implemented.

Note that implementing your own string type is a useful exercise while learning the language, but for real-world applications you should use the std::string type provided by the standard library. It is extremely efficient and the class template can cater for any type of character, including user-defined characters. Consult the source code to get a better understanding of how string concatenation should be implemented.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C plus plus program to concatenate any two strings using operator overloading?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why operator overloading is not there in java?

== == === === === === === === Some Body told me that operator overloading is not there because it violates the transparency of java.since there is no hiding of information in java it does support op overloading === === === === === === Pranab Kumar Rana Software Engineer..... === === === ===


A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string


What is the operator that cannot be overloaded?

There are 5 operators which cannot be overloaded. They are: * .* - class member access operator * :: - scope resolution operator * . - dot operator * ?:: - conditional operator * Sizeof() - operator Note:- This is possible only in C++.


How do you find largest of two object using operator overloading in c?

I will not use operator overloading in C# to do anything. Operator overloading may lead to one operator has more than 1 semantic meaning. For example, we know 1 + 2 yields 3, and "1" + 2 yields "12". I do not like this overloading of the operator + being used for addition in Number hierarchy, while as the concatenation in strings. That is, one operator (+) has 2 significant semantics.And the question "find largest of two object" is too vague - what do you mean "largest"? and object? We know apple and orange are 2 objects, but how do you compare them, and find the largest one?????? (size, price or what???)


What is operator overloading?

I think you mean operation overlord??? It is the American, Canadian and British offensive on Europe in World War 2. They landed in Normandy on 6th June 1944 (Commonly called D-Day, Day of Days or Deliverance Day) and progressed throughout France liberating Paris on the 25th August. This allowed the allies a foothold in Europe.

Related questions

C coding for operator overloading?

C does not support operator overloading. If you mean C++ operator overloading, it depends on exactly what you wanted to do. If you wanted to '+' to strings, then you could write: string operator+(string a, string b) { // do something }


What is operator over loading?

Same operator can be used for different purposes like + can be used for addition of two integers and used for concatenate strings.


What are the restriction that are applied to operator overloading?

Java does not support user defined operator overloading.The operator '+' is overloaded in Java and can be used for adding both numbers and Strings.


Why operator overloading is not there in java?

== == === === === === === === Some Body told me that operator overloading is not there because it violates the transparency of java.since there is no hiding of information in java it does support op overloading === === === === === === Pranab Kumar Rana Software Engineer..... === === === ===


What character allows you to concatenate strings in PHP?

To concatenate strings in PHP, you use the . (dot) character. For example: $str = "String1" . "String2";


A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string


What is the operator that cannot be overloaded?

There are 5 operators which cannot be overloaded. They are: * .* - class member access operator * :: - scope resolution operator * . - dot operator * ?:: - conditional operator * Sizeof() - operator Note:- This is possible only in C++.


How do you find largest of two object using operator overloading in c?

I will not use operator overloading in C# to do anything. Operator overloading may lead to one operator has more than 1 semantic meaning. For example, we know 1 + 2 yields 3, and "1" + 2 yields "12". I do not like this overloading of the operator + being used for addition in Number hierarchy, while as the concatenation in strings. That is, one operator (+) has 2 significant semantics.And the question "find largest of two object" is too vague - what do you mean "largest"? and object? We know apple and orange are 2 objects, but how do you compare them, and find the largest one?????? (size, price or what???)


What is use of dot in php?

The dot (.) operator in PHP is used to concatenate strings. For instance:$start = "Big";$end = "Bird";echo $start . ' ' . $end;This code would produce the output:Big Bird


What is operator overloading?

I think you mean operation overlord??? It is the American, Canadian and British offensive on Europe in World War 2. They landed in Normandy on 6th June 1944 (Commonly called D-Day, Day of Days or Deliverance Day) and progressed throughout France liberating Paris on the 25th August. This allowed the allies a foothold in Europe.


How do you concatenate strings in C programming?

strcat if u wnt to use strcat then include string.h header file


What is concatetion operation used in java?

"+" is the concatenation operator in Java. It can be used to concatenate two strings. Ex: String firstName = "John"; String lastName = "Morrison"; System.out.println(firstName + " " + lastName); The above code snippet would display John Morrison in the console.