0% found this document useful (0 votes)
163 views8 pages

Unary Operator and Binary Operator Overloading

Here are some practice programs to implement unary logical NOT (!) operator overloading in C++ : 1. Overload logical NOT operator for a Boolean class: class Boolean { private: bool value; public: Boolean(bool val) { value = val; } Boolean operator!() { if(value) return Boolean(false); else return Boolean(true); } void display() { cout << value; } }; 2. Overload logical NOT operator for a Bit class: class Bit { private: int value; public: Bit(int val) { value = val; } Bit operator!() { if

Uploaded by

Lavanya Sinha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
163 views8 pages

Unary Operator and Binary Operator Overloading

Here are some practice programs to implement unary logical NOT (!) operator overloading in C++ : 1. Overload logical NOT operator for a Boolean class: class Boolean { private: bool value; public: Boolean(bool val) { value = val; } Boolean operator!() { if(value) return Boolean(false); else return Boolean(true); } void display() { cout << value; } }; 2. Overload logical NOT operator for a Bit class: class Bit { private: int value; public: Bit(int val) { value = val; } Bit operator!() { if

Uploaded by

Lavanya Sinha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

PP Lab Name: PRN:

Lavanya Sinha 21070122086


Practical No 10 Date: /11/2021

Title: Write a C++ menu driven program to implement unary operator overloading (prefix and
postfix increment operator) and binary operator overloading (+ and <).

Description:
In C++, we can change the way operators work for user-defined types like objects and structures.
This is known as operator overloading. Using operator overloading in C++, you can specify
more than one meaning for an operator in one scope. The purpose of operator overloading is to
provide a special meaning of an operator for a user-defined data type.

With the help of operator overloading, you can redefine the majority of the C++ operators. You
can also use operator overloading to perform different operations using one operator.

To overload a C++ operator, you should define a special function inside the Class as follows:

class class_name
{
... .. ...
public
return_type operator symbol (argument(s))
{
... .. ...
}
... .. ...
};
Here is an explanation for the above syntax:

 The return_type is the return type for the function.


 Next, you mention the operator keyword.
 The symbol denotes the operator symbol to be overloaded. For example, +, -, <, ++.
 The argument(s) can be passed to the operator function in the same way as functions.

Hint:
Menu:
Press 1 to apply prefix increment operator on the object of class complex
Press 2 to apply postfix increment operator on the object of class complex
Press 3 to add two objects of class complex (using friend function)
Press 4 to compare two complex numbers using < operator

Overloading unary operator using member function:


Return_type operator op (arg list)
{
}
PP Lab Name: PRN:

Operator overloaded functions can be invoked by expressions:


op x;
Or
x op;
Overloading unary operator using friend function:
friend return_type operator op (class_name &obj_name);
Binary operator overloading to add two complex numbers using member function:
complex operator +(complex m)
{
complex temp;
temp.real=real+m.real;
temp.imag=imag+m.imag;
return (temp);
}

Operator overloaded functions can be invoked by expressions:


c3=c1+c2; //c1,c2,c3 are objects of class complex, this statement is equivalent to
c3=c1.operator+(c2);

Binary operator overloading to add two complex numbers using friend function:
Friend function can also be used in place of member functions for overloading a binary operator,
the only difference being that a friend function requires two arguments to be explicitly passed to
it, while a member function requires only one.
In the complex number program discussed in the previous section, the statement:
c3=c1+c2;
is equivalent to:
c3= operator+(c1,c2);
Binary operator overloading to compare two complex numbers using member function:
bool operator < (complex c)
{…

}

Operator overloaded functions can be invoked by expression like:


If (c1<c2) // c1 and c2 are the objects

Program Code:
#include<iostream>

using namespace std;

class Complex{

int feet,inches;
PP Lab Name: PRN:

public:

Complex(){

feet=0;

inches=0;

Complex(int x,int y){

feet=x;

inches=y;

Complex operator++(){//unary operator

++feet;

++inches; //prefix

Complex operator--(int){//unary operator

feet--;

inches--;

Complex operator+(Complex c){//binary operator

Complex temp;

temp.feet=feet+c.feet;
PP Lab Name: PRN:

temp.inches=inches+c.inches;

return temp;

bool operator <(Complex obj){//binary relational operator

if(feet<obj.feet){

return true;

if(feet == obj.feet && inches<obj.inches){

return true;

return false;

void display(){

cout<<feet<<"ft "<<inches<<"in"<<endl;

};

int main()
{

int a,b,c,d;

cout<<"Enter the feet and inches:"<<endl;


PP Lab Name: PRN:

cin>>a>>b;

Complex c1(a,b);

c1.display();

cout<<"Enter the feet and inches:"<<endl;

cin>>c>>d;

Complex c2(c,d);

c2.display();

Complex c3;

int choice;
cout<<"case 1=Result after applying prefix increment operator on object of class
complex\n\n";
cout<<"case 2=Result after applying postfix increment operator on object of class
complex\n\n";
cout<<"case 3=Addition\n\n";
cout<<"case 4=Comparing\n\n";
cout<<"Enter your choice:";

cin>>choice;

switch(choice){

case 1://Result after applying prefix increment operator on object of class complex:

cout<<"Complex no before increment(prefix):";

c1.display();

++c1;

cout<<"Complex no after increment(prefix):";

c1.display();
PP Lab Name: PRN:

break;

case 2://Result after applying postfix increment operator on object of class complex:

cout<<"Complex no before increment(postfix):";

c2.display();

c2--;

cout<<"Complex no after increment(postfix):";

c2.display();

break;

case 3://Addition of complex :

cout<<"Addition is:"<<endl;

c3=c1+c2;

c3.display();

break;

case 4://Comparing complex which is smaller:

cout<<"Comparing :"<<endl;

if(c1<c2)
{

cout<<"c1 is less than c2"<<endl;

}
PP Lab Name: PRN:

else{

cout<<"c2 is less than c1"<<endl;

break;

return 0;

Input and Output


PP Lab Name: PRN:

Conclusion: Thus we have implemented the concept of operator overloading in C++

Practice programs: Write a C++ program for Unary logical NOT (!) operator overloading.

You might also like