0% found this document useful (0 votes)
5 views7 pages

Lab 12

The document provides an overview of C++ operator overloading, explaining how it allows customization of operator behavior for user-defined types like classes and structures. It details the types of operator overloading (unary and binary) and specifies four operators that cannot be overloaded. Additionally, it includes syntax examples and outlines several lab tasks related to operator overloading, such as creating a Book class and implementing various operator overloads.

Uploaded by

ziomlubda
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)
5 views7 pages

Lab 12

The document provides an overview of C++ operator overloading, explaining how it allows customization of operator behavior for user-defined types like classes and structures. It details the types of operator overloading (unary and binary) and specifies four operators that cannot be overloaded. Additionally, it includes syntax examples and outlines several lab tasks related to operator overloading, such as creating a Book class and implementing various operator overloads.

Uploaded by

ziomlubda
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
You are on page 1/ 7

Objective: (Mapping CLO 1,2,3,4)

 Introduction to C++ Operator Overloading


 Unary operator overloading
 Binary operator overloading

What is operator overloading?


In C++, we can change the way operators work for user-defined types like objects and structures.
This is known as operator overloading.

Operators that do not allow overloading:


There are 4 operators that cannot be overloaded in C++. They are:
1. :: (scope resolution)
2. . (member selection)
3. .* (member selection through pointer to function)
4. ?: (ternary operator)

Types of operators:
There are two types of operator overloading:
1. Unary operator overloading
2. Binary operator overloading
Introduction to C++ Operator Overloading:
In C++, we can define how operators behave for user-defined types like class and structures For
example,
The + operator, when used with values of type int, returns their sum. However, when used with
objects of a user-defined type, it is an error.
In this case, we can define the behavior of the + operator to work with objects as well.
This concept of defining operators to work with objects and structure variables is known as
operator overloading.

Syntax for C++ Operator Overloading:


The syntax for overloading an operator is similar to that of function with the addition of the
operator keyword followed by the operator symbol.
returnType operator symbol (arguments) {
... .. ...
}
Here,
returnType - the return type of the function
operator - a special keyword
symbol - the operator we want to overload (+, <, -, ++, etc.)
arguments - the arguments passed to the function
Overloading the Binary + Operator:
Overloading -- as a Postfix Operator:
Add 1st object and 2nd object and store the result in 3rd object
Virtual Function:
LAB Task
Task#1: Subtract the two complex numbers by using objects through binary operator
overloading.
Task#2: Create a class called Book. The class will contain data members for name, publisher,
author and price. Your task is to create two Book objects in the main and provide different
values to each object. Then you will need to overload the operator:
 + operator to compute the sum of prices of two book objects.
void operator + (Book b) { }
 operator to find a book having higher price
void operator > (Book b) { }
Task#03:
Write a code to assign value of object to another object using operator overloading? Assume
class three different typed class data member?
Task #04:
Write a c++ program to Multiple two objects of class calculator which have two data members

Task #05:
Write a C++ code to Overloading Extraction and Insertion Operators?

You might also like