Write a C++ program to illustrate assignment operator overloading.
AIM:
Write
a C++ program to illustrate assignment operator overloading.
THEORY:
An assignment operator is the
operator used to assign a new value to a variable, property, event or indexer
element in C# programming language. Assignment operators can also be used for
logical operations such as bitwise logical operations or operations on integral
operands and Boolean operands.
Unlike in C++, assignment operators in C# cannot be overloaded directly, but the user-defined types can overload the operators like +, -, /, etc. This allows the assignment operator to be used with those type
The following are the characteristics of assignment operators:
Unlike in C++, assignment operators in C# cannot be overloaded directly, but the user-defined types can overload the operators like +, -, /, etc. This allows the assignment operator to be used with those type
The following are the characteristics of assignment operators:
- When using the "=" operator for an
assignment with the left operand as the property or indexer access, the
property or indexer must have a set accessor.
- Overloading a binary operator implicitly
overloads its corresponding assignment operator (if any).
- The different assignment operators are based
on the type of operation performed between two operands such as addition
(+=), subtraction, (-=), etc. The meaning of the operator symbol used
depends on the type of the operands.
- Assignment operators are right-associative,
which means they are grouped from right to left
- nment using assignment operator (a += b)
achieves the same result as that without ( =a +b), the difference between
the two ways is that unlike in the latter example, "a" is
evaluated only once.
- The assignment operator usually
returns a reference
SOURCE CODE:
#include<iostream>
using namespace std;
class A{
public:
int
a,b;
A(){ }
A
(int m,int n) {
a=m;
b=n;
}
void
operator=(A ob) {
a=ob.a;
b=ob.b;
}
void
show() {
cout<<"\n
a= "<<a<<"\n b= "<<b;
}
};
int main(){
A
ob1(45,90),ob2;
cout<<"\n
\n ob1 value before operator=()";
ob1.show();
ob2=ob1;
cout<<"\n
\n ob2 value after operator()";
ob2.show();
}
Labels: oop through c++ lab
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home