SlideShare a Scribd company logo
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2010, 11, 12, 13, 14 
C++ 
Programming Language 
L11-POLYMORPHISM
Polymorphism
Polymorphism 
•Polymorphism 
–“Multi-form” 
–Enables us to program “in general” 
–Write program that process object of classes that are part of the same class hierarchy as if they are all objects of the hierarchy's base class 
–Makes programs extensible 
•Add new classes (new classes to the hierarchy) with no modification
Polymorphism 
#ifndefPOINT_H 
#definePOINT_H 
classPoint { 
public: 
Point( int= 0, int= 0 ); // default constructor 
voidsetX( int); // set x in coordinate pair 
intgetX() const; // return x from coordinate pair 
voidsetY( int); // set y in coordinate pair 
intgetY() const; // return y from coordinate pair 
voidprint() const; // output Point object 
private: 
intx; // x part of coordinate pair 
inty; // y part of coordinate pair 
}; // end class Point 
#endif 
#include"point.h"// Point class definition 
Point::Point( intxValue, intyValue ): x( xValue ), y( yValue ){} 
voidPoint::setX( intxValue ){ x = xValue; } 
intPoint::getX() const{ returnx;} 
voidPoint::setY( intyValue ){ y = yValue;} 
intPoint::getY() const{ returny;} 
voidPoint::print() const 
{ cout << '['<< getX() << ", "<< getY() << ']‘;}
Polymorphism 
#ifndefCIRCLE_H 
#defineCIRCLE_H 
#include"point.h” 
classCircle: publicPoint { 
public: 
Circle( int= 0, int= 0, double= 0.0 ); 
voidsetRadius( double); // set radius 
doublegetRadius() const; // return radius 
doublegetDiameter() const; // return diameter 
doublegetCircumference() const; // return circumference 
doublegetArea() const; // return area 
voidprint() const; // output Circle object 
private: 
doubleradius; // Circle's radius 
}; // end class Circle 
#endif 
#include"circle.h"// Circle class definition 
Circle::Circle( intxValue, intyValue, doubleradiusValue ) 
: Point( xValue, yValue ) // call base-class constructor 
{ setRadius( radiusValue );} 
voidCircle::setRadius( doubleradiusValue ) 
{ radius = ( radiusValue < 0.0? 0.0: radiusValue );} 
doubleCircle::getRadius() const{ returnradius;} 
doubleCircle::getDiameter() const{ return2 * getRadius();} 
doubleCircle::getCircumference() const{ return3.14159 * getDiameter();} 
doubleCircle::getArea() const 
{ return3.14159 * getRadius() * getRadius();} 
voidCircle::print() const 
{ cout << "center = "; 
Point::print(); 
cout << "; radius = "<< getRadius(); 
}
Polymorphism 
void main() 
{ 
Point point( 30, 50 ); Point *pointPtr = 0; // base-class pointer 
Circle circle( 120, 89, 2.7 ); Circle *circlePtr = 0; // derived-class pointer 
// set floating-point numeric formatting 
cout << fixed << setprecision( 2 ); 
// output objects point and circle 
cout << "Print point and circle objects:” << "nPoint: "; 
point.print(); // invokes Point's print 
cout << "nCircle: "; 
circle.print(); // invokes Circle's print 
pointPtr = &point; 
cout << "nnCalling print with base-class pointer to “ << "nbase-class object invokes base-class print “<< "function:n"; 
pointPtr->print(); // invokes Point's print 
circlePtr = &circle; 
cout << "nnCalling print with derived-class pointer to “ << "nderived-class object invokes derived-class “ << "print function:n"; 
circlePtr->print(); // invokes Circle's print 
// aim base-class pointer at derived-class object and print 
pointPtr = &circle; 
cout << "nnCalling print with base-class pointer to “ << "derived-class objectninvokes base-class print “ << 
"function on that derived-class objectn"; 
pointPtr->print(); // invokes Point's print 
cout << endl; 
} 
Print point and circle objects: 
Point: [30, 50] 
Circle: center = [120, 89]; radius = 2.7 
Calling print with base-class pointer to 
base-class object invokes base-class print function: 
[30, 50] 
Calling print with derived-class pointer to 
derived-class object invokes derived-class print function: 
center = [120, 89]; radius = 2.7 
Calling print with base-class pointer to derived-class object 
invokes base-class print function on that derived-class object 
[120, 89] 
Press any key to continue
Polymorphism 
void main() 
{ 
Point point( 30, 50 ); Point *pointPtr = 0; // base-class pointer 
Circle circle( 120, 89, 2.7 ); Circle *circlePtr = 0; // derived-class pointer 
// set floating-point numeric formatting 
cout << fixed << setprecision( 2 ); 
// output objects point and circle 
cout << "Print point and circle objects:” << "nPoint: "; 
point.print(); // invokes Point's print 
cout << "nCircle: "; 
circle.print(); // invokes Circle's print 
pointPtr = &point; 
cout << "nnCalling print with base-class pointer to “ << "nbase-class object invokes base-class print “<< "function:n"; 
pointPtr->print(); // invokes Point's print 
circlePtr = &circle; 
cout << "nnCalling print with derived-class pointer to “ << "nderived-class object invokes derived-class “ << "print function:n"; 
circlePtr->print(); // invokes Circle's print 
// aim base-class pointer at derived-class object and print 
pointPtr = &circle; 
cout << "nnCalling print with base-class pointer to “ << "derived-class objectninvokes base-class print “ << 
"function on that derived-class objectn"; 
pointPtr->print(); // invokes Point's print 
circlePtr = &point; 
} 
Compiler error, circlePtr = &point;a point is not a circle!
Polymorphism 
// Fig. 10.6: fig10_06.cpp 
// Aiming a derived-class pointer at a base-class object. 
#include"point.h"// Point class definition 
#include"circle.h"// Circle class definition 
intmain() 
{ 
Point point( 30, 50 ); 
Circle *circlePtr = 0; 
// aim derived-class pointer at base-class object 
circlePtr = &point; // Error: a Point is not a Circle 
return0; 
} // end main 
Compiler error, circlePtr = &point;a point is not a circle!
Polymorphism 
#include<iostream> 
usingnamespace::std; 
#include"xpoint.h" 
#include"xcircle.h" 
intmain() 
{ 
Point *pointPtr = 0; 
Circle circle( 120, 89, 2.7 ); 
// aim base-class pointer at derived-class object 
pointPtr = &circle; 
// invoke base-class member functions on derived-class 
// object through base-class pointer 
intx = pointPtr->getX(); 
inty = pointPtr->getY(); 
pointPtr->setX( 10 ); 
pointPtr->setY( 10 ); 
pointPtr->print(); 
} // end main 
[10, 10] 
Press any key to continue
Polymorphism 
intmain() 
{ 
Point *pointPtr = 0; 
Circle circle( 120, 89, 2.7 ); 
// aim base-class pointer at derived-class object 
pointPtr = &circle; 
// invoke base-class member functions on derived-class 
// object through base-class pointer 
intx = pointPtr->getX(); 
inty = pointPtr->getY(); 
pointPtr->setX( 10 ); 
pointPtr->setY( 10 ); 
pointPtr->print(); 
// attempt to invoke derived-class-only member functions 
// on derived-class object through base-class pointer 
doubleradius = pointPtr->getRadius(); 
pointPtr->setRadius( 33.33 ); 
doublediameter = pointPtr->getDiameter(); 
doublecircumference = pointPtr->getCircumference(); 
doublearea = pointPtr->getArea(); 
return0; 
} // end main 
Compiler error
Polymorphism -Summary 
•Base-Pointer can aim at derived class 
–But can only call base-class functions 
–Calling derived class functions is a compiler error 
•Coz functions are not defined at base class
RULE 
The type of pointer/reference determines function to call
virtual functions
virtual functions 
•The type of object, not pointer, determines functions to call 
•Why this is so good? suppose the following: 
–draw function in base class 
•Shape 
–draw functions in all derived class 
•Circle, triangle, rectangle 
•Now, to draw any shape: 
–Have base class shape pointer, call member function draw 
–Treat all these shapes generically as object of the base class shape 
–Program determines proper draw function at run time dynamically based on the type of the object to which the base class shape pointer points to at any given time. 
–Declare draw virtual in the base class 
–Override draw in each base class (must have the same signature ) 
–Declare draw virtual in all derived class 
–But that’s not even necessary.
RULE when using virtual 
The type of the object, not the pointer, determines function to call
The Point Class Was Looking Like this: 
#ifndefPOINT_H 
#definePOINT_H 
classPoint { 
public: 
Point( int= 0, int= 0 ); // default constructor 
voidsetX( int); // set x in coordinate pair 
intgetX() const; // return x from coordinate pair 
voidsetY( int); // set y in coordinate pair 
intgetY() const; // return y from coordinate pair 
voidprint() const; // output Point object 
private: 
intx; // x part of coordinate pair 
inty; // y part of coordinate pair 
}; // end class Point 
#endif 
#include"point.h"// Point class definition 
Point::Point( intxValue, intyValue ): x( xValue ), y( yValue ){} 
voidPoint::setX( intxValue ){ x = xValue; } 
intPoint::getX() const{ returnx;} 
voidPoint::setY( intyValue ){ y = yValue;} 
intPoint::getY() const{ returny;} 
voidPoint::print() const 
{ cout << '['<< getX() << ", "<< getY() << ']‘;}
Now, we use virtualfunctions 
#ifndefPOINT_H 
#definePOINT_H 
classPoint { 
public: 
Point( int= 0, int= 0 ); // default constructor 
voidsetX( int); // set x in coordinate pair 
intgetX() const; // return x from coordinate pair 
voidsetY( int); // set y in coordinate pair 
intgetY() const; // return y from coordinate pair 
virtual voidprint() const; // output Point object 
private: 
intx; // x part of coordinate pair 
inty; // y part of coordinate pair 
}; // end class Point 
#endif 
#include"point.h"// Point class definition 
Point::Point( intxValue, intyValue ): x( xValue ), y( yValue ){} 
voidPoint::setX( intxValue ){ x = xValue; } 
intPoint::getX() const{ returnx;} 
voidPoint::setY( intyValue ){ y = yValue;} 
intPoint::getY() const{ returny;} 
voidPoint::print() const 
{ cout << '['<< getX() << ", "<< getY() << ']‘;} 
Just here
The Same in Circle Class, Which Was Looking Like this: 
#ifndefCIRCLE_H 
#defineCIRCLE_H 
#include"point.h” 
classCircle: publicPoint { 
public: 
Circle( int= 0, int= 0, double= 0.0 ); 
voidsetRadius( double); // set radius 
doublegetRadius() const; // return radius 
doublegetDiameter() const; // return diameter 
doublegetCircumference() const; // return circumference 
doublegetArea() const; // return area 
voidprint() const; // output Circle object 
private: 
doubleradius; // Circle's radius 
}; // end class Circle 
#endif 
#include"circle.h"// Circle class definition 
Circle::Circle( intxValue, intyValue, doubleradiusValue ) 
: Point( xValue, yValue ) // call base-class constructor 
{ setRadius( radiusValue );} 
voidCircle::setRadius( doubleradiusValue ) 
{ radius = ( radiusValue < 0.0? 0.0: radiusValue );} 
doubleCircle::getRadius() const{ returnradius;} 
doubleCircle::getDiameter() const{ return2 * getRadius();} 
doubleCircle::getCircumference() const{ return3.14159 * getDiameter();} 
doubleCircle::getArea() const 
{ return3.14159 * getRadius() * getRadius();} 
voidCircle::print() const 
{ cout << "center = "; 
Point::print(); 
cout << "; radius = "<< getRadius(); 
}
#ifndefCIRCLE_H 
#defineCIRCLE_H 
#include"point.h” 
classCircle: publicPoint { 
public: 
Circle( int= 0, int= 0, double= 0.0 ); 
voidsetRadius( double); // set radius 
doublegetRadius() const; // return radius 
doublegetDiameter() const; // return diameter 
doublegetCircumference() const; // return circumference 
doublegetArea() const; // return area 
virtualvoidprint() const; // output Circle object 
private: 
doubleradius; // Circle's radius 
}; // end class Circle 
#endif 
#include"circle.h"// Circle class definition 
Circle::Circle( intxValue, intyValue, doubleradiusValue ) 
: Point( xValue, yValue ) // call base-class constructor 
{ setRadius( radiusValue );} 
voidCircle::setRadius( doubleradiusValue ) 
{ radius = ( radiusValue < 0.0? 0.0: radiusValue );} 
doubleCircle::getRadius() const{ returnradius;} 
doubleCircle::getDiameter() const{ return2 * getRadius();} 
doubleCircle::getCircumference() const{ return3.14159 * getDiameter();} 
doubleCircle::getArea() const 
{ return3.14159 * getRadius() * getRadius();} 
voidCircle::print() const 
{ cout << "center = "; 
Point::print(); 
cout << "; radius = "<< getRadius(); 
} 
Now, we use virtualfunctions 
Just here
virtualfunctions 
intmain() 
{ 
Point point( 30, 50 ); Point *pointPtr = 0; 
Circle circle( 120, 89, 2.7 ); Circle *circlePtr = 0; 
// output objects point and circle using static binding 
cout << "Invoking print function on point and circle “ 
<< "nobjects with static binding “ << "nnPoint: "; 
point.print(); // static binding 
cout << "nCircle: “; 
circle.print(); // static binding 
cout << "nnInvoking print function on point and circle “ 
<< "nobjects with dynamic binding"; 
pointPtr = &point; 
cout << "nnCalling virtual function print with base-class” << "npointer to base-class object” << "ninvokes base-class print function:n"; 
pointPtr->print(); 
circlePtr = &circle; 
cout << "nnCalling virtual function print with “ << "nderived-class pointer to derived-class object “ << "ninvokes derived- class print function:n"; 
circlePtr->print(); 
pointPtr = &circle; 
cout << "nnCalling virtual function print with base-class" 
<< "npointer to derived-class object " 
<< "ninvokes derived-class print function:n"; 
pointPtr->print(); // polymorphism: invokes circle's print} 
Invoking print function on point and circle 
objects with static binding 
Point: [30, 50] 
Circle: Center = [120, 89]; Radius = 2.70 
Invoking print function on point and circle 
objects with dynamic binding 
Calling virtual function print with base-class 
pointer to base-class object 
invokes base-class print function: 
[30, 50] 
Calling virtual function print with 
derived-class pointer to derived-class object 
invokes derived-class print function: 
Center = [120, 89]; Radius = 2.70 
Calling virtual function print with base-class 
pointer to derived-class object 
invokes derived-class print function: 
Center = [120, 89]; Radius = 2.70 
Press any key to continue 
[30, 50] 
center = [120, 89]; radius = 2.7 
[30, 50] 
center = [120, 89]; radius = 2.7 
center = [120, 89]; radius = 2.7 
Press any key to continue
RULE when using virtual 
The type of the object, not the pointer, determines function to call 
REMEMBER, REMEMBER
virtualin baseclassnot virtualat childclassWhat happens?
virtualin baseclass, notvirtualat childclass 
#ifndefPOINT_H 
#definePOINT_H 
classPoint { 
public: 
Point( int= 0, int= 0 ); // default constructor 
voidsetX( int); // set x in coordinate pair 
intgetX() const; // return x from coordinate pair 
voidsetY( int); // set y in coordinate pair 
intgetY() const; // return y from coordinate pair 
virtualvoidprint() const; // output Point object 
private: 
intx; // x part of coordinate pair 
inty; // y part of coordinate pair 
}; // end class Point 
#endif 
#ifndefCIRCLE_H 
#defineCIRCLE_H 
#include"point.h” 
classCircle: publicPoint { 
public: 
Circle( int= 0, int= 0, double= 0.0 ); 
voidsetRadius( double); // set radius 
doublegetRadius() const; // return radius 
doublegetDiameter() const; // return diameter 
doublegetCircumference() const; // return circumference 
doublegetArea() const; // return area 
voidprint() const; // output Circle object 
private: 
doubleradius; // Circle's radius 
}; // end class Circle 
#endif 
Just here 
Nothing here
virtualfunctions 
intmain() 
{ 
Point point( 30, 50 ); 
Point *pointPtr = 0; 
Circle circle( 120, 89, 2.7 ); 
Circle *circlePtr = 0; 
point.print(); cout << endl; // static binding 
circle.print(); cout << endl; // static binding 
pointPtr = &point; 
pointPtr->print(); cout << endl; 
circlePtr = &circle; 
circlePtr->print(); cout << endl; 
pointPtr = &circle; 
pointPtr->print(); cout << endl; 
cout << endl; 
return0; 
} // end main 
[30, 50] 
center = [120, 89]; radius = 2.7 
[30, 50] 
center = [120, 89]; radius = 2.7 
center = [120, 89]; radius = 2.7 
Press any key to continue 
The same behavior as before, that means virtual funtions are directly virtual in child classes once declared virtual in base class
virtual declaration 
virtualfuntions are directly virtualin childclasses once declaredvirtualin baseclass
Virtual Destructors
Virtual Destructors 
•If an object with non-virtual destructor is destroyed explicitly, by applying the delete operator to a base class pointer to the object 
–The behavior is un-expected 
•How to fix this? 
–Declare base-class destructor virtual 
–Making all derived class virtual as well (that’s not necessary as we have seen) 
–Now, when deleting, the appropriate destructor will be called based on the type on object the base class pointer points to. 
•Note 
–Constructors can’t be virtual.
Virtual Destructors 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son(){cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Son S; 
} 
I'm constructor of dad 
I'm constructor of son 
I'm destructor of son 
I'm destructor of dad 
Press any key to continue
Virtual Destructors 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
Son(){cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Son S; 
} 
Compile error. Declaring constructor and desrtuctors private in base class
dynamic_castkeyword
dynamic_castkeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son(){cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D; 
Son S; 
cout << "______________________n"; 
Son *SonPtr =&S; 
Dad *DadPtr =&D; 
Dad *Person = dynamic_cast<Dad *> (DadPtr); 
if(Person) 
// means: if Person points to an object of type Dad 
{cout << "It works!n";} 
else 
{cout <<"Not working!n";} 
} 
I'm constructor of dad 
I'm constructor of dad 
I'm constructor of son 
______________________ 
It works! 
I'm destructor of son 
I'm destructor of dad 
I'm destructor of dad 
Press any key to continue
dynamic_castkeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son(){cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D; 
Son S; 
cout << "______________________n"; 
Son *SonPtr =&S; 
Dad *DadPtr =&D; 
Dad *Person = dynamic_cast<Son *> (DadPtr); 
if(Person) 
{cout << "It works!n";} 
else 
{cout << "Not working!n";} 
} 
Compiler error
typeidKeyword
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2 = newDad; 
cout << typeid(D1).name() << endl; 
cout << typeid(D2).name() << endl; 
} 
I'm constructor of dad 
I'm constructor of dad 
class Dad 
class Dad * 
I'm destructor of dad 
Press any key to continue
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2 = newDad; 
cout << typeid(D1).name() << endl; 
cout << typeid(D2).name() << endl; 
deleteD1; 
} 
Compiler error Delete D1
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2 = newDad; 
cout << typeid(D1).name() << endl; 
cout << typeid(D2).name() << endl; 
deleteD2; 
} 
I'm constructor of dad 
I'm constructor of dad 
class Dad 
class Dad * 
I'm destructor of dad 
I'm destructor of dad 
Press any key to continue
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2 = newDad(); 
cout << typeid(D1).name() << endl; 
cout << typeid(D2).name() << endl; 
deleteD2; 
} 
I'm constructor of dad 
I'm constructor of dad 
class Dad 
class Dad * 
I'm destructor of dad 
I'm destructor of dad 
Press any key to continue
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
cout << typeid(D1).name() << endl; 
cout << typeid(D2).name() << endl; 
deleteD2; 
} 
I'm constructor of dad 
class Dad 
class Dad * 
Press any key to continue 
Then runtime error Delete with no “new”
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
cout << typeid(D1).name() << endl; 
cout << typeid(D2).name() << endl; 
} 
I'm constructor of dad 
class Dad 
class Dad * 
I'm destructor of dad 
Press any key to continue
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Son S1; 
Son *S2 = newDad(); 
cout << typeid(S1).name() << endl; 
cout << typeid(S2).name() << endl; 
} 
Compiler error
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
cout << typeid(D1).name() << endl; 
cout << typeid(*D2).name() << endl; 
} 
I'm constructor of dad 
class Dad 
class Dad 
I'm destructor of dad 
Press any key to continue
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
cout << typeid(Dad).name() << endl; 
} 
I'm constructor of dad 
class Dad 
I'm destructor of dad 
Press any key to continue
typeidKeyword 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
typeid(Dad)=typeid(D2)? 
cout <<"Yeahn": cout << "Non"; 
} 
Compiler error
Quiz, Small One
Quiz #1 
#ifndefPOINT_H 
#definePOINT_H 
classPoint { 
public: 
Point( int= 0, int= 0 ); // default constructor 
voidsetX( int); // set x in coordinate pair 
intgetX() const; // return x from coordinate pair 
voidsetY( int); // set y in coordinate pair 
intgetY() const; // return y from coordinate pair 
virtual voidprint() const; // output Point object 
private: 
intx; // x part of coordinate pair 
inty; // y part of coordinate pair 
}; // end class Point 
#endif 
#ifndefCIRCLE_H 
#defineCIRCLE_H 
#include"point.h” 
classCircle: publicPoint { 
public: 
Circle( int= 0, int= 0, double= 0.0 ); 
voidsetRadius( double); // set radius 
doublegetRadius() const; // return radius 
doublegetDiameter() const; // return diameter 
doublegetCircumference() const; // return circumference 
doublegetArea() const; // return area 
virtual voidprint() const; // output Circle object 
private: 
doubleradius; // Circle's radius 
}; // end class Circle 
#endif
Quiz #1 
intmain() 
{ 
Point point( 30, 50 ); 
Point *pointPtr = 0; 
Circle circle( 120, 89, 2.7 ); 
Circle *circlePtr = 0; 
{ 
staticPoint P(20,40); 
} 
point.print(); cout << endl; 
circle.print(); cout << endl; 
pointPtr = &circle; 
pointPtr->print(); cout << endl; 
circlePtr = &circle; 
circlePtr->print(); cout << endl; 
pointPtr->print(); cout << endl; 
cout << endl; 
return0; 
} // end main 
WaaaWeee Constructor of Point! 
WaaaWeee Constructor of Point! 
WaaaWeee Constructor of Cricle! 
WaaaWeee Constructor of Point! 
[30, 50] 
center = [120, 89]; radius = 2.7 
center = [120, 89]; radius = 2.7 
center = [120, 89]; radius = 2.7 
center = [120, 89]; radius = 2.7 
WaaaWeee Destructor of Cricle! 
WaaaWeee Destructor of Point! 
WaaaWeee Destructor of Point! 
WaaaWeee Destructor of Point! 
Press any key to continue
Quiz #2 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
typeid(Dad)==typeid(D1)? cout<<"Yeahn":cout<<"Non"; 
} 
I'm constructor of dad 
Yeah 
I'm destructor of dad 
Press any key to continue
Quiz #3 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
typeid(Dad)==typeid(D2)? cout <<"Yeahn": cout << "Non"; 
} 
I'm constructor of dad 
No 
I'm destructor of dad 
Press any key to continue
Quiz #4 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
Son *S1 = newSon(); 
D2 = &(S1); 
cout << typeid(D2).name() << endl; 
} 
Compiler error D2 = &(S1);
Quiz #5 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
Son *S1 = newSon; 
D2 = &(*S1); 
cout << typeid(D2).name() << endl; 
} 
I'm constructor of dad 
I'm constructor of dad 
I'm constructor of son 
class Dad * 
I'm destructor of dad 
Press any key to continue
Quiz #6 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2; 
Son *S1; 
D2 = &(*S1); 
cout << typeid(D2).name() << endl; 
} 
Runtime error
Quiz #7 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2 = newDad (); 
Son *S1 = newSon; 
D2 = &(*S1); 
cout << typeid(D2).name() << endl; 
deleteDad; 
} 
Compiler error deleteDad;
Quiz #8 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Dad *D2 = newDad (); 
Son *S1 = newSon; 
D2 = &(*S1); 
cout << typeid(D2).name() << endl; 
deleteS1; 
} 
I'm constructor of dad 
I'm constructor of dad 
I'm constructor of dad 
I'm constructor of son 
class Dad * 
I'm destructor of son 
I'm destructor of dad 
I'm destructor of dad 
Press any key to continue
Quiz #9 
#include<iostream> 
usingnamespace::std; 
classDad 
{ 
public: 
Dad(){cout << "I'm constructor of dad "<< endl; }; 
~Dad(){cout << "I'm destructor of dad "<< endl; }; 
}; 
classSon: publicDad 
{ 
public: 
Son() {cout << "I'm constructor of son "<< endl; } 
~Son() {cout << "I'm destructor of son "<< endl; } 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Dad D1; 
Son *S1 = newSon; 
Dad *D2 = &(*(&(*S1))); 
cout << typeid(D2).name() << endl; 
} 
I'm constructor of dad 
I'm constructor of dad 
I'm constructor of son 
class Dad * 
I'm destructor of dad 
Press any key to continue
http://www.mohammadshaker.com 
mohammadshakergtr@gmail.com 
https://twitter.com/ZGTRShaker@ZGTRShakerhttps://de.linkedin.com/pub/mohammad-shaker/30/122/128/ 
http://www.slideshare.net/ZGTRZGTR 
https://www.goodreads.com/user/show/11193121-mohammad-shaker 
https://plus.google.com/u/0/+MohammadShaker/ 
https://www.youtube.com/channel/UCvJUfadMoEaZNWdagdMyCRA 
http://mohammadshakergtr.wordpress.com/

More Related Content

What's hot

C++ L08-Classes Part1
C++ L08-Classes Part1C++ L08-Classes Part1
C++ L08-Classes Part1
Mohammad Shaker
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
Mohammad Shaker
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
c programming
c programmingc programming
c programming
Arun Umrao
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
rohassanie
 
C tech questions
C tech questionsC tech questions
C tech questions
vijay00791
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
kinan keshkeh
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
Han Lee
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
Kevlin Henney
 
c programming
c programmingc programming
c programming
Arun Umrao
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
ssuserd6b1fd
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
David de Boer
 
Arduino coding class
Arduino coding classArduino coding class
Arduino coding class
Jonah Marrs
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
ssuserd6b1fd
 
C++11
C++11C++11
Functional C++
Functional C++Functional C++
Functional C++
Kevlin Henney
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
勇浩 赖
 

What's hot (20)

C++ L08-Classes Part1
C++ L08-Classes Part1C++ L08-Classes Part1
C++ L08-Classes Part1
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
c programming
c programmingc programming
c programming
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
C tech questions
C tech questionsC tech questions
C tech questions
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
c programming
c programmingc programming
c programming
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
 
Arduino coding class
Arduino coding classArduino coding class
Arduino coding class
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 
C++11
C++11C++11
C++11
 
Functional C++
Functional C++Functional C++
Functional C++
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
 

Viewers also liked

ICT C++
ICT C++ ICT C++
ICT C++
Karthikeyan A K
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Duane Wesley
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
cprogrammings
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
Anil Bapat
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Ahmed Za'anin
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
023henil
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Kumar Gaurav
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Sagar Savale
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Raffaele Doti
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
SlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
SlideShare
 

Viewers also liked (11)

ICT C++
ICT C++ ICT C++
ICT C++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to C++ L11-Polymorphism

Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
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
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
Thesis Scientist Private Limited
 
Day 1
Day 1Day 1
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
AhalyaR
 
L10
L10L10
L10
lksoo
 
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
Abu Saleh
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
DeepasCSE
 
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
Virtual function
Virtual functionVirtual function
Virtual function
harman kaur
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
Syed Zaid Irshad
 
Csphtp1 09
Csphtp1 09Csphtp1 09
Csphtp1 09
HUST
 
3433 Ch09 Ppt
3433 Ch09 Ppt3433 Ch09 Ppt
3433 Ch09 Ppt
martha leon
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
Composite Pattern
Composite PatternComposite Pattern
Composite Pattern
Somenath Mukhopadhyay
 
Csphtp1 10
Csphtp1 10Csphtp1 10
Csphtp1 10
HUST
 
3433 Ch10 Ppt
3433 Ch10 Ppt3433 Ch10 Ppt
3433 Ch10 Ppt
martha leon
 
Functional programming principles
Functional programming principlesFunctional programming principles
Functional programming principles
Andrew Denisov
 

Similar to C++ L11-Polymorphism (20)

Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
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
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Day 1
Day 1Day 1
Day 1
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
L10
L10L10
L10
 
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
 
7 functions
7  functions7  functions
7 functions
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
Virtual function
Virtual functionVirtual function
Virtual function
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 
Csphtp1 09
Csphtp1 09Csphtp1 09
Csphtp1 09
 
3433 Ch09 Ppt
3433 Ch09 Ppt3433 Ch09 Ppt
3433 Ch09 Ppt
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Composite Pattern
Composite PatternComposite Pattern
Composite Pattern
 
Csphtp1 10
Csphtp1 10Csphtp1 10
Csphtp1 10
 
3433 Ch10 Ppt
3433 Ch10 Ppt3433 Ch10 Ppt
3433 Ch10 Ppt
 
Functional programming principles
Functional programming principlesFunctional programming principles
Functional programming principles
 

More from Mohammad Shaker

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
Mohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
Mohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
Mohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
Mohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
Mohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
Mohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
Mohammad Shaker
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
Mohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
Mohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
Mohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
Mohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
Mohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
Mohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
Mohammad Shaker
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
Mohammad Shaker
 

More from Mohammad Shaker (20)

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 

Recently uploaded

Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
PuktoonEngr
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 

Recently uploaded (20)

Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 

C++ L11-Polymorphism

  • 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2010, 11, 12, 13, 14 C++ Programming Language L11-POLYMORPHISM
  • 3. Polymorphism •Polymorphism –“Multi-form” –Enables us to program “in general” –Write program that process object of classes that are part of the same class hierarchy as if they are all objects of the hierarchy's base class –Makes programs extensible •Add new classes (new classes to the hierarchy) with no modification
  • 4. Polymorphism #ifndefPOINT_H #definePOINT_H classPoint { public: Point( int= 0, int= 0 ); // default constructor voidsetX( int); // set x in coordinate pair intgetX() const; // return x from coordinate pair voidsetY( int); // set y in coordinate pair intgetY() const; // return y from coordinate pair voidprint() const; // output Point object private: intx; // x part of coordinate pair inty; // y part of coordinate pair }; // end class Point #endif #include"point.h"// Point class definition Point::Point( intxValue, intyValue ): x( xValue ), y( yValue ){} voidPoint::setX( intxValue ){ x = xValue; } intPoint::getX() const{ returnx;} voidPoint::setY( intyValue ){ y = yValue;} intPoint::getY() const{ returny;} voidPoint::print() const { cout << '['<< getX() << ", "<< getY() << ']‘;}
  • 5. Polymorphism #ifndefCIRCLE_H #defineCIRCLE_H #include"point.h” classCircle: publicPoint { public: Circle( int= 0, int= 0, double= 0.0 ); voidsetRadius( double); // set radius doublegetRadius() const; // return radius doublegetDiameter() const; // return diameter doublegetCircumference() const; // return circumference doublegetArea() const; // return area voidprint() const; // output Circle object private: doubleradius; // Circle's radius }; // end class Circle #endif #include"circle.h"// Circle class definition Circle::Circle( intxValue, intyValue, doubleradiusValue ) : Point( xValue, yValue ) // call base-class constructor { setRadius( radiusValue );} voidCircle::setRadius( doubleradiusValue ) { radius = ( radiusValue < 0.0? 0.0: radiusValue );} doubleCircle::getRadius() const{ returnradius;} doubleCircle::getDiameter() const{ return2 * getRadius();} doubleCircle::getCircumference() const{ return3.14159 * getDiameter();} doubleCircle::getArea() const { return3.14159 * getRadius() * getRadius();} voidCircle::print() const { cout << "center = "; Point::print(); cout << "; radius = "<< getRadius(); }
  • 6. Polymorphism void main() { Point point( 30, 50 ); Point *pointPtr = 0; // base-class pointer Circle circle( 120, 89, 2.7 ); Circle *circlePtr = 0; // derived-class pointer // set floating-point numeric formatting cout << fixed << setprecision( 2 ); // output objects point and circle cout << "Print point and circle objects:” << "nPoint: "; point.print(); // invokes Point's print cout << "nCircle: "; circle.print(); // invokes Circle's print pointPtr = &point; cout << "nnCalling print with base-class pointer to “ << "nbase-class object invokes base-class print “<< "function:n"; pointPtr->print(); // invokes Point's print circlePtr = &circle; cout << "nnCalling print with derived-class pointer to “ << "nderived-class object invokes derived-class “ << "print function:n"; circlePtr->print(); // invokes Circle's print // aim base-class pointer at derived-class object and print pointPtr = &circle; cout << "nnCalling print with base-class pointer to “ << "derived-class objectninvokes base-class print “ << "function on that derived-class objectn"; pointPtr->print(); // invokes Point's print cout << endl; } Print point and circle objects: Point: [30, 50] Circle: center = [120, 89]; radius = 2.7 Calling print with base-class pointer to base-class object invokes base-class print function: [30, 50] Calling print with derived-class pointer to derived-class object invokes derived-class print function: center = [120, 89]; radius = 2.7 Calling print with base-class pointer to derived-class object invokes base-class print function on that derived-class object [120, 89] Press any key to continue
  • 7. Polymorphism void main() { Point point( 30, 50 ); Point *pointPtr = 0; // base-class pointer Circle circle( 120, 89, 2.7 ); Circle *circlePtr = 0; // derived-class pointer // set floating-point numeric formatting cout << fixed << setprecision( 2 ); // output objects point and circle cout << "Print point and circle objects:” << "nPoint: "; point.print(); // invokes Point's print cout << "nCircle: "; circle.print(); // invokes Circle's print pointPtr = &point; cout << "nnCalling print with base-class pointer to “ << "nbase-class object invokes base-class print “<< "function:n"; pointPtr->print(); // invokes Point's print circlePtr = &circle; cout << "nnCalling print with derived-class pointer to “ << "nderived-class object invokes derived-class “ << "print function:n"; circlePtr->print(); // invokes Circle's print // aim base-class pointer at derived-class object and print pointPtr = &circle; cout << "nnCalling print with base-class pointer to “ << "derived-class objectninvokes base-class print “ << "function on that derived-class objectn"; pointPtr->print(); // invokes Point's print circlePtr = &point; } Compiler error, circlePtr = &point;a point is not a circle!
  • 8. Polymorphism // Fig. 10.6: fig10_06.cpp // Aiming a derived-class pointer at a base-class object. #include"point.h"// Point class definition #include"circle.h"// Circle class definition intmain() { Point point( 30, 50 ); Circle *circlePtr = 0; // aim derived-class pointer at base-class object circlePtr = &point; // Error: a Point is not a Circle return0; } // end main Compiler error, circlePtr = &point;a point is not a circle!
  • 9. Polymorphism #include<iostream> usingnamespace::std; #include"xpoint.h" #include"xcircle.h" intmain() { Point *pointPtr = 0; Circle circle( 120, 89, 2.7 ); // aim base-class pointer at derived-class object pointPtr = &circle; // invoke base-class member functions on derived-class // object through base-class pointer intx = pointPtr->getX(); inty = pointPtr->getY(); pointPtr->setX( 10 ); pointPtr->setY( 10 ); pointPtr->print(); } // end main [10, 10] Press any key to continue
  • 10. Polymorphism intmain() { Point *pointPtr = 0; Circle circle( 120, 89, 2.7 ); // aim base-class pointer at derived-class object pointPtr = &circle; // invoke base-class member functions on derived-class // object through base-class pointer intx = pointPtr->getX(); inty = pointPtr->getY(); pointPtr->setX( 10 ); pointPtr->setY( 10 ); pointPtr->print(); // attempt to invoke derived-class-only member functions // on derived-class object through base-class pointer doubleradius = pointPtr->getRadius(); pointPtr->setRadius( 33.33 ); doublediameter = pointPtr->getDiameter(); doublecircumference = pointPtr->getCircumference(); doublearea = pointPtr->getArea(); return0; } // end main Compiler error
  • 11. Polymorphism -Summary •Base-Pointer can aim at derived class –But can only call base-class functions –Calling derived class functions is a compiler error •Coz functions are not defined at base class
  • 12. RULE The type of pointer/reference determines function to call
  • 14. virtual functions •The type of object, not pointer, determines functions to call •Why this is so good? suppose the following: –draw function in base class •Shape –draw functions in all derived class •Circle, triangle, rectangle •Now, to draw any shape: –Have base class shape pointer, call member function draw –Treat all these shapes generically as object of the base class shape –Program determines proper draw function at run time dynamically based on the type of the object to which the base class shape pointer points to at any given time. –Declare draw virtual in the base class –Override draw in each base class (must have the same signature ) –Declare draw virtual in all derived class –But that’s not even necessary.
  • 15. RULE when using virtual The type of the object, not the pointer, determines function to call
  • 16. The Point Class Was Looking Like this: #ifndefPOINT_H #definePOINT_H classPoint { public: Point( int= 0, int= 0 ); // default constructor voidsetX( int); // set x in coordinate pair intgetX() const; // return x from coordinate pair voidsetY( int); // set y in coordinate pair intgetY() const; // return y from coordinate pair voidprint() const; // output Point object private: intx; // x part of coordinate pair inty; // y part of coordinate pair }; // end class Point #endif #include"point.h"// Point class definition Point::Point( intxValue, intyValue ): x( xValue ), y( yValue ){} voidPoint::setX( intxValue ){ x = xValue; } intPoint::getX() const{ returnx;} voidPoint::setY( intyValue ){ y = yValue;} intPoint::getY() const{ returny;} voidPoint::print() const { cout << '['<< getX() << ", "<< getY() << ']‘;}
  • 17. Now, we use virtualfunctions #ifndefPOINT_H #definePOINT_H classPoint { public: Point( int= 0, int= 0 ); // default constructor voidsetX( int); // set x in coordinate pair intgetX() const; // return x from coordinate pair voidsetY( int); // set y in coordinate pair intgetY() const; // return y from coordinate pair virtual voidprint() const; // output Point object private: intx; // x part of coordinate pair inty; // y part of coordinate pair }; // end class Point #endif #include"point.h"// Point class definition Point::Point( intxValue, intyValue ): x( xValue ), y( yValue ){} voidPoint::setX( intxValue ){ x = xValue; } intPoint::getX() const{ returnx;} voidPoint::setY( intyValue ){ y = yValue;} intPoint::getY() const{ returny;} voidPoint::print() const { cout << '['<< getX() << ", "<< getY() << ']‘;} Just here
  • 18. The Same in Circle Class, Which Was Looking Like this: #ifndefCIRCLE_H #defineCIRCLE_H #include"point.h” classCircle: publicPoint { public: Circle( int= 0, int= 0, double= 0.0 ); voidsetRadius( double); // set radius doublegetRadius() const; // return radius doublegetDiameter() const; // return diameter doublegetCircumference() const; // return circumference doublegetArea() const; // return area voidprint() const; // output Circle object private: doubleradius; // Circle's radius }; // end class Circle #endif #include"circle.h"// Circle class definition Circle::Circle( intxValue, intyValue, doubleradiusValue ) : Point( xValue, yValue ) // call base-class constructor { setRadius( radiusValue );} voidCircle::setRadius( doubleradiusValue ) { radius = ( radiusValue < 0.0? 0.0: radiusValue );} doubleCircle::getRadius() const{ returnradius;} doubleCircle::getDiameter() const{ return2 * getRadius();} doubleCircle::getCircumference() const{ return3.14159 * getDiameter();} doubleCircle::getArea() const { return3.14159 * getRadius() * getRadius();} voidCircle::print() const { cout << "center = "; Point::print(); cout << "; radius = "<< getRadius(); }
  • 19. #ifndefCIRCLE_H #defineCIRCLE_H #include"point.h” classCircle: publicPoint { public: Circle( int= 0, int= 0, double= 0.0 ); voidsetRadius( double); // set radius doublegetRadius() const; // return radius doublegetDiameter() const; // return diameter doublegetCircumference() const; // return circumference doublegetArea() const; // return area virtualvoidprint() const; // output Circle object private: doubleradius; // Circle's radius }; // end class Circle #endif #include"circle.h"// Circle class definition Circle::Circle( intxValue, intyValue, doubleradiusValue ) : Point( xValue, yValue ) // call base-class constructor { setRadius( radiusValue );} voidCircle::setRadius( doubleradiusValue ) { radius = ( radiusValue < 0.0? 0.0: radiusValue );} doubleCircle::getRadius() const{ returnradius;} doubleCircle::getDiameter() const{ return2 * getRadius();} doubleCircle::getCircumference() const{ return3.14159 * getDiameter();} doubleCircle::getArea() const { return3.14159 * getRadius() * getRadius();} voidCircle::print() const { cout << "center = "; Point::print(); cout << "; radius = "<< getRadius(); } Now, we use virtualfunctions Just here
  • 20. virtualfunctions intmain() { Point point( 30, 50 ); Point *pointPtr = 0; Circle circle( 120, 89, 2.7 ); Circle *circlePtr = 0; // output objects point and circle using static binding cout << "Invoking print function on point and circle “ << "nobjects with static binding “ << "nnPoint: "; point.print(); // static binding cout << "nCircle: “; circle.print(); // static binding cout << "nnInvoking print function on point and circle “ << "nobjects with dynamic binding"; pointPtr = &point; cout << "nnCalling virtual function print with base-class” << "npointer to base-class object” << "ninvokes base-class print function:n"; pointPtr->print(); circlePtr = &circle; cout << "nnCalling virtual function print with “ << "nderived-class pointer to derived-class object “ << "ninvokes derived- class print function:n"; circlePtr->print(); pointPtr = &circle; cout << "nnCalling virtual function print with base-class" << "npointer to derived-class object " << "ninvokes derived-class print function:n"; pointPtr->print(); // polymorphism: invokes circle's print} Invoking print function on point and circle objects with static binding Point: [30, 50] Circle: Center = [120, 89]; Radius = 2.70 Invoking print function on point and circle objects with dynamic binding Calling virtual function print with base-class pointer to base-class object invokes base-class print function: [30, 50] Calling virtual function print with derived-class pointer to derived-class object invokes derived-class print function: Center = [120, 89]; Radius = 2.70 Calling virtual function print with base-class pointer to derived-class object invokes derived-class print function: Center = [120, 89]; Radius = 2.70 Press any key to continue [30, 50] center = [120, 89]; radius = 2.7 [30, 50] center = [120, 89]; radius = 2.7 center = [120, 89]; radius = 2.7 Press any key to continue
  • 21. RULE when using virtual The type of the object, not the pointer, determines function to call REMEMBER, REMEMBER
  • 22. virtualin baseclassnot virtualat childclassWhat happens?
  • 23. virtualin baseclass, notvirtualat childclass #ifndefPOINT_H #definePOINT_H classPoint { public: Point( int= 0, int= 0 ); // default constructor voidsetX( int); // set x in coordinate pair intgetX() const; // return x from coordinate pair voidsetY( int); // set y in coordinate pair intgetY() const; // return y from coordinate pair virtualvoidprint() const; // output Point object private: intx; // x part of coordinate pair inty; // y part of coordinate pair }; // end class Point #endif #ifndefCIRCLE_H #defineCIRCLE_H #include"point.h” classCircle: publicPoint { public: Circle( int= 0, int= 0, double= 0.0 ); voidsetRadius( double); // set radius doublegetRadius() const; // return radius doublegetDiameter() const; // return diameter doublegetCircumference() const; // return circumference doublegetArea() const; // return area voidprint() const; // output Circle object private: doubleradius; // Circle's radius }; // end class Circle #endif Just here Nothing here
  • 24. virtualfunctions intmain() { Point point( 30, 50 ); Point *pointPtr = 0; Circle circle( 120, 89, 2.7 ); Circle *circlePtr = 0; point.print(); cout << endl; // static binding circle.print(); cout << endl; // static binding pointPtr = &point; pointPtr->print(); cout << endl; circlePtr = &circle; circlePtr->print(); cout << endl; pointPtr = &circle; pointPtr->print(); cout << endl; cout << endl; return0; } // end main [30, 50] center = [120, 89]; radius = 2.7 [30, 50] center = [120, 89]; radius = 2.7 center = [120, 89]; radius = 2.7 Press any key to continue The same behavior as before, that means virtual funtions are directly virtual in child classes once declared virtual in base class
  • 25. virtual declaration virtualfuntions are directly virtualin childclasses once declaredvirtualin baseclass
  • 27. Virtual Destructors •If an object with non-virtual destructor is destroyed explicitly, by applying the delete operator to a base class pointer to the object –The behavior is un-expected •How to fix this? –Declare base-class destructor virtual –Making all derived class virtual as well (that’s not necessary as we have seen) –Now, when deleting, the appropriate destructor will be called based on the type on object the base class pointer points to. •Note –Constructors can’t be virtual.
  • 28. Virtual Destructors #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son(){cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Son S; } I'm constructor of dad I'm constructor of son I'm destructor of son I'm destructor of dad Press any key to continue
  • 29. Virtual Destructors #include<iostream> usingnamespace::std; classDad { Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { Son(){cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Son S; } Compile error. Declaring constructor and desrtuctors private in base class
  • 31. dynamic_castkeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son(){cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D; Son S; cout << "______________________n"; Son *SonPtr =&S; Dad *DadPtr =&D; Dad *Person = dynamic_cast<Dad *> (DadPtr); if(Person) // means: if Person points to an object of type Dad {cout << "It works!n";} else {cout <<"Not working!n";} } I'm constructor of dad I'm constructor of dad I'm constructor of son ______________________ It works! I'm destructor of son I'm destructor of dad I'm destructor of dad Press any key to continue
  • 32. dynamic_castkeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son(){cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D; Son S; cout << "______________________n"; Son *SonPtr =&S; Dad *DadPtr =&D; Dad *Person = dynamic_cast<Son *> (DadPtr); if(Person) {cout << "It works!n";} else {cout << "Not working!n";} } Compiler error
  • 34. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2 = newDad; cout << typeid(D1).name() << endl; cout << typeid(D2).name() << endl; } I'm constructor of dad I'm constructor of dad class Dad class Dad * I'm destructor of dad Press any key to continue
  • 35. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2 = newDad; cout << typeid(D1).name() << endl; cout << typeid(D2).name() << endl; deleteD1; } Compiler error Delete D1
  • 36. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2 = newDad; cout << typeid(D1).name() << endl; cout << typeid(D2).name() << endl; deleteD2; } I'm constructor of dad I'm constructor of dad class Dad class Dad * I'm destructor of dad I'm destructor of dad Press any key to continue
  • 37. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2 = newDad(); cout << typeid(D1).name() << endl; cout << typeid(D2).name() << endl; deleteD2; } I'm constructor of dad I'm constructor of dad class Dad class Dad * I'm destructor of dad I'm destructor of dad Press any key to continue
  • 38. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; cout << typeid(D1).name() << endl; cout << typeid(D2).name() << endl; deleteD2; } I'm constructor of dad class Dad class Dad * Press any key to continue Then runtime error Delete with no “new”
  • 39. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; cout << typeid(D1).name() << endl; cout << typeid(D2).name() << endl; } I'm constructor of dad class Dad class Dad * I'm destructor of dad Press any key to continue
  • 40. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Son S1; Son *S2 = newDad(); cout << typeid(S1).name() << endl; cout << typeid(S2).name() << endl; } Compiler error
  • 41. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; cout << typeid(D1).name() << endl; cout << typeid(*D2).name() << endl; } I'm constructor of dad class Dad class Dad I'm destructor of dad Press any key to continue
  • 42. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; cout << typeid(Dad).name() << endl; } I'm constructor of dad class Dad I'm destructor of dad Press any key to continue
  • 43. typeidKeyword #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; typeid(Dad)=typeid(D2)? cout <<"Yeahn": cout << "Non"; } Compiler error
  • 45. Quiz #1 #ifndefPOINT_H #definePOINT_H classPoint { public: Point( int= 0, int= 0 ); // default constructor voidsetX( int); // set x in coordinate pair intgetX() const; // return x from coordinate pair voidsetY( int); // set y in coordinate pair intgetY() const; // return y from coordinate pair virtual voidprint() const; // output Point object private: intx; // x part of coordinate pair inty; // y part of coordinate pair }; // end class Point #endif #ifndefCIRCLE_H #defineCIRCLE_H #include"point.h” classCircle: publicPoint { public: Circle( int= 0, int= 0, double= 0.0 ); voidsetRadius( double); // set radius doublegetRadius() const; // return radius doublegetDiameter() const; // return diameter doublegetCircumference() const; // return circumference doublegetArea() const; // return area virtual voidprint() const; // output Circle object private: doubleradius; // Circle's radius }; // end class Circle #endif
  • 46. Quiz #1 intmain() { Point point( 30, 50 ); Point *pointPtr = 0; Circle circle( 120, 89, 2.7 ); Circle *circlePtr = 0; { staticPoint P(20,40); } point.print(); cout << endl; circle.print(); cout << endl; pointPtr = &circle; pointPtr->print(); cout << endl; circlePtr = &circle; circlePtr->print(); cout << endl; pointPtr->print(); cout << endl; cout << endl; return0; } // end main WaaaWeee Constructor of Point! WaaaWeee Constructor of Point! WaaaWeee Constructor of Cricle! WaaaWeee Constructor of Point! [30, 50] center = [120, 89]; radius = 2.7 center = [120, 89]; radius = 2.7 center = [120, 89]; radius = 2.7 center = [120, 89]; radius = 2.7 WaaaWeee Destructor of Cricle! WaaaWeee Destructor of Point! WaaaWeee Destructor of Point! WaaaWeee Destructor of Point! Press any key to continue
  • 47. Quiz #2 #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; typeid(Dad)==typeid(D1)? cout<<"Yeahn":cout<<"Non"; } I'm constructor of dad Yeah I'm destructor of dad Press any key to continue
  • 48. Quiz #3 #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; typeid(Dad)==typeid(D2)? cout <<"Yeahn": cout << "Non"; } I'm constructor of dad No I'm destructor of dad Press any key to continue
  • 49. Quiz #4 #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; Son *S1 = newSon(); D2 = &(S1); cout << typeid(D2).name() << endl; } Compiler error D2 = &(S1);
  • 50. Quiz #5 #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; Son *S1 = newSon; D2 = &(*S1); cout << typeid(D2).name() << endl; } I'm constructor of dad I'm constructor of dad I'm constructor of son class Dad * I'm destructor of dad Press any key to continue
  • 51. Quiz #6 #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2; Son *S1; D2 = &(*S1); cout << typeid(D2).name() << endl; } Runtime error
  • 52. Quiz #7 #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2 = newDad (); Son *S1 = newSon; D2 = &(*S1); cout << typeid(D2).name() << endl; deleteDad; } Compiler error deleteDad;
  • 53. Quiz #8 #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Dad *D2 = newDad (); Son *S1 = newSon; D2 = &(*S1); cout << typeid(D2).name() << endl; deleteS1; } I'm constructor of dad I'm constructor of dad I'm constructor of dad I'm constructor of son class Dad * I'm destructor of son I'm destructor of dad I'm destructor of dad Press any key to continue
  • 54. Quiz #9 #include<iostream> usingnamespace::std; classDad { public: Dad(){cout << "I'm constructor of dad "<< endl; }; ~Dad(){cout << "I'm destructor of dad "<< endl; }; }; classSon: publicDad { public: Son() {cout << "I'm constructor of son "<< endl; } ~Son() {cout << "I'm destructor of son "<< endl; } }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Dad D1; Son *S1 = newSon; Dad *D2 = &(*(&(*S1))); cout << typeid(D2).name() << endl; } I'm constructor of dad I'm constructor of dad I'm constructor of son class Dad * I'm destructor of dad Press any key to continue
  • 55. http://www.mohammadshaker.com mohammadshakergtr@gmail.com https://twitter.com/ZGTRShaker@ZGTRShakerhttps://de.linkedin.com/pub/mohammad-shaker/30/122/128/ http://www.slideshare.net/ZGTRZGTR https://www.goodreads.com/user/show/11193121-mohammad-shaker https://plus.google.com/u/0/+MohammadShaker/ https://www.youtube.com/channel/UCvJUfadMoEaZNWdagdMyCRA http://mohammadshakergtr.wordpress.com/