[go: up one dir, main page]

0% found this document useful (0 votes)
13 views26 pages

Linux Based PPT Presentation Commands and More

The document provides an overview of operator overloading and type conversion in C++. It explains how operator overloading allows developers to redefine operator behavior for user-defined data types and outlines the rules and methods for implementing it. Additionally, it covers the different types of type conversions supported in C++, including implicit, explicit, and conversions using constructors and operator overloading.

Uploaded by

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

Linux Based PPT Presentation Commands and More

The document provides an overview of operator overloading and type conversion in C++. It explains how operator overloading allows developers to redefine operator behavior for user-defined data types and outlines the rules and methods for implementing it. Additionally, it covers the different types of type conversions supported in C++, including implicit, explicit, and conversions using constructors and operator overloading.

Uploaded by

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

Object-oriented programming

C++

Dr. Irtiqa Amin


Assistant Professor
Learning
Outcomes

• Covering the basic concepts of

Operator Overloading

Type Conversion
Operator overloading in C++

 Operator Overloading in C++ is a feature that allows developers to redefine the way
operators (like +, -, *, ==, etc.) work for user-defined data types (such as classes and
structs).

 By default, C++ operators work with built-in data types. But if we create your class
(e.g., Complex, Matrix, Vector), we want these operators to behave appropriately.
Operator overloading lets you use natural syntax with your custom objects.

You can overload operators in two ways

 As member functions

OR

 As a friend functions
Syntax

Example
Explanation

 In the previous example, we have three variables “a1”, “a2”, and “a3” of type “class A”.
Here we are trying to add two objects “a1” and “a2”, which are of user-defined type i.e.
of type “class A” using the “+” operator. This is not allowed because the addition
operator “+” is predefined to operate only on built-in data types. But here, “class A” is a
user-defined type, so the compiler generates an error. This is where the concept of
“Operator overloading” comes in.

 Now, if the user wants to make the operator “+” add two class objects, the user has to
redefine the meaning of the “+” operator such that it adds two class objects. This is done
by using the concept of “Operator overloading”. So, the main idea behind “Operator
overloading” is to use C++ operators with class variables or class objects. Redefining the
meaning of operators does not change their original meaning; instead, they have been
given additional meaning along with their existing ones.
A C++ program demonstrating operator overloading of the + operator using a
custom Complex class

 c1 + c2 calls the overloaded operator+ function.


 It returns a new Complex object that contains the
sum of the real and imaginary parts.
 The result is stored in c3 and displayed.
Operators That Cannot Be
Operators That Can Be
Overloaded
Overloaded
Operator Reason
 Arithmetic: +, -, *, /, %
:: Scope resolution
 Relational: ==, !=, <, >, <=, >= . Member access
.* Pointer-to-member access
 Logical: &&, ||, !
sizeof Compile-time operator
 Bitwise: &, |, ^, ~, <<, >> typeid Runtime type info
?: Ternary conditional
 Assignment: =, +=, -=, etc.
const_cast,
 Subscript [], Function call (), static_cast,
reinterpret_ca Casting operators
st,
Member access -> dynamic_cast
Rules for Operator Overloading

 Only Existing Operators Can Be Overloaded


You can only overload operators that already exist in C++. You cannot create
new operators.
 At Least One Operand Must Be a User-Defined Type
Operator overloading must involve a class or struct.
You cannot overload operators for built-in types like:

 Cannot Change Operator Precedence or Associativity


You cannot change how operators associate (left-to-right/right-to-left) or their
precedence.
 Default Arguments Are Not Allowed in Overloaded Operators
When overloading an operator function, do not use default arguments.
 Friend Functions vs. Member Functions
Use a member function if the left-hand operand is the object of the class.
Use the friend function if both operands are objects of different classes or if
the left operand isn’t a class object.
 Overloaded Operators Must Follow Expected Behavior
Although you can redefine the functionality, it's best practice to keep the
behavior intuitive and not misleading (e.g., + should still mean addition-like
behavior).
 Unary and Binary Operators Have Different Function Signatures
Unary operators (e.g., -a, !a) take no arguments when defined as member
functions.
Binary operators (e.g., a + b) take one argument (the right-hand operand).
 Return by Value for Most Operators

Operators like +, -, and * typically return by value, not by

reference, to avoid unwanted side effects.

 Do Not Overuse Operator Overloading

Use operator overloading only when it improves code

readability and when the meaning of the operator is clear for

the user-defined type.


Unary Operator Overloading (Member Function)
Description
Unary operators work on one operand, like:
 - (negation)
 ++ (increment)
 -- (decrement)
They can be overloaded as member functions with no
arguments.
Example: Overloading - to negate values
Binary Operator Overloading (Member Function)

Description

Binary operators operate on two operands, like:

 +, -, *, /, etc.

When overloaded as member functions, the left operand is the calling

object, and the right operand is passed as a parameter.


Example: Overloading + for adding two Complex numbers
Binary Operator Overloading (Friend Function)
Description

A friend function is used when

 You want to overload an operator, but the left operand is not an object of your

class.

 Or when you want the operator to access private members of both operands.
Example: Overloading + for adding two Distance objects
Difference

Operator Type Function Type Syntax in Class Arguments

Unary Member Function Type operator op() 0

Type operator
Binary Member Function 1
op(Type)

friend Type
Binary Friend Function operator op(Type, 2
Type)
Type conversion in C++

 Type Conversion in C++ refers to changing the data type of a variable or

expression into another data type. This is commonly done when assigning

values between different types (like int to float, or float to int).


 C++ supports four types of type conversions
 Implicit conversion
 Explicit
 Conversion using operators
 Conversion using operator overloading.
Implicit Type Conversion (Automatic)

Also called type promotion, it is automatically performed by the

compiler when data types are compatible.

 Performed automatically

 Smaller types are converted to larger types (int → float, char

→ int, etc.)
Explicit Type Conversion (Casting)

The programmer manually converts one type into


another using cast operators.
 Can use C-style cast: (type)
 Or C++ cast: static_cast<type>(value)
Conversion using a constructor

This is used when a non-class type (like int) is converted into


a class type using a constructor.
Type Conversion Direction Who Performs It? Example

int a = 5; float b =
Implicit Built-in → Built-in Compiler
a;

Explicit (Casting) Built-in → Built-in Programmer int b = (int)5.6;

Constructor-Based Built-in → Class Constructor Distance d = 50;

Operator Conversion
Class → Built-in int x = d;
Overloading operator function
Basic-to-Class Conversion

Behind the scenes

Basic-to-class conversion occurs when a basic data type (like int, float, etc.) is
converted to a class object using a constructor.
Differences
Feature Basic-to-Class Class-to-Basic Class-to-Class

Converts a basic type Converts a class Converts one class


Definition (e.g., int) into a class object into a basic type object into
object type (e.g., int) another class type

Conversion
Constructor with Constructor or
Implemented Using operator (e.g.,
one argument conversion operator
operator int())

Member constructor
Member function
Function Type Member constructor or conversion
(operator type())
operator

Yes (if not marked


Automatic? Yes Yes
explicit)

Distance d = 100; → int x = d; → operator ClassB b = a; →


Example Code
Distance(int) int() ClassB(ClassA)
Task to do!

 A program to demonstrate the working class to basic

type conversion and one class to another.


That’s all for now…

You might also like