0% found this document useful (0 votes)
40 views20 pages

Module 4 OOPS-II Part

This document discusses object oriented concepts including nested classes, polymorphism, function overloading, operator overloading, function overriding, virtual functions, early binding and late binding. It defines nested classes as classes declared within other enclosing classes. Polymorphism is defined as an operation exhibiting different behaviors depending on the types of data used. Function overloading and operator overloading achieve compile-time polymorphism, while function overriding and virtual functions achieve runtime polymorphism through late binding. Early binding refers to function calls resolved at compile-time, while late binding refers to calls resolved at runtime.

Uploaded by

PRATHAM JAIN
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)
40 views20 pages

Module 4 OOPS-II Part

This document discusses object oriented concepts including nested classes, polymorphism, function overloading, operator overloading, function overriding, virtual functions, early binding and late binding. It defines nested classes as classes declared within other enclosing classes. Polymorphism is defined as an operation exhibiting different behaviors depending on the types of data used. Function overloading and operator overloading achieve compile-time polymorphism, while function overriding and virtual functions achieve runtime polymorphism through late binding. Early binding refers to function calls resolved at compile-time, while late binding refers to calls resolved at runtime.

Uploaded by

PRATHAM JAIN
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/ 20

Module IV: Object Oriented Concepts

Nested Classes
 A nested class is a class which is declared in another enclosing class. A nested class is a
member and as such has the same access rights as any other member. The members of
an enclosing class have no special access to members of a nested class; the usual
access rules shall be obeyed.
 For example, program 1 compiles without any error and program 2 fails in compilation.
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 person at the same time can have different characteristic. Like a man at the same
time is a father, a husband, an employee. So the same person posses different
behaviour in different situations. This is called polymorphism.
 An operation may exhibit different behaviours in different instances. The behaviour
depends upon the types of data used in the operation.

 https://www.geeksforgeeks.org/cpp-polymorphism/
 1. Compile-Time Polymorphism
 This type of polymorphism is achieved by function overloading or operator
overloading.

 2. Runtime Polymorphism
 This type of polymorphism is achieved by Function Overriding.
 Late binding and dynamic polymorphism are other names for runtime polymorphism.
 The function call is resolved at runtime in runtime polymorphism.
Function Overloading
 Suppose we have to write a function to add some integers, some times there are 2
integers, some times there are 3 integers. We can write the Addition Method with
the same name having different parameters, the concerned method will be called
according to parameters.
Operator Overloading
 Operator overloading is a compile-time polymorphism. It is an idea of giving
special meaning to an existing operator in C++ without changing its original
meaning.
Example:
        int a;
      float b,sum;
      sum=a+b;
 Here, variables “a” and “b” are of types “int” and “float”, which are built-in
data types. Hence the addition operator ‘+’ can easily add the contents of
“a” and “b”. This is because the addition operator “+” is predefined to add
variables of built-in data type only.
 Now, consider another example
 class A
{
 };
 int main()
{
      A   a1,a2,a3;
       a3= a1 + a2;
       return 0;
}
 In this example, we have 3 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 “+” to 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 “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 really does not change their
original meaning; instead, they have been given additional meaning along with
their existing ones.
 Some operators can’t be overloaded such as scope resolution operator, class
accessing operator, sizeof operator, ternary operator.
Function Overriding
 Function Overriding occurs when a derived class has a definition for one of
the member functions of the base class. That base function is said to be
overridden.
 https://www.geeksforgeeks.org/function-overriding-in-cpp/#:~:text=Real%2D
Life%20Example%20of%20Function%20Overriding&text=Another%20Developme
nt%20real%2Dlife%20example,%2C%20PNB%2C%20ICICI%2C%20etc
.
Virtual Function
 A virtual function is a member function that is declared in the base class using the
keyword virtual and is re-defined (Overridden) in the derived class.
 Some Key Points About Virtual Functions:
 Virtual functions are Dynamic in nature. 
 They are defined by inserting the keyword “virtual” inside a base class and are always
declared with a base class and overridden in a child class
 A virtual function is called during Runtime
Early binding and Late binding
 https://www.geeksforgeeks.org/early-binding-late-binding-c/

 Function call decided at compile time is called early binding.


 Function call decided at run time is called late binding.
Difference between OOPs & POP
Parameter Procedure Oriented Programming Object Oriented Programming

Divided In POP, program is divided into small In OOP, program is divided into parts
Into parts called functions. called objects.

Importance In POP, Importance is not given In OOP, Importance is given to the data
to data but to functions as well rather than procedures or functions
as sequence of actions to be done. because it works as a real world.

Approach POP follows Top Down approach. OOP follows Bottom Up approach.

Access POP does not have any access specifier. OOP has access specifiers named Public,
Specifiers Private, Protected, etc.

Data In POP, Data can move freely from In OOP, objects can move and
Moving function to function in the system. communicate with each other through
member functions.
Difference between OOPS & POP
Parameter Procedure Oriented Programming Object Oriented Programming

Data Access In POP, Most function uses Global data In OOP, data can not move easily from
for sharing that can be accessed freely function to function, it can be kept public
from function to function in the system. or private so we can control the access of
data.

Data Hiding POP does not have any proper way for OOP provides Data Hiding so
hiding data so it is less secure. provides more security.
Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the
form of Function Overloading and
Operator Overloading.

Examples Example of POP are : C, VB, Example of OOP are : C++, JAVA,
FORTRAN, Pascal. VB.NET, C#.NET.

You might also like