SlideShare une entreprise Scribd logo
1  sur  11
A Class definition with data members and
member functions using which no objects are
created is called an abstract class.
It is designed only to act as a base class .
So that other classes can inherit these data
members and member functions.
#include <iostream.h>
class X
// Base Class
{ int a;
public:
void display() { cout<<a<<endl;}
};
Class Y : public X
// Derived Class
{ int b;
public:
void display()
{
cout<<a<<endl;
cout<<b<<endl; }
};
Void main()
{ Y ob;
……….
………. }
Pure virtual function can not be defined in
the base class, it has to be only declared.
Hence, a class containing pure virtual
functions cannot be used to declare any
object of that class
These type of classes are only used to
create derived classes and are called as
“Abstract base classes”
#include <iostream.h>
class base
// Base Class
{ public:
virtual void abc() //Pure virtual function declaration
};
Class derived : public base
// Derived Class
{ public:
void abc()
{
cout<< “In derived Classn”; }
};
Void main()
{ base b;

derived d;
d1.abc();
……..
}
Polymorphism is the ability to use an operator or
function in different ways.
It is achieved using overloading of functions and
overloading of operators.
Polymorphism is achieved using overloading of
functions using:
 Multiple methods
 Single interface
Multiple methods imply two or more
functions with the same name but with
different signatures.
For example, we have two overloaded add
functions as shown below:
// adds two integer values
add(int a, int b)
{
int c =a+b;
cout<<c<<endl;
}

// adds two floating values
add(float a, float b)
{
float c =a+b;
cout<<c<<endl;
}

The above two functions are called methods
With respect to previous example,
whether we find sum of two integers or two
floating point values it doesn’t matter for
us.
What is required is “to add two values”
So, it is logical to use add as the function
name. We may call two functions as
shown below:
add(10,20);
add(1.5,3.5);

//Invoke the first function
//Invoke the second add function
Polymorphism is a feature of OOP with
single interface and multiple methods.
It also means “One name and multiple
forms”.
It helps us to reduce complexity using a
single interface and multiple methods.
It indicates calling one or more functions
using same name or giving two or more
meanings to an operator.
Also called as static
binding, it binds an object to
a function call during
compilation time.
Refers to events that occur
during compile time.
Execution is much faster.
Not flexible since functions
are decided during
compilation time.
Achieved using normal
function calls, function
overloading and operator
overloading.

Also called as dynamic
binding, it binds an object to
a function call during run
time.
Refers to events that occur
during run time.
Execution is slower.
Very flexible since
executable functions are
decided during run time.
Achieved using inheritance
and virtual functions.
Abstract Base Class and Polymorphism in C++

Contenu connexe

Tendances

Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 

Tendances (20)

Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 

En vedette

Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
Princess Sam
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kumar
 

En vedette (20)

Virtual function
Virtual functionVirtual function
Virtual function
 
Uid
UidUid
Uid
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Insecure coding in C (and C++)
Insecure coding in C (and C++)Insecure coding in C (and C++)
Insecure coding in C (and C++)
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
 
C++
C++C++
C++
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 

Similaire à Abstract Base Class and Polymorphism in C++

Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
bunnykhan
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Kumar Boro
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
Niti Arora
 

Similaire à Abstract Base Class and Polymorphism in C++ (20)

Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 
ccc
cccccc
ccc
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Lab3
Lab3Lab3
Lab3
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
2nd puc computer science chapter 8 function overloading
 2nd puc computer science chapter 8   function overloading 2nd puc computer science chapter 8   function overloading
2nd puc computer science chapter 8 function overloading
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Dernier

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 

Dernier (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Abstract Base Class and Polymorphism in C++

  • 1.
  • 2. A Class definition with data members and member functions using which no objects are created is called an abstract class. It is designed only to act as a base class . So that other classes can inherit these data members and member functions.
  • 3. #include <iostream.h> class X // Base Class { int a; public: void display() { cout<<a<<endl;} }; Class Y : public X // Derived Class { int b; public: void display() { cout<<a<<endl; cout<<b<<endl; } }; Void main() { Y ob; ………. ………. }
  • 4. Pure virtual function can not be defined in the base class, it has to be only declared. Hence, a class containing pure virtual functions cannot be used to declare any object of that class These type of classes are only used to create derived classes and are called as “Abstract base classes”
  • 5. #include <iostream.h> class base // Base Class { public: virtual void abc() //Pure virtual function declaration }; Class derived : public base // Derived Class { public: void abc() { cout<< “In derived Classn”; } }; Void main() { base b; derived d; d1.abc(); …….. }
  • 6. Polymorphism is the ability to use an operator or function in different ways. It is achieved using overloading of functions and overloading of operators. Polymorphism is achieved using overloading of functions using:  Multiple methods  Single interface
  • 7. Multiple methods imply two or more functions with the same name but with different signatures. For example, we have two overloaded add functions as shown below: // adds two integer values add(int a, int b) { int c =a+b; cout<<c<<endl; } // adds two floating values add(float a, float b) { float c =a+b; cout<<c<<endl; } The above two functions are called methods
  • 8. With respect to previous example, whether we find sum of two integers or two floating point values it doesn’t matter for us. What is required is “to add two values” So, it is logical to use add as the function name. We may call two functions as shown below: add(10,20); add(1.5,3.5); //Invoke the first function //Invoke the second add function
  • 9. Polymorphism is a feature of OOP with single interface and multiple methods. It also means “One name and multiple forms”. It helps us to reduce complexity using a single interface and multiple methods. It indicates calling one or more functions using same name or giving two or more meanings to an operator.
  • 10. Also called as static binding, it binds an object to a function call during compilation time. Refers to events that occur during compile time. Execution is much faster. Not flexible since functions are decided during compilation time. Achieved using normal function calls, function overloading and operator overloading. Also called as dynamic binding, it binds an object to a function call during run time. Refers to events that occur during run time. Execution is slower. Very flexible since executable functions are decided during run time. Achieved using inheritance and virtual functions.

Notes de l'éditeur

  1. //Error: Object of an abstract class can not be created