SlideShare une entreprise Scribd logo
1  sur  55
Télécharger pour lire hors ligne
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/

Contenu connexe

Tendances

C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control StructureMohammad Shaker
 
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 tech questions
C tech questionsC tech questions
C tech questionsvijay00791
 
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
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with PythonHan Lee
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTsKevlin Henney
 
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 classJonah 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
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象勇浩 赖
 

Tendances (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初印象
 

En vedette

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
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism023henil
 
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 & TricksSlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShareSlideShare
 

En vedette (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
 

Similaire à C++ L11-Polymorphism

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 operatorJussi Pohjolainen
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
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-13Abu Saleh
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
Virtual function
Virtual functionVirtual function
Virtual functionharman kaur
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptxhara69
 
Csphtp1 09
Csphtp1 09Csphtp1 09
Csphtp1 09HUST
 
Csphtp1 10
Csphtp1 10Csphtp1 10
Csphtp1 10HUST
 
Functional programming principles
Functional programming principlesFunctional programming principles
Functional programming principlesAndrew Denisov
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Andreas Dewes
 

Similaire à C++ L11-Polymorphism (20)

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’
 
3433 Ch09 Ppt
3433 Ch09 Ppt3433 Ch09 Ppt
3433 Ch09 Ppt
 
Csphtp1 09
Csphtp1 09Csphtp1 09
Csphtp1 09
 
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
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 

Plus de 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 GraduateMohammad 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 PsychologyMohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game DevelopmentMohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesMohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - ColorMohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - TypographyMohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingMohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and ThreadingMohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSMohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsMohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsMohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and GamingMohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / ParseMohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesMohammad 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 AdaptersMohammad Shaker
 

Plus de 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
 

Dernier

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Dernier (20)

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

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/