0% found this document useful (0 votes)
4 views

Function and polymorphism

The document covers key concepts of Object-Oriented Programming (OOP) including functions, polymorphism, and memory management. It explains inline functions, friend functions, static variables, constructor overloading, and operator overloading, along with their rules and examples. Additionally, it distinguishes between virtual and pure virtual functions, and discusses compile-time versus runtime polymorphism.

Uploaded by

harshvardhanagam
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)
4 views

Function and polymorphism

The document covers key concepts of Object-Oriented Programming (OOP) including functions, polymorphism, and memory management. It explains inline functions, friend functions, static variables, constructor overloading, and operator overloading, along with their rules and examples. Additionally, it distinguishes between virtual and pure virtual functions, and discusses compile-time versus runtime polymorphism.

Uploaded by

harshvardhanagam
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/ 15

FY- OOP- Programming for Problem Solving

Prof. Sarang Joshi


OOP Fundamentals: Functions and Polymorphism
(F015)
Module III
Functions: inline, friends, static
⚫ Keyword: inline
⚫ The inline declaration runtime copies the function
code to a position of a function call to improve the
speed rather than calling a function and stacking
the calling address or return address.
⚫ For example, if a inline function MyFunc has a code
say, cout << “ Count is 5” << endl; then at compile
time function call is replace by the function code.
⚫ Example
Functions: inline, friends, static
⚫ Keyword: friend
⚫ A friend function in a class, can access private and
protected members of the class.
⚫ Friend class can access private, protected
members of the friend classes.

⚫ Example Friend Function


⚫ Example Friend Class
Functions: inline, friends, static
⚫ Keyword: static, it indicates, static allocation of
memory.
⚫ Conventionally, when a class object is created,
every time new memory is allocated but with
keyword static one memory is allocated it is
maintained.
⚫ In C++, Static keyword has different meaning i.e.,
static can be a literal, a method, a class methods,
properties methods or event, e.g. clear screen
event may not change.
Functions: inline, friends, static
⚫ Advantage of Keyword: static, is its memory efficiency being static, no
memory is allocated again and again like instantiating using object.
⚫ Static variable in a function: advantage: Memorization for future reference.
⚫ Static variable in a class: Advantage: Arbitration logic of class,
Configuration count, Sharing data with different instances.
⚫ Static member function in a class: Advantage: Shared common
initialization variables, login, debug, helper static functions, Static features or
functions like clear screen
⚫ Global static variable: These are globally declared out side any class or
function but accessible only in a code file where it is defined. Advantages:
Global counters, store default settings, shared across functions in a file.
Avoids frequent loading and destruction being static.
⚫ Example code
Polymorphism: Constructor Overloading
⚫ Constructor Overloading: means constructor
having different morphs typically, initializing the
private members, such constructors can take no
argument to multiple arguments or input parameters
with class name is used as constructor name.
⚫ This is helpful in constraint based initialization for
example, Unicode keyboard switches from say
English to Marathi language script.

Polymorphism: Operator Overloading
⚫ Operator Overloading: It means functions of the validly
selected operators can be changed from predefined
operations or functions, keyword: operator
⚫ Unary Operator overloading and
⚫ Binary Operator overloading
⚫ Rules for operator overloading
− The operators :: * . : ? can not be overloaded
− The operators () [] -> and = can not be overloaded
when a function is a friend function
Polymorphism: Operator Overloading
⚫ Operator Overloading:
⚫ Rules for operator overloading
− In case of non-static member function, binary operator
should have only one arguments while, unary operator
must have no arguments.
− In case of friend function, binary operator should have only
two arguments while, unary operator must have one
argument.
− The operator function must be either non-static member
function or global free function or a friend function.
− Example
Polymorphism: Operator Overloading
⚫ Unary, Binary Operator Overloading:
⚫ Rules for operator overloading
− In case of non-static member function, binary operator
should have only one arguments while, unary operator
must have no arguments.
− In case of friend function, binary operator should have only
two arguments while, unary operator must have one
argument.
− The operator function must be either non-static member
function or global free function or a friend function.
− Unary operator overloading Example
Polymorphism: insertion and extraction Overloading
⚫ cin and cout Operator Overloading:
⚫ cout(<<) and cin(>>) are stream insertion and extraction
operators respectively.
⚫ cout is an object of ostream class and cin is an object of
istream class.
⚫ Rule: These operators must be overloaded as a global
function.
⚫ This is because, in operator overloading, if an operator is
overloaded as a member, then it must be a member of an
object associated with it on left hand side. For other example,
In class line, dashed line, dotted line are overloaded and are
member functions of class object of line.
Polymorphism: insertion and extraction Overloading
ref: geeksforgeeks

⚫ cin and cout Operator Overloading:


⚫ cout(<<) is an object of ostream class and cin(>>) is an
object of istream class.
⚫ Rule: These operators must be member of ostream and
istream class respectively, but its BAD c++
programming.
⚫ Hence, these operators are overloaded as a global
functions with two parameters cout and object of a user
defined class.
⚫ Operator Overloading Example
Virtual and Pure virtual function
⚫ A virtual function is written in a base class and rewritten or
redefined in a derived class
⚫ A virtual function can be called using a pointer or reference to a
base class using derived class object.
⚫ A pure virtual function is a abstract function which is declared
only by assigning zero in declaration.
⚫ These functions can not be global or static.
⚫ These function implement runtime polymorphism.
⚫ The declaration of these function remain unchanged during the
runtime of the program.
Virtual Vs Pure virtual function
⚫ Virtual Function ⚫ Pure Virtual Function

⚫ Its a declared and defined ⚫ Its only declared in base class


member function of base class and defined in derived class
and can be redefined in ⚫ Base class holding pure virtual
derived class function is always abstract.
⚫ Classes with virtual function ⚫ The base class becomes abstract
need not be abstract class and can not be instantiated.

⚫ Such base class can be ⚫ If not defined and assigned to


instantiated i.e. class object zero in derived class compiler
error occurs.
can be created.
⚫ Explain difference between
⚫ If not defined in derived class interface classes and abstract
no compilation error occurs. classes.
⚫ Example, Students to Give ⚫ Example
Runtime and compile time polymorphism
⚫ Polymorphism means different roles of given object, for example, A
person can be a brother, A son, an employee or an employer or a
teacher or a principal etc. The results in different functionality, data
access restrictions etc.
⚫ Compile time polymorphism: When a relationship between definition of
different functions and their calls is determined during compile time,
then it is called as compile time polymorphism. It is also called as static
or early binding polymorphism.
− Function overloading, operator overloading

⚫ It is less preferred for complex tasks since all methods and associated
data becomes open during compile time
⚫ Debugging is tedious

Runtime and compile time polymorphism
⚫ Run time Polymorphism: The compiler resolves the
object at run time( in a memory segment allocated at
runtime) and then decides which function call should
be associated with that object at run time.
⚫ It is also called as dynamic or late binding
polymorphism.
⚫ It is implemented using function overloading or virtual
functions.

⚫ Example using virtual function

You might also like