SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Kinan keshkeh 
IT Engineering-Damascus University 
3rd year 
Summer course- 2014 
2 bytes team
• 
Who has any Ques? 
• 
So Let’s Go ! 
• 
What we will take : 
• 
Abstract Classes and Pure Virtual Functions. 
• 
Poymorphism ! 
• 
Downcasting and Upcasting.
Polymorphism
Polymorphism 
•Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) .
Polymorphism 
•Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) . 
•Virtual Function :
Polymorphism 
•Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) . 
•Virtual Function :
Polymorphism 
•Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) . 
•Virtual Function : 
•by including the modifier virtual in the member function declaration (which is given in the definition of the class).
Polymorphism 
•Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) . 
•Virtual Function : 
•by including the modifier virtual in the member function declaration (which is given in the definition of the class). 
•If a function is virtual and a new definition of the function is given in a derived class, then for any object of the derived class, that object will always use the definition of the virtual function that was given in the derived class . 
-1- 
-2- 
-3- 
-4-
Polymorphism 
•NOTE : 
Don’t use too many Virtual Functions , it needs memory space and then slow your program running !!
Polymorphism 
•Sale.h :
Polymorphism 
•Sale.cpp :
Polymorphism
Polymorphism 
•DiscountSale.h:
Polymorphism 
•DiscountSale.cpp:
Polymorphism 
•Main.cpp:
ABSTRACT CLASSES AND PURE VIRTUAL FUNCTIONS
ABSTRACT CLASSES AND PURE VIRTUAL FUNCTIONS 
•Pure Virtual functions : 
•Virtual function can be Pure when it ends by “ =0 “ . 
virtual void draw( ) = 0 ;
ABSTRACT CLASSES AND PURE VIRTUAL FUNCTIONS 
•Pure Virtual functions : 
•Virtual function can be Pure when it ends by “ =0 “ . 
virtual void draw( ) = 0 ; 
•Abstract Classes : 
•Which contain at least one Pure virtual function . 
•They’re used just for deriving classes. 
•Classes which are derived from Abstract classes , are Abstract ! .
! Slicing problem
Slicing problem
Slicing problem
Slicing problem 
•So !! 
•The Slicing problem is to lose some of members , when assigning Child  Father 
•It can be defeated by using pointers !! 
•And !!
!! MAKE DESTRUCTORS VIRTUAL
MAKE DESTRUCTORS VIRTUAL 
Base*p = Derived ; . . . delete p; 
•If destructor of the Base isn’t Virual , then “delete p; “ destruct just the members in Base ..!! 
• when we put Base destructor Virtual , then the Derived destructor be Virtual , and that destruct first the Derived members and by the way ..the Base members !!
MAKE DESTRUCTORS VIRTUAL 
Base*p = Derived ; 
. . . 
delete p; 
•NOTE: 
•when a destructor is marked virtual, then all destructors of derived classes are automatically virtual !
DOWNCASTING AND UPCASTING
DOWNCASTING AND UPCASTING 
•Upcasting : casting from a descendant type to an ancestor type . 
•Upcasting is safe 
•Upcasting doesn’t need any instruction , it works by assigning automaticaly . 
Pet vpet; Dog vdog; //Dog is a derived class with base class Pet. . . . vpet = vdog; //Legal (but does produce the slicing problem.) 
•EX:
DOWNCASTING AND UPCASTING 
•Downcasting : casting from a ancestor type to an descendant type . 
•downcasting is very dangerous 
•downcasting needs dynamic_cast< p2*>(p1); 
Pet *ppet; ppet = new Dog; Dog *pdog = dynamic_cast<Dog*>(ppet); //Dangerous! 
•EX:
DOWNCASTING AND UPCASTING 
•Downcasting : casting from a ancestor type to an descendant type . 
•downcasting is very dangerous 
•downcasting needs dynamic_cast< p2*>(p1); 
Pet *ppet; ppet = new Dog; Dog *pdog = dynamic_cast<Dog*>(ppet); //Dangerous! 
•EX: 
Pet vpet; Dog vdog; //Dog is a derived class with base class Pet. . . . vdog = static_cast<Dog>(vpet); //ILLEGAL!
DOWNCASTING AND UPCASTING 
•Downcasting : casting from a ancestor type to an descendant type . 
•downcasting is very dangerous 
•downcasting needs dynamic_cast< p2*>(p1); 
Pet *ppet; ppet = new Dog; Dog *pdog = dynamic_cast<Dog*>(ppet); //Dangerous! 
•EX: 
Pet vpet; 
Dog vdog; //Dog is a derived class with base class Pet. 
. . . 
vdog = static_cast<Dog>(vpet); //ILLEGAL!
That’s for today 
That’s for today guys !! 
Bye Bye !
2 bytes team 
Group : group link 
Mobile phone- Kinan : 0994385748 
Facebook account : kinan’s account 
2 bytes team

Más contenido relacionado

Similar a 2 BytesC++ course_2014_c12_ polymorphism

Frederick web meetup slides
Frederick web meetup slidesFrederick web meetup slides
Frederick web meetup slides
Pat Zearfoss
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
Hassan Hashmi
 
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
Mike Harris
 

Similar a 2 BytesC++ course_2014_c12_ polymorphism (20)

Understanding Typing. Understanding Ruby.
Understanding Typing. Understanding Ruby.Understanding Typing. Understanding Ruby.
Understanding Typing. Understanding Ruby.
 
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
 
Puppet Camp Duesseldorf 2014: Martin Alfke - Can you upgrade to puppet 4.x?
Puppet Camp Duesseldorf 2014: Martin Alfke - Can you upgrade to puppet 4.x?Puppet Camp Duesseldorf 2014: Martin Alfke - Can you upgrade to puppet 4.x?
Puppet Camp Duesseldorf 2014: Martin Alfke - Can you upgrade to puppet 4.x?
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
The State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila SzegediThe State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila Szegedi
 
Day 1 - Intro to Ruby
Day 1 - Intro to RubyDay 1 - Intro to Ruby
Day 1 - Intro to Ruby
 
Frederick web meetup slides
Frederick web meetup slidesFrederick web meetup slides
Frederick web meetup slides
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented Programmers
 
Can you upgrade to Puppet 4.x?
Can you upgrade to Puppet 4.x?Can you upgrade to Puppet 4.x?
Can you upgrade to Puppet 4.x?
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010
 
OOP in JS
OOP in JSOOP in JS
OOP in JS
 
Software Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with Scala
 
Virtual Function
Virtual FunctionVirtual Function
Virtual Function
 
The best system for object-oriented thinking
The best system for object-oriented thinkingThe best system for object-oriented thinking
The best system for object-oriented thinking
 
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
 
COMP 4026 Lecture 5 OpenFrameworks and Soli
COMP 4026 Lecture 5 OpenFrameworks and SoliCOMP 4026 Lecture 5 OpenFrameworks and Soli
COMP 4026 Lecture 5 OpenFrameworks and Soli
 

Más de kinan keshkeh

Más de kinan keshkeh (20)

10 Little Tricks to Get Your Class’s Attention (and Hold It)
10 Little Tricks to Get Your  Class’s Attention (and Hold It)10 Little Tricks to Get Your  Class’s Attention (and Hold It)
10 Little Tricks to Get Your Class’s Attention (and Hold It)
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm  GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm
 
Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units
 
2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers
 
2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles
 
2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles
 
2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates
 
2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces 2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces
 

Último

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Último (20)

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 

2 BytesC++ course_2014_c12_ polymorphism

  • 1. Kinan keshkeh IT Engineering-Damascus University 3rd year Summer course- 2014 2 bytes team
  • 2. • Who has any Ques? • So Let’s Go ! • What we will take : • Abstract Classes and Pure Virtual Functions. • Poymorphism ! • Downcasting and Upcasting.
  • 4. Polymorphism •Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) .
  • 5. Polymorphism •Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) . •Virtual Function :
  • 6. Polymorphism •Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) . •Virtual Function :
  • 7. Polymorphism •Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) . •Virtual Function : •by including the modifier virtual in the member function declaration (which is given in the definition of the class).
  • 8. Polymorphism •Polymorphism refers to the ability to associate many meanings to one function name by means of (virtual functions ) . •Virtual Function : •by including the modifier virtual in the member function declaration (which is given in the definition of the class). •If a function is virtual and a new definition of the function is given in a derived class, then for any object of the derived class, that object will always use the definition of the virtual function that was given in the derived class . -1- -2- -3- -4-
  • 9. Polymorphism •NOTE : Don’t use too many Virtual Functions , it needs memory space and then slow your program running !!
  • 16. ABSTRACT CLASSES AND PURE VIRTUAL FUNCTIONS
  • 17. ABSTRACT CLASSES AND PURE VIRTUAL FUNCTIONS •Pure Virtual functions : •Virtual function can be Pure when it ends by “ =0 “ . virtual void draw( ) = 0 ;
  • 18. ABSTRACT CLASSES AND PURE VIRTUAL FUNCTIONS •Pure Virtual functions : •Virtual function can be Pure when it ends by “ =0 “ . virtual void draw( ) = 0 ; •Abstract Classes : •Which contain at least one Pure virtual function . •They’re used just for deriving classes. •Classes which are derived from Abstract classes , are Abstract ! .
  • 22. Slicing problem •So !! •The Slicing problem is to lose some of members , when assigning Child  Father •It can be defeated by using pointers !! •And !!
  • 24. MAKE DESTRUCTORS VIRTUAL Base*p = Derived ; . . . delete p; •If destructor of the Base isn’t Virual , then “delete p; “ destruct just the members in Base ..!! • when we put Base destructor Virtual , then the Derived destructor be Virtual , and that destruct first the Derived members and by the way ..the Base members !!
  • 25. MAKE DESTRUCTORS VIRTUAL Base*p = Derived ; . . . delete p; •NOTE: •when a destructor is marked virtual, then all destructors of derived classes are automatically virtual !
  • 27. DOWNCASTING AND UPCASTING •Upcasting : casting from a descendant type to an ancestor type . •Upcasting is safe •Upcasting doesn’t need any instruction , it works by assigning automaticaly . Pet vpet; Dog vdog; //Dog is a derived class with base class Pet. . . . vpet = vdog; //Legal (but does produce the slicing problem.) •EX:
  • 28. DOWNCASTING AND UPCASTING •Downcasting : casting from a ancestor type to an descendant type . •downcasting is very dangerous •downcasting needs dynamic_cast< p2*>(p1); Pet *ppet; ppet = new Dog; Dog *pdog = dynamic_cast<Dog*>(ppet); //Dangerous! •EX:
  • 29. DOWNCASTING AND UPCASTING •Downcasting : casting from a ancestor type to an descendant type . •downcasting is very dangerous •downcasting needs dynamic_cast< p2*>(p1); Pet *ppet; ppet = new Dog; Dog *pdog = dynamic_cast<Dog*>(ppet); //Dangerous! •EX: Pet vpet; Dog vdog; //Dog is a derived class with base class Pet. . . . vdog = static_cast<Dog>(vpet); //ILLEGAL!
  • 30. DOWNCASTING AND UPCASTING •Downcasting : casting from a ancestor type to an descendant type . •downcasting is very dangerous •downcasting needs dynamic_cast< p2*>(p1); Pet *ppet; ppet = new Dog; Dog *pdog = dynamic_cast<Dog*>(ppet); //Dangerous! •EX: Pet vpet; Dog vdog; //Dog is a derived class with base class Pet. . . . vdog = static_cast<Dog>(vpet); //ILLEGAL!
  • 31. That’s for today That’s for today guys !! Bye Bye !
  • 32. 2 bytes team Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 2 bytes team