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.
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 !!
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!