C++

Topic: Operators

What is operator overloading in C++?  

C++ provides ability to overload most operators so that they perform special operations relative to classes. For example, a class String can overload the + operator to concatenate two strings.When an operator is overloaded, none of its original meanings are lost. Instead, the type of objects it can be applied to is expanded. By overloading the operators, we can use objects of classes in expressions in just the same way we use C++’s built-in data types.Operators are overloaded by creating operator functions. An operator function defines the operations that the overloaded operator will perform on the objects of the class. An operator function is created using the keyword operator.For example: Consider class String having data members char *s, int size, appropriate function members and overloaded binary operator + to concatenate two string objects.class String{char *s;int sz;public:              String(int n)              {                         size = n;                         s = new char [n];              }             void accept()             {                    cout <<”\n Enter String:”;                    cin.getline(s, sz);             }             void display()             {                     cout<<”The string is: ”<<s;            }             String operator +(String s2)         // ‘+’ operator overloaded             {                     String ts(sz + s2.sz);                     for(int i = 0; s[i]!=’\0’;i++)                                  ts.s[i] = s[i];                     for(int j = 0; s2.s[j]!=’\0’; i++, j++)                                  ts.s[i] = s2.s[j];                     ts.s[i] = ‘\0’;                     return ts;}};int main(){          String s1, s2, s3;          s1.accept();          s2.accept();          s3 = s1 + s2;           //call to the overloaded ‘+’ operator          s3.display()}If you pass strings “Hello” and “World” for s1 and s2 respectively, it will concatenate both into s3 and display output as “HelloWorld”Operator overloading can also be achieved using friend functions of a class. 

Browse random answers:

What is a scope resolution operator?
Why the constructor can't be virtual?
Why can't we overload the sizeof, :?, :: ., .* operators in c++?
Difference between a "assignment operator" and a "copy constructor"
Can you explain the term "resource acquisition is initialization?"
Difference between realloc() and free()?
Define copy constructor? What is the use of copy constructor?
Explain the scope resolution operator.
What is aggression and association?
In C++, what is a constructor,destructor?
How to convert ascii into number value like if i typed in the letter 'R' it would give me a value of 120 0r something ?
What problems might the following macro bring to the application?
What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.
What is the difference between a copy constructor and an overloaded assignment operator?
Hi I would like to know how to convert binary to decimal in c++?
What is a scope resolution operator?
What is function overloading and operator overloading?
How can you overload a method?
what is the difference between ==and.=operator
Assignment Operator - What is the diffrence between a "assignment operator" and a "copy constructor"?
What are operators?
What is the difference between operator new and the new operator?
Can you declare the override method static while the original method is non-static?
What is a scope resolution operator?
What is operator overloading in C++?  
What is overloading unary operator?  
What is function overloading and operator overloading?  
Explain about Arithmetic Operators with example ?
Explain about Relational Operators with example ?
What is function overloading?  
Explain about Relational and equality operators ( ==, !=, >, <, >=, <= ) ?
What are other operators ?
Explain about Precedence of operators with example ?
Explicit type casting operato ?r
Explain about Comma operator ( , ) ?
Explain about Bitwise Operators ( &, |, ^, ~, <<, >> )
Explain about Conditional operator ( ? ) with example ?
Explain about Logical operators ( !, &&, || ) with example ?
Explain about Increase and decrease (++, --) with example ?
Explain about Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
Explain about Arithmetic operators ( +, -, *, /, % ) ?
Explain about Assignment (=) ?
what is the operator overloading in c++
What is the difference between "overloading" and "overriding"?
How many ways can an argument be passed to a subroutine?
What is operator overloading?what r the advantages of operator overloading?
What is function overriding?
Can main() be overridden?
What is difference between overloading and overriding?
In C++, what is the difference between method overloading and method overriding?
Can destructor be private?
You?re given a simple code for the class Bank Customer. Write the following functions:* Copy constructor* = operator overload* == operator overload* + operator overload (customers? balances should be added up, as an example of joint account between husband and wife)
What is the need /use of function overloading
Can a method be overloaded based on different return type but same argument type ?
What is the difference between shadow and override?
What is the difference between a template class and class template?