0% found this document useful (0 votes)
2 views4 pages

Lab 6 Binaryoperator Overloading OOP

The document outlines an experiment for a C++ course focused on operator overloading with complex numbers, specifically performing addition, subtraction, multiplication, and division. It includes a theoretical explanation of operator overloading, along with example code demonstrating binary operator overloading using a class. The experiment aims to enhance understanding of operator overloading concepts in C++ programming.

Uploaded by

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

Lab 6 Binaryoperator Overloading OOP

The document outlines an experiment for a C++ course focused on operator overloading with complex numbers, specifically performing addition, subtraction, multiplication, and division. It includes a theoretical explanation of operator overloading, along with example code demonstrating binary operator overloading using a class. The experiment aims to enhance understanding of operator overloading concepts in C++ programming.

Uploaded by

pranav duse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Progressive Education Society’s

Modern College of Engineering


Department of Electronics and Telecommunication Engineering
Electronics and Computer Engineering Program
ACADEMIC YEAR: Class: S.E. Semester- II
Course: Object Oriented Programming

Experiment No 6
TITLE:
Write a program in C++ to perform following operations on complex numbers Add, Subtract, Multiply, Divide. Use
operator overloading for these operations.

OBJECTIVES OF THE EXPT.:


The objective of this assignment is to learn the concepts operator overloading.

APPARATUS: Computer with Windows Operating System. CODE BLOCK or Eclipse IDE (Neon 3). Or Dev C++
THEORY:
C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is
called function overloading and operator overloading respectively. An overloaded declaration is a declaration that
is declared with the same name as a previously declared declaration in the same scope, except that both
declarations have different arguments and obviously different definition (implementation).
When you call an overloaded function or operator, the compiler determines the most appropriate definition to use,
by comparing the argument types you have used to call the function or operator with the parameter types specified
in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload
resolution.
Binary Operator Overloading in C++
In the binary operator overloading function, there should be one argument to be passed. It is the overloading of an
operator operating on two operands. Below is the C++ program to show the overloading of the binary operator (+)
using a class Distance with two distant objects.

// C++ program to show binary


// operator overloading using
// a Friend Function
#include <iostream>
using namespace std;

class Distance {
public:

int feet, inch;

Distance()
{
this->feet = 0;
this->inch = 0;
}

Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}

// Declaring friend function


// using friend keyword
friend Distance operator + (Distance&, Distance&);
};

// Implementing friend function


// with two parameters
// Call by reference
Distance operator+(Distance& d1,
Distance& d2)
{
// Create an object to return
Distance d3;

d3.feet = d1.feet + d2.feet;


d3.inch = d1.inch + d2.inch;

// Return the resulting object


return d3;
}

// Driver Code
int main()
{
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;

// Use overloaded operator


d3 = d1 + d2;

cout << "\nTotal Feet & Inches: " <<


d3.feet << "'" << d3.inch;
return 0;
}
Output
Total Feet & Inches: 18'11

Overloading Binary Operator using a Friend function


In this approach, the operator overloading function must be preceded by the friend keyword, and declare the
function in the class scope. Keeping in mind, the friend operator function takes two parameters in a binary operator
and varies one parameter in a unary operator. All the working and implementation would same as the binary
operator function except this function will be implemented outside the class scope.

// C++ program to show binary


// operator overloading using
// a Friend Function
#include <iostream>
using namespace std;
class Distance {
public:

int feet, inch;

Distance()
{
this->feet = 0;
this->inch = 0;
}

Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}

// Declaring friend function


// using friend keyword
friend Distance operator + (Distance&,Distance&);
};

// Implementing friend function


// with two parameters
// Call by reference
Distance operator+(Distance& d1,
Distance& d2)
{
// Create an object to return
Distance d3;

d3.feet = d1.feet + d2.feet;


d3.inch = d1.inch + d2.inch;

// Return the resulting object


return d3;
}

// Driver Code
int main()
{
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;

// Use overloaded operator


d3 = d1 + d2;

cout << "\nTotal Feet & Inches: " <<


d3.feet << "'" << d3.inch;
return 0;
}
Output
Total Feet & Inches: 18'11

The operator function is implemented outside of the class scope by declaring that function as the
friend function.
In these ways, an operator can be overloaded to perform certain tasks by changing the functionality
of operators
COMMENTS AND CONCLUSION:

FAQ: What is operator overloading?


Which operator can not be overloaded

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy