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

Lec-10 Polymorphism

The document provides an overview of Object-Oriented Programming concepts in C++, focusing on encapsulation, data abstraction, and polymorphism. It explains encapsulation as the bundling of data and methods, data abstraction as hiding unnecessary details, and polymorphism as the ability to present the same interface in different forms. Additionally, it covers compile-time and runtime polymorphism, including function and operator overloading with examples.

Uploaded by

Aksjsy
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)
6 views32 pages

Lec-10 Polymorphism

The document provides an overview of Object-Oriented Programming concepts in C++, focusing on encapsulation, data abstraction, and polymorphism. It explains encapsulation as the bundling of data and methods, data abstraction as hiding unnecessary details, and polymorphism as the ability to present the same interface in different forms. Additionally, it covers compile-time and runtime polymorphism, including function and operator overloading with examples.

Uploaded by

Aksjsy
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/ 32

Object Oriented

Programming
Mohona Ghosh
Encapsulation
• Encapsulation in C++ is defined as the wrapping up of data and
functions that manipulate them together in a single unit.

The process of implementing encapsulation can be sub-


divided into two steps:
1.Creating a class to encapsulate all the data and methods
into a single unit.

2.Hiding relevant data using access specifiers.


Features of Encapsulation
1.We can not access any function from the class directly. We need an object to
access that function that is using the member variables of that class.

2.The function which we are making inside the class must use only member
variables, only then it is called encapsulation.

3.If we don’t make a function inside the class which is using the member
variable of the class then we don’t call it encapsulation.

4.Encapsulation improves readability, maintainability, and security by grouping


data and methods together.

5.It helps to control the modification of our data members.


Data Abstraction
• Encapsulation also leads to data abstraction.
• Data abstraction refers to providing only essential information about
the data to the outside world, ignoring unnecessary details or
implementation.
• Consider a real-life example of a man driving a car. The man only
knows that pressing the accelerator will increase the speed of the car
or applying brakes will stop the car but he does not know how on
pressing the accelerator the speed is actually increasing, he does not
know about the inner mechanism of the car or the implementation of
the accelerator, brakes, etc in the car. This is what abstraction is.
How to implement Data Abstraction
• We can implement Abstraction in C++ using classes.
• The class helps us to group data members and member functions using
available access specifiers.
• A Class can decide which data member will be visible to the outside world
and which is not.
• Members declared as public in a class can be accessed from anywhere in the program.
• Members declared as private in a class, can be accessed only from within the class. They
are not allowed to be accessed from any part of the code outside the class.

• Another type of abstraction in C++ can be header files. For example, consider
the pow() method present in math.h header file. Whenever we need to
calculate the power of a number, we simply call the function pow() present in
the math.h header file and pass the numbers as arguments without knowing
the underlying algorithm according to which the function is actually
calculating the power of numbers.
#include<iostream>
using namespace std; void color()
{
class Vehicle cout<<"Red/GREEN/Silver\n";
{ }
private: void cost()
void piston() {
{ cout<<"Rs. 60000 to 900000\n";
cout<<"4 piston\n"; }
} void oil()
{
void manWhoMade() cout<<"PETROL\n";
{ }
cout<<"Markus Librette\n"; };
} int main()
public: {
void company()
{ Vehicle obj;
cout<<"GFG\n"; obj.company();
} obj.model();
void model() obj.color();
{ obj.cost();
cout<<"SIMPLE\n"; obj.oil();
} }
Advantages of Data Abstraction
• Can change the internal implementation of the class independently
without affecting the user.

• Helps to increase the security of an application or program as only


important details are provided to the user.

• It reduces the complexity as well as the redundancy of the code, therefore


increasing the readability.

• New features or changes can be added to the system with minimal


impact on existing code.
Polymorphism
• The word polymorphism means having many forms.
• In simple words, we can define polymorphism as the ability of a
message to be displayed in more than one form.
• A real-life example of polymorphism, a person at the same time can
have different characteristics.
• Like a man at the same time is a father, a husband, an employee. So
the same person posess different behavior in different situations.
• This is called polymorphism.
• Polymorphism is considered as one of the important features of
Object Oriented Programming.
Polymorphism
• In C++ polymorphism is mainly divided into two types:
• Compile time Polymorphism
• Runtime Polymorphism
• Runtime polymorphism – we
will look into later when we
study inheritance
Compile Time Polymorphism
• Compile-time polymorphism is achieved through function overloading and operator
overloading.
• 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 function 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).
• You invoke the overloaded functions by matching the number and type of arguments.
• The information is present during compile-time.
• This means the C++ compiler will select the right function at compile time.
• 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.
Function Overloading
Function Overloading
Function Overloading
• Overloaded functions may or may not have different return types but
they must have different arguments. For example,
class Calculator {
public:
int add(int a, int b) { return a + b; } // Adds two integers
double add(double a, double b) { return a + b; } // Adds two doubles
};

int main() {
Calculator calc;
int result1 = calc.add(5, 3); // Calls add(int, int)
double result2 = calc.add(2.5, 1.5); // Calls add(double, double)
return 0;
}

Example demonstrating function overloading using a Calculator class that contains overloaded add
function
#include <iostream>
using namespace std;

class Display {
public: int main() {
// Function to display an integer Display obj;
void show(int a) {
cout << "Integer: " << a << endl; obj.show(25);
} obj.show(3.14);
obj.show("Hello, World!");
// Function to display a double
void show(double a) { return 0;
cout << "Double: " << a << endl; }
}

// Function to display a string


void show(string a) {
cout << "String: " << a << endl; Example demonstrating function overloading using a Display
} class that contains overloaded show methods
};
Operator overloading
Operator overloading Syntax
Operator
Overloading
Overloading unary operators
In 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.
Overloading binary operators
• In binary operator overloading function, there should be one
argument to be passed. It is overloading of an operator operating on
two operands.
#include <iostream>
using namespace std; int main() {
Number num1, num2, result;
class Number { num1.value = 10;
public: num2.value = 5;
int value; result = num1 - num2; // Using overloaded - operator

// Overloading the - operator cout << "num1: ";


Number operator - ( Number obj) { num1.display();
Number temp; cout << "num2: ";
temp.value = value - obj.value; num2.display();
return temp; cout << "Result (num1 - num2): ";
} result.display();

// Display function return 0;


void display() const { }
cout << "Value: " << value << endl;
}
}; Program that demonstrates operator overloading for the -
(subtraction) operator
Assignment
operator
overloading
#include <iostream>
using namespace std;

class Number { int main() {


public: Number num1, num2;
int value; num1.value = 10;
num2 = num1; // Using overloaded assignment operator
// Overloading the assignment operator
Number operator=(Number obj) { cout << "num1: ";
Number temp; num1.display();
temp.value = obj.value; cout << "num2 (after assignment): ";
return temp; num2.display();
}
return 0;
// Display function }
void display() const {
cout << "Value: " << value << endl;
}
};
#include <iostream> int main() {
using namespace std; Number num;
num.value = 10;
class Number {
public: cout << "Before increment: ";
int value; num.display();
// Overloading the postfix ++ operator Number temp = num++; // Using overloaded postfix ++
Number operator++() { operator
Number temp;
temp.value = value; cout << "After postfix increment: ";
value++; num.display();
return temp; cout << "Returned value (before increment): ";
} temp.display();
// Display function return 0;
void display() const { }
cout << "Value: " << value << endl;
}
}; Before increment: Value = 10 // Num. value
After postfix increment: Value = 11 // Num.value
Returned value (before increment): Value = 10 // temp.value
Operators that cannot be overloaded …

You might also like