SlideShare a Scribd company logo
6
Most read
7
Most read
8
Most read
Polymorphism
In
C++ Presented by:
Vishesh Kumar jha
04620602018
BCA 3rd Semester 1st Shift
jhavishesh123@gmail.com
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
(Affiliated to Guru Gobind Singh Indraprastha University,Delhi)
Ranked “A+” Institution of GGSIPU, Delhi
Recognised under section 2(f) of the UGC Act, 1956
NAAC Accredited “B++” Grade Institution
Ranked “A+” Institution by SFRC, Govt. of NCT of India
POLYMORPHISM
 Polymorphism is derived from 2Greek words: poly and
morphs. The word "poly" means many and morphs
means forms.
Polymorphism is the ability to use an operator or function
in different manner,it gives the special meaning to the
function or operator and also it refers to the
codes,operations,or objects that behave differently in
different context.
Real life example of Polymorphism
Suppose if you are in class room that time you behave
like a student, when you are in market at that time you
behave like a customer, when you at your home at that
time you behave like a son or daughter, Here one person
have different-different behaviors.
Type of Polymorphism
 There are mainly five types of polymorphism 1.Ad-hoc 2.Parametric 3.Sub-typing
4.Row 5.Polytypism.
 Static polymorphism is also known as early binding and compile-time polymorphism.
In static polymorphism memory will be allocated at compile-time.
Dynamic polymorphism is also known as late binding and run-time polymorphism. In
dynamic polymorphism memory will be allocated at run-time.
Polymorphism
Static
Function
Overloading
Operator
Overloading
Dynamic
Virtual
Functions
Method Overloading
Whenever same method name is exiting multiple times in the same class with
different number of parameter or different order of parameters or different types of
parameters is known as method overloading.
In next example method "sum()" is present in Addition class with same namebut
with different signature or arguments.
Function Overloading Example
class Addition {
public: void sum(int a, int b)
{
//output :-a+b : 30
//output :-a+b+c :60
cout<<"a+b :"<<a+b; }
void sum(int a, int b, int c)
{
cout<<"a+b+c :"<<a+b+c; }
};
int main() {
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20,30);
}
Operator Overloading
The process of making an operator to exhibit different behaviors in different
instances is known as operator overloading.
Only predefined operator can be overloaded.
Some operators which can’t be overloaded such as scope resolution
operator , sizeof operator,class member acess and conditional operator etc.
Types Of Operator Overloading
Unary operator overloading.
These Operators have only single operand.
Examples:- ++,--,~,!
Binary operator overloading.
These operators can have two or more operands.
Examples:-+,-,*,/,%,^,=,==,+=,&,&& etc
Operator Overloading Example
void main()
{
clrscr();
test p1;
p1.gt(-10,-90,-23);
p1.dp();
cout<<"Values after operator overloading"<<endl;
-p1;
p1.dp();
getch();
}
#include<iostream.h>
#include<conio.h>
class test{
int a,b,c;
public:
void get(int x,int y,int z);
void operator -();
void dp();
};
void test::gt(int x,int y,int z)
{a=x;
b=y;
c=z;}
void test::operator-()
{ a=-a;
b=-b;
c=-c;}
void test::dp()
{cout<<"Value of a="<<a<<endl;
cout<<"Value of b="<<b<<endl;
cout<<"Value of c="<<c<<endl;
}
Virtual Function
A virtual function is a member function that is declared as virtual within abase
class and redefined by a derived class.
To create virtual function, precede the base version of function’s declaration with
the keyword virtual.
 Here we use a pointer to the base class to refer to all the derived objects.
The function name and type signature should be same for both base and derived
version of function.
Virtual Function Example
class Base {
public: virtual void show() {
cout<<"Content of base class.n"; }};
class derived:publicA
{ public:
void show() {
cout<<"Content of derived class.n"; }};
int main() {
Base *a //Base class pointer
derivedp1; //Derived class
a= &p1;
a->show(); //Late Binding Occurs
getch();
return 0; }
Polymorphism In c++

More Related Content

PDF
Classes and objects
PDF
Operator overloading
PDF
Constructor and Destructor
PPTX
OOPS Basics With Example
PPT
C++ classes tutorials
PPTX
Polymorphism in C++
PPTX
Constructors and destructors
PPTX
OOP Unit 2 - Classes and Object
Classes and objects
Operator overloading
Constructor and Destructor
OOPS Basics With Example
C++ classes tutorials
Polymorphism in C++
Constructors and destructors
OOP Unit 2 - Classes and Object

What's hot (20)

PPTX
classes and objects in C++
PPTX
Method overloading
PDF
Function overloading ppt
PDF
Constructors and destructors
PPTX
Inline Functions and Default arguments
PPT
Class and object in C++
PPT
Operator Overloading
PPTX
07. Virtual Functions
PPTX
Inheritance in c++
PPT
Function overloading(c++)
PPTX
Polymorphism in c++(ppt)
PPTX
Inheritance in java
PPTX
Functions in c
PPTX
INLINE FUNCTION IN C++
PPT
friend function(c++)
PPTX
Operator overloading
PDF
Managing I/O in c++
PPTX
Constructor and Types of Constructors
PPTX
classes and objects in C++
Method overloading
Function overloading ppt
Constructors and destructors
Inline Functions and Default arguments
Class and object in C++
Operator Overloading
07. Virtual Functions
Inheritance in c++
Function overloading(c++)
Polymorphism in c++(ppt)
Inheritance in java
Functions in c
INLINE FUNCTION IN C++
friend function(c++)
Operator overloading
Managing I/O in c++
Constructor and Types of Constructors
Ad

Similar to Polymorphism In c++ (20)

PDF
vdocument in_polymorphism-in-cppt_python.pdf
PDF
Polymorphism
PPTX
oops.pptx
PPTX
11.C++Polymorphism [Autosaved].pptx
PDF
Unit3_OOP-converted.pdf
PDF
Polymorphism and Type Conversion.pdf pot
PPTX
Polymorphism 140527082302-phpapp01
PPTX
Polymorphism and its types
PPTX
Polymorphism
PPT
Polymorphism
PPTX
POLYMORPHISM
PPTX
21CSC101T best ppt ever OODP UNIT-2.pptx
PPTX
Polymorphism.Difference between Inheritance & Polymorphism
PPTX
Polymorphism
PDF
polymorphism.pdf
PPT
3d7b7 session4 c++
PDF
Ch-4-Operator Overloading.pdf
PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
PPTX
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
vdocument in_polymorphism-in-cppt_python.pdf
Polymorphism
oops.pptx
11.C++Polymorphism [Autosaved].pptx
Unit3_OOP-converted.pdf
Polymorphism and Type Conversion.pdf pot
Polymorphism 140527082302-phpapp01
Polymorphism and its types
Polymorphism
Polymorphism
POLYMORPHISM
21CSC101T best ppt ever OODP UNIT-2.pptx
Polymorphism.Difference between Inheritance & Polymorphism
Polymorphism
polymorphism.pdf
3d7b7 session4 c++
Ch-4-Operator Overloading.pdf
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Ad

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Institutional Correction lecture only . . .
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Business Ethics Teaching Materials for college
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Types and Its function , kingdom of life
PPH.pptx obstetrics and gynecology in nursing
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O5-L3 Freight Transport Ops (International) V1.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Week 4 Term 3 Study Techniques revisited.pptx
Cell Structure & Organelles in detailed.
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
O7-L3 Supply Chain Operations - ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Institutional Correction lecture only . . .
Supply Chain Operations Speaking Notes -ICLT Program
102 student loan defaulters named and shamed – Is someone you know on the list?
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Basic Mud Logging Guide for educational purpose
Business Ethics Teaching Materials for college
Anesthesia in Laparoscopic Surgery in India
Cell Types and Its function , kingdom of life

Polymorphism In c++

  • 1. Polymorphism In C++ Presented by: Vishesh Kumar jha 04620602018 BCA 3rd Semester 1st Shift jhavishesh123@gmail.com TRINITY INSTITUTE OF PROFESSIONAL STUDIES (Affiliated to Guru Gobind Singh Indraprastha University,Delhi) Ranked “A+” Institution of GGSIPU, Delhi Recognised under section 2(f) of the UGC Act, 1956 NAAC Accredited “B++” Grade Institution Ranked “A+” Institution by SFRC, Govt. of NCT of India
  • 2. POLYMORPHISM  Polymorphism is derived from 2Greek words: poly and morphs. The word "poly" means many and morphs means forms. Polymorphism is the ability to use an operator or function in different manner,it gives the special meaning to the function or operator and also it refers to the codes,operations,or objects that behave differently in different context.
  • 3. Real life example of Polymorphism Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person have different-different behaviors.
  • 4. Type of Polymorphism  There are mainly five types of polymorphism 1.Ad-hoc 2.Parametric 3.Sub-typing 4.Row 5.Polytypism.  Static polymorphism is also known as early binding and compile-time polymorphism. In static polymorphism memory will be allocated at compile-time. Dynamic polymorphism is also known as late binding and run-time polymorphism. In dynamic polymorphism memory will be allocated at run-time. Polymorphism Static Function Overloading Operator Overloading Dynamic Virtual Functions
  • 5. Method Overloading Whenever same method name is exiting multiple times in the same class with different number of parameter or different order of parameters or different types of parameters is known as method overloading. In next example method "sum()" is present in Addition class with same namebut with different signature or arguments.
  • 6. Function Overloading Example class Addition { public: void sum(int a, int b) { //output :-a+b : 30 //output :-a+b+c :60 cout<<"a+b :"<<a+b; } void sum(int a, int b, int c) { cout<<"a+b+c :"<<a+b+c; } }; int main() { Addition obj; obj.sum(10, 20); cout<<endl; obj.sum(10, 20,30); }
  • 7. Operator Overloading The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. Only predefined operator can be overloaded. Some operators which can’t be overloaded such as scope resolution operator , sizeof operator,class member acess and conditional operator etc. Types Of Operator Overloading Unary operator overloading. These Operators have only single operand. Examples:- ++,--,~,! Binary operator overloading. These operators can have two or more operands. Examples:-+,-,*,/,%,^,=,==,+=,&,&& etc
  • 8. Operator Overloading Example void main() { clrscr(); test p1; p1.gt(-10,-90,-23); p1.dp(); cout<<"Values after operator overloading"<<endl; -p1; p1.dp(); getch(); } #include<iostream.h> #include<conio.h> class test{ int a,b,c; public: void get(int x,int y,int z); void operator -(); void dp(); }; void test::gt(int x,int y,int z) {a=x; b=y; c=z;} void test::operator-() { a=-a; b=-b; c=-c;} void test::dp() {cout<<"Value of a="<<a<<endl; cout<<"Value of b="<<b<<endl; cout<<"Value of c="<<c<<endl; }
  • 9. Virtual Function A virtual function is a member function that is declared as virtual within abase class and redefined by a derived class. To create virtual function, precede the base version of function’s declaration with the keyword virtual.  Here we use a pointer to the base class to refer to all the derived objects. The function name and type signature should be same for both base and derived version of function.
  • 10. Virtual Function Example class Base { public: virtual void show() { cout<<"Content of base class.n"; }}; class derived:publicA { public: void show() { cout<<"Content of derived class.n"; }}; int main() { Base *a //Base class pointer derivedp1; //Derived class a= &p1; a->show(); //Late Binding Occurs getch(); return 0; }