Oop 07
Oop 07
Polymorphism
Example:
#include <iostream>
using namespace std;
void sayHi();
int main(){
sayHi(); // the compiler binds any invocation of sayHi()
// to sayHi()’s entry point.
}
void sayHi(){
cout << ‘‘Hello, World!\n’’;
}
1
Polymorphism
2
Polymorphism
3
Polymorphism
Example:
#include <iostream>
using namespace std;
class Shape{
public:
virtual void sayHi() { cout <<‘‘Just hi! \n’’;}
};
class Triangle : public Shape{
public:
virtual void sayHi() { cout <<‘‘Hi from a triangle! \n’’;}
};
class Rectangle : public Shape{
public:
virtual void sayHi() { cout <<‘‘Hi from a rectangle! \n; }
};
int main(){
Shape *p;
int which;
cout << ‘‘1 -- shape, 2 -- triangle, 3 -- rectangle\n ’’;
cin >> which;
switch ( which ) {
case 1: p = new Shape; break;
case 2: p = new Triangle; break;
case 3: p = new Rectangle; break;
}
p -> sayHi(); // dynamic binding of sayHi()
delete p;
}
4
Polymorphism
Virtual Functions
5
Polymorphism
Example:
class B {
public:
virtual void m() { cout << ‘‘Hello! \n’’; }
};
int main(){
B b_obj;
b_obj.m();
}
Example:
class B {
public:
virtual void m() { cout << ‘‘Hello! \n’’;}
};
class D : public B{
// inherite B::m()
};
int main(){
D d_obj;
d_obj.m();
}
6
Polymorphism
Example:
class Shape{
public:
virtual void sayHi() { cout <<‘‘Just hi! \n’’;}
};
class Triangle : public Shape{
public:
// overrides Shape::sayHi(), automatically virtual
void sayHi() { cout <<‘‘Hi from a triangle! \n’’;}
};
7
Polymorphism
Polymorphism
Example:
void print(Shape obj, Shape *ptr, Shape &ref){
ptr -> sayHi(); // bound at run time
ref.sayHi(); // bound at run time
obj.sayHi(); // bound at compile time
}
int main(){
Triangle mytri;
print( mytri, &mytri, mytri );
}
8
Polymorphism
Exercise 1:
class D : public B{
public:
void m() { cout << ‘‘D::m \n’’;}
};
int main(){
B *p;
p = new D;
p -> m();
}
9
Polymorphism
Exercise 2:
class D : public B{
public:
void m() { cout << ‘‘D::m \n’’;}
};
int main(){
D d;
d.m();
}
10
Polymorphism
Name Hiding
Example:
class B {
public:
virtual void m(int x){ cout << ‘‘B::m \n’’;}
};
class D : public B{
public:
void m(){ cout << ‘‘D::m \n’’;} // hides B::m(int)
};
int main(){
D d;
d.m(5); // Error! B::m(int) is hidden.
}
11
Polymorphism
Exercise 3:
class D : public B{
public:
virtual void m(){ cout << ‘‘D::m \n’’;}
};
int main(){
B *p;
p = new D;
p -> m();
}
12
Polymorphism
class B {
public:
virtual void m(){ cout << ‘‘B::m \n’’;}
};
class D : public B{
public:
void m(){ cout << ‘‘D::m \n’’;}
};
int main(){
B *p = new D;
p -> B::m();
}
13
Polymorphism
Virtual Tables
14
Polymorphism
Example:
class B {
public:
virtual void m1(){
// ...
}
virtual void m2(){
// ...
}
};
class D :: B {
void m1(){ // overide B::m1()
// ...
}
};
int main(){
B *p;
B b;
D d;
p = &d; // p is set to d’s address
p -> m1();
p -> m2();
p = &b; // p is set to b’s address
p -> m1()
p -> m2();
}
15
Polymorphism
Example:
class B {
public:
virtual B(); // error
virtual ~B(); // ok
virtual void f(); // ok
};
16
Polymorphism
Example:
class B{
public:
B(){
cout <<"constructing B. \n";
bp = new char[5];
}
~B(){
cout <<"destructing B. \n";
delete[] bp;
}
private:
char *bp;
};
class D : public B{
public:
D(){
cout <<"constructing D. \n";
dp = new char[5000];
}
~D(){
cout <<"destructing D. \n";
delete[] dp;
}
private:
char *dp;
};
int main(){
B *ptr = new D();
delete ptr;
}
OUTPUT?
17
Polymorphism
18
Polymorphism
19
Polymorphism
20
Polymorphism
Example:
class Shape {
public:
void setDim(double, double = 0);
virtual void showArea();
protected:
double x, y;
};
void Shape::showArea(){
cout << "No area computation defined for this class\n";
}
21
Polymorphism
22
Polymorphism
int main(){
Shape *ptr; // declare a pointer to base class
Shape myshape; // create objects
Triangle t;
Rectangle s;
Circle c;
ptr = &myshape;
ptr -> showArea();
ptr = &t;
ptr -> setDim(10.0, 5.0);
ptr -> showArea();
ptr = &s;
ptr -> setDim(10.0, 10.0);
ptr -> showArea();
ptr = &c;
ptr -> setDim(10.0);
ptr -> showArea();
}
23
Polymorphism
24
Polymorphism
Note:
class B{
public:
void setX() = 0; //error! setX not virtual
// ...
};
25
Polymorphism
Abstract Classes
26
Polymorphism
Example:
27
Polymorphism
Example (Cont’d)
class Shape { // Abstract base class
public:
void setDim(double, double = 0);
virtual void showArea() = 0;
protected:
double x, y;
};
void Shape::setDim(double xx, double yy){
x = xx;
y = yy;
}
28
Polymorphism
int main(){
Shape *ptr; // pointers to ABC is ok.
ptr = &t;
ptr -> setDim(10.0, 5.0);
ptr -> showArea();
ptr = &s;
ptr -> setDim(10.0, 10.0);
ptr -> showArea();
ptr = &c;
ptr -> setDim(10.0);
ptr -> showArea();
}
29
Polymorphism
class A { ... };
30
Polymorphism
31
Polymorphism
32
Polymorphism
Example:
int main(){
D *dptr;
B *bptr = new B;
dptr = static_cast< D* >( bptr );
dptr -> zap(); // ?
}
33
Polymorphism
class B{
public:
virtual int zip() { return x; }
private:
int x;
};
class D : public B{
// ...
};
int main(){
D *dptr;
B *bptr = new B;
dptr = dynamic_cast< D* >( bptr );
if (dptr) // check if cast is successful
dptr -> zap();
else
cerr << "Cast not safe \n";
}
class Employee{
public:
virtual void salary() { cout << "Employee::salary"; }
};
class Manager : public Employee{
public:
void salary() { cout << "Manager::salary"; }
};
class Programmer : public Employee{
public:
void salary() { cout << "Programmer::salary"; }
void bonus() { cout << "Programmer::bonus"; }
};
void paycheck( Employee *ep ){
Programmer *pp = dynamic_cast< Programmer* >( ep );
if (pp)
pp -> bonus();
else
ep -> salary();
}
int main(){
Employee *eptr = new Programmer;
paycheck( eptr );
eptr = new Manager;
paycheck( eptr );
}
35
Polymorphism
Example:
#include <iostream>
#include <typeinfo>
using namespace std;
class B_class{
public:
virtual void f(){}
// ...
};
class D_class : public B_class{
// ...
};
int main(){
int x;
cout << typeid( x ).name() << endl;
cout << typeid( 8.16 ).name() <<endl;
D_class dobj;
B_class *bptr = &dobj;
36
Polymorphism
Example:
37