answersLogoWhite

0

There is no need to overload the plus operator to achieve this. It is already overloaded with this functionality.

#include<iostream>

#include<string>

int main()

{

int x=40;

int y=2;

int z=x+y;

std::cout<<x<<" + "<<y<<" = "<<z<<std::endl;

std::string s1="Hello ";

std::string s2="world!";

std::string s3=s1+s2;

std::cout<<"""<<s1.c_str()<<"" + ""<<s2.c_str()<<"" = ""<<s3.c_str()<<"""<<std::endl;

return(0);

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Continue Learning about Engineering

Rules for function overloading?

It's a way by which you use define the same function for different input types. For example, think about the the operator "+" which in java works for adding integers, floating point numbers and even string concatenation. The way such functionality is achieved is by overloading.


C program to multiply two complex no?

/*C++ program to multiply two complex numbers using * operator overloading*/ #include&lt;iostream.h&gt; #include&lt;conio.h&gt; class complex { float x,y; public: complex() {} complex(float real,float img) { x=real; y=img; } complex operator*(complex); void display() { cout&lt;&lt;x&lt;&lt;" + "&lt;&lt;y&lt;&lt;"i"&lt;&lt;endl; } }; complex complex::operator*(complex e) { complex temp; temp.x=x*e.x+y*e.y*(-1); temp.y=x*e.y+y*e.x; return(temp); } void main() { clrscr(); complex c1(5,3),c2(3,2),c3=c1*c2; c1.display(); c2.display(); cout&lt;&lt;"Multiplication"&lt;&lt;endl; c3.display(); getch(); } OUTPUT: 5 + 3i 3 + 2i Multiplication 9 + 19i


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++.


Accept 5 numbers in an array and display it?

Accept 5 numbers in an array and display it.


How do you find the greatest of three numbers using ternery operator?

Compare the first two numbers with the ternary operator. Store the result in a temporary variable. Compare the temporary variable with the third number, again using the ternary operator.

Related Questions

Rules for function overloading?

It's a way by which you use define the same function for different input types. For example, think about the the operator "+" which in java works for adding integers, floating point numbers and even string concatenation. The way such functionality is achieved is by overloading.


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.


How can you create a new operator through operator overloading?

You cannot create a new operator through operator overloading. You can only redefine an existing operator, with certain limitations. As an example, for a class of complex numbers, having a real and an imaginary part, you might want an addition operator. This is the skeleton of code to do that. I only show the operator, not any constructors or other operators or methods, etc.class complex {private:double real, imaginary;public:complex operator+ (complex operand) {complex temp;temp.real = this.real + operand.real;temp.imaginary = this.imaginary + operand.imaginary;return temp;}};The above answer is for C++. Since this question is also categorized in Java Programming it's important to note that operator overloading is not currently possible in Java.


What are the advantage and disadvantage of operator overloadin?

Operator overloading allows c/c++ operators to have user defined meanings on user defined types. For example + operator is used to add to numbers but we can also use it for concatenating a string the only limitation is you cannot change the literal meaning of the operator.


C program to multiply two complex no?

/*C++ program to multiply two complex numbers using * operator overloading*/ #include&lt;iostream.h&gt; #include&lt;conio.h&gt; class complex { float x,y; public: complex() {} complex(float real,float img) { x=real; y=img; } complex operator*(complex); void display() { cout&lt;&lt;x&lt;&lt;" + "&lt;&lt;y&lt;&lt;"i"&lt;&lt;endl; } }; complex complex::operator*(complex e) { complex temp; temp.x=x*e.x+y*e.y*(-1); temp.y=x*e.y+y*e.x; return(temp); } void main() { clrscr(); complex c1(5,3),c2(3,2),c3=c1*c2; c1.display(); c2.display(); cout&lt;&lt;"Multiplication"&lt;&lt;endl; c3.display(); getch(); } OUTPUT: 5 + 3i 3 + 2i Multiplication 9 + 19i


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++.


What is the formula of combination of letters numbers and some symbols in Excel?

A combination of letters and numbers and symbols is a character set. To use a formula to combine letter and numbers and symbols is known as concatenation.


Accept 5 numbers in an array and display it?

Accept 5 numbers in an array and display it.


What does add operator do?

Add two numbers.


How do operators provide the necessary data for Java program?

Technically, they don't. Operators only compare values and/or assign new ones. In the case of operator overloading for class objects, the specifics of how a particular operator interacts with class data is defined within the class method.


What is a combination of numbers variable and operator symbols?

It is an expression.


How do you find the greatest of three numbers using ternery operator?

Compare the first two numbers with the ternary operator. Store the result in a temporary variable. Compare the temporary variable with the third number, again using the ternary operator.