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

Operator Overloading in C++

Operator overloading allows operators to be redefined for user-defined types like classes. The document discusses overloading unary operators like ! to take the NOT of an integer member, and overloading binary operators like + and - to add and subtract complex numbers. It also provides an example of overloading * to multiply two matrices by creating a Matrix class and overloading the * operator to perform matrix multiplication. Code examples and outputs are provided for overloading ! to invert an integer, overloading + and - to add and subtract complex numbers, and overloading * to multiply matrices.

Uploaded by

Candid Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views8 pages

Operator Overloading in C++

Operator overloading allows operators to be redefined for user-defined types like classes. The document discusses overloading unary operators like ! to take the NOT of an integer member, and overloading binary operators like + and - to add and subtract complex numbers. It also provides an example of overloading * to multiply two matrices by creating a Matrix class and overloading the * operator to perform matrix multiplication. Code examples and outputs are provided for overloading ! to invert an integer, overloading + and - to add and subtract complex numbers, and overloading * to multiply matrices.

Uploaded by

Candid Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EXPERIMENT 06

Operator Overloading
Objectives:
 To implement the concepts of Operator Overloading.
Equipment /Tool:
PC, Microsoft Visual Studio 2013
Background:

Operator Overloading:
Example: Overloading Unary Operators

2
Fig 6.1: Operator Arguments

3
Nameless Temporary Object:

4
Postfix Notation:

5
Procedure:

Perform all the tasks and provide your code and result in the space below the questions.
Lab Tasks

Q1) Write a program that overload “!” operator to take NOT of an integer member from class
invert

Answer:

Solution

CODE:
#include <iostream>
#include <conio.h>
using namespace std;
class invert

{
private:
int abd;
public:
invert() : abd(0)
{}
int oper()
{
return abd;
}
void operator !()
{
abd = !abd;
}
};
void main()
{

invert abd;
!abd;
cout << [Link]()<<endl;
system("pause");
}

Output:
6
Q2) Write a program to add and subtract two complex numbers using operator overloading.

Answer:
Solution

CODE:
#include<iostream>
#include<conio.h>
using namespace std;
class complex

{
int real;
int imag;
public:
complex() :real(0), imag(0)
{}
complex(int a, int b)

{
real =a;

imag = b;

}
complex operator +(complex c)
{

complex temp;
[Link] = real + [Link];
[Link] = imag + [Link];
return(temp);
}
complex operator -(complex c)
{
complex temp;
[Link] = real - [Link];
[Link] = imag - [Link];
return(temp);
}
void show()
{
cout << "by performing addtion:" << endl;
cout << real << endl;

7
cout << imag << endl;
}
};
void main()

{
complex c1(9, 8), c2(17, 9), c3, c4;
c3 = c1 + c2;
[Link]();
c4 = c1 - c2;
[Link]();
_getch();
}

Output:

Q3) Write a C++ program to multiply two matrices. For that, create a class ‘matrix’ that contains
a matrix ‘arr’. Overload the operator ‘*’ for ‘matrix’ class which will perform matrix
multiplication.

Answer:

Solution

You might also like