C++

Topic: Operators

What is overloading unary operator?  

Unary operators are those which operate on a single variable. Overloading unary operator means extending the operator’s original functionality to operate upon object of the class. The declaration of a overloaded unary operator function precedes the word operator.For example, consider class 3D which has data members x, y and z and overloaded increment operators:class 3D{          int x, y, z;         public:              3D (int a=0, int b=0, int c=0)               {                     x = a;                     y = b;                      z = c;              }              3D operator ++() //unary operator ++ overloaded               {                        x = x + 1;                         y = y + 1;                        z = z + 1;                        return *this; //this pointer which points to the caller object                }               3D operator ++(int) //use of dummy argument for post increment                          operator               {                      3D t = *this;                       x = x + 1;                       y = y + 1;                       z = z + 1;                       return t; //return the original object                }               3D show()                {                       cout<<”The elements are:\n”                       cout<<”x:”<<this->x<<”, y:<this->y <<”, z:”<<this->z;                }};int main(){        3D pt1(2,4,5), pt2(7,1,3);         cout<<”Point one’s dimensions before increment are:”<< pt1.show();         ++pt1; //The overloaded operator function ++() will return object’s this pointer         cout<<”Point one’s dimensions after increment are:”<< pt1.show();         cout<<”Point two’s dimensions before increment are:”<< pt2.show();          pt2++; //The overloaded operator function ++() will return object’s this pointer          cout<<”Point two’s dimensions after increment are:”<< pt2.show();           return 0;}The o/p would be:Point one’s dimensions before increment are:x:2, y:4, z:5Point one’s dimensions after increment are:x:3, y:5, z:6Point two’s dimensions before increment are:x:7, y:1, z:3Point two’s dimensions after increment are:x:7, y:1, z:3Please note in case of post increment, the operator function increments the value; but returns the original value since it is post increment. 

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?