0% found this document useful (0 votes)
6 views16 pages

Object Oriented Programming 7 + 8

The document discusses polymorphism in object-oriented programming, particularly in C++, highlighting its two types: compile-time (static) and run-time (dynamic) polymorphism. It explains method and operator overloading, including unary and binary operator overloading, and provides examples of how these concepts are implemented in C++. Additionally, it covers function overriding as a means of achieving run-time polymorphism and poses several questions related to the topic.

Uploaded by

zoryavecer79
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)
6 views16 pages

Object Oriented Programming 7 + 8

The document discusses polymorphism in object-oriented programming, particularly in C++, highlighting its two types: compile-time (static) and run-time (dynamic) polymorphism. It explains method and operator overloading, including unary and binary operator overloading, and provides examples of how these concepts are implemented in C++. Additionally, it covers function overriding as a means of achieving run-time polymorphism and poses several questions related to the topic.

Uploaded by

zoryavecer79
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/ 16

OBJECT

ORIENTED
PROGRAMMING
Lecture: 9 + 10
Instructor: Tayyba Khalid
Content:

Polymorphism: • Method overloading


• Types of polymorphism • Introduction to operator
• Compile Time polymorphism overloading
(Static/Early Binding) • Unary Operator overloading (prefix
• Run Time polymorphism & postfix)
(Dynamic/Late Binding)
Compile Time polymorphism
Polymorphism

The word polymorphism means having many forms.

A real-life example of polymorphism is a person who at the same time


can have different characteristics. A man at the same time is a father,
a husband, and an employee. So, the same person exhibits different
behaviour in different situations. This is called polymorphism.
■ In C++, polymorphism concept can be applied to functions and
operators. A single function can work differently in different situations.
Types of Polymorphism

Polymorphism in C++ can be classified into two types:

• Compile-time Polymorphism

• Runtime Polymorphism
Compile-Time Polymorphism

■ Also known as early binding and static polymorphism, in compile-time


polymorphism, the compiler determines how the function or operator will
work depending on the context.
■ This type of polymorphism is achieved by function overloading or operator
overloading.

■ Function Overloading
Function overloading is a feature of object-oriented programming where two
or more functions can have the same name but behave differently for
different parameters.
Explanation already discussed
Operator Overloading

■ C++ has the ability to provide the operators with a special meaning for
particular data type, this ability is known as operator overloading.

■ Return_Type classname :: operator op(Argument list)


{
Function Body
}
• Return_Type is the value type to be returned to another object.

• operator op is the function where the operator is a keyword.

• op is the operator to be overloaded.


Types of Operator overloading

Operator Overloading can be done by using two approaches, i.e.

1. Overloading Unary Operator.

2. Overloading Binary Operator.


Operators that can be Overloaded in
C++

• Unary operators (+, -, ++, —)


• Binary operators (+, -, *, /, %)

■ The operator that cannot be overload:

■ Scope resolution operator ::


■ Member Selection (. Dot operator)
■ Member pointer (*)
■ Ternary operator (?:)
Unary Operator Overloading
Used to overload operators like ++, --, -, etc., for a class.
In the unary operator function, no arguments should be
passed.

It works only with one class object.

It is the overloading of an operator operating on a single


operand.

Two ways:
■ Prefix Unary Operator Overloading
■ Postfix Unary Operator Overloading
Prefix Unary Operator
Overloading
■ class Count {
■ int value;
■ public:
■ Count(){
■ Value = 0;
■ }
■ void operator++() { // Prefix
■ ++value;
■ }

■ void display() {
■ cout << "Value: " << value << endl;
■ }
■ };
Postfix Unary Operator
Overloading
■ class Count {
■ int value;
■ public:
■ Count() {
■ Value = 10;
■ }
■ void operator++(int) { // Postfix
■ value++;
■ }
■ void display() {
■ cout << "Value: " << value << endl;
■ }
■ };
Binary Operator Overloading
Binary operators are operators that work on two operands, like:
•+, -, *, /, ==, !=, etc.
•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.
■ // C++ Program to
Demonstrate
// This is automatically called when '+' is used with
■ // Operator Overloading // between two Complex objects
■ #include <iostream> Complex operator+(Complex const& obj)
{
■ using namespace std; Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
■ class Complex {
return res;
■ private: }
void print() { cout << real << " + i" << imag << '\n'; }
■ int real, imag;
};

■ public: int main()


{
■ Complex(int r = 0, int i = 0) Complex c1(10, 5), c2(2, 4);
■ { Complex c3 = c1 + c2;
c3.print();
■ real = r; }
■ imag = i;
■ }
Runtime Polymorphism

■ Also known as late binding and dynamic polymorphism, the


function call in runtime polymorphism is resolved at runtime in
contrast with compile time polymorphism, where the compiler
determines which function call to bind at compilation.
■ Runtime polymorphism is implemented using function overriding with
virtual functions.
Function Overriding

■ Function Overriding occurs when a derived class defines one or


more member functions of the base class.
■ That base function is said to be overridden.
■ The base class function must be declared as virtual function for
runtime polymorphism to happen.
■ Comparatively slower execution rate.
■ Involves inheritance.
Questions:
1. Why is polymorphism important in software development?
2. How does polymorphism support code reusability and flexibility?
3. How does compile-time polymorphism differ from run-time polymorphism?
4. Give an example scenario where compile-time polymorphism is preferred.
5. What are the rules for method overloading in a class?
6. Why would you want to overload an operator?
7. How is the prefix form of a unary operator different from the postfix form?
8. How do you overload the increment (++) operator in prefix or postfix form?
9. How does method overriding enable run-time polymorphism?

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