You are on page 1of 22

OBECT ORIENTED

PROGRAMMING
Week 10 – Polymorphism
Inheritance Syntax
int main() {
class Principal : public Teacher{ Teacher t1;
//derived class
string school_name; Principal p1;
int numOfTeachers; p1.setName(“A. Ali");
public: t1.setName(“Ayesha");
void setSchool(string
s_name){ p1.setSchool(“SEECS");
school_name = s_name; return 0;
}
}; }

class Teacher{ // Base class


string name;
int age, numOfStudents;
public:
void setName (string new_name){ name = new_name; }
};
Method Overriding

Fall 2023 CS 212 3


The Student Class Hierarchy

student
student_id, print()
year, name year_group()

inherits (is a)

grad_student

dept, print()
thesis
Student Class
class Student {
int student_id;
int year;
string name;
public:
Student(string name, int id, int year){
student_id = id;
this->year = year;
this->name = name;
}
void print(){
cout <<name<<“,”<<student_id<<“,”<<year<<endl;
}
int year_group(){ return year; }
};

5
Graduate Student Class
class GradStudent : public Student {
string dept;
string thesis;
public:
GradStudent(string name, int id,int year, string dep,
string diss):Student(name, id, year){
dept = dep;
thesis = diss;
}
void print(){
Student::print();
cout << dept << ", " << thesis << endl;
}
};

6
Convocation Class
class Convocation {
int year;
public:
Convocation(int year = 2015) {
this->year = year;
}
// Invite a student to convocation if he has completed
// studies recently
void inviteStudent(Student &s){
if (s.year_group() == year){
cout << “invited”;
}
}
};

7
Send Invitations for Convocation
int main (int argc, char ** argv){
Student s1(“ahmad", 100, 2023);
GradStudent gs1(“ali", 200, 2023,“OOP",
"polymorphism");
s1.print();
gs1.print();

Convocation conv(2023);
if( conv.inviteStudent(s1) ){ An object of class
// send invitation GradStudent cannot be
} convert to Student!
if( conv.inviteStudent(gs1) ){
// send invitation
}
};

8
Exploiting inheritance

◼ What can be done with different classes related by


inheritance?
❑ Any child of a class, can be stored in a pointer to that of a parent

Student* ps1;
GradStudent gs1;
ps1 = &gs1;

◼ This is called polymorphism


How does polymorphism work?

◼ When a base class pointer is used to store one of it’s


children, any method declared in the base class can be
called.
◼ However, any methods that are not declared in the base
class can not be called.
Convocation Class
class Convocation {
int year;
public:
Convocation(int year = 2023) {
this->year = year;
}
// Invite a student to convocation if he has cleared
// all exams AND completed studies recently
void inviteStudent(Student *ps){
if (ps->year_group() == year){
cout << “invited”;
}

}
};

11
Send Invitations for Convocation
int main (int argc, char ** argv){
Student ps1 = new Student(“ahmad", 100, 2023);
GradStudent pgs1 = new GradStudent(“ali", 200,
2023,“OOP", polymorphism");
ps1->print();
pgs1->print();
Convocation conv(2023);
if( conv.inviteStudent(ps1) ){
// send invitation Works Perfectly Now
}
if( conv.inviteStudent(pgs1) ){
// send invitation
}
delete ps1;
delete pgs1;
};
12
Convocation Class
class Convocation {
int year;
public:
Convocation(int year = 2023) {
this->year = year;
}
// Invite a student to convocation
void inviteStudent(Student *ps){
if (ps->year_group() == year){
ps->print();
cout << “invited”;
}
else {cout << “not invited”;}
}
};

13
Static Binding

◼ The choice of print() depends on the pointer


type, not the object pointed to.

◼ This is a compile time decision


(called static binding).

14
Dynamic Binding in OOP

Classes
X
print() X.x;
Y.y;
inherits (isa) Z.z;
X *px;
Y
print() px = & ??;
// can be x,y,or z

px->print(); // ??
Z
print()

15
Two Types of Binding

◼ Static Binding (the default in C++)


❑ px->print() uses px’s print
❑ this is known at compile time

◼ Dynamic Binding
❑ px->print() uses the print() in the object pointed at
❑ this is only known at run time
❑ coded in C++ with virtual functions

16
Why “only known at run time”?

◼ Assume dynamic binding is being used:


X.x;
Y.y;
Z.z;
X *px;
:
cin >> val;
if (val == 1)
px = &x;
else
px = &y;
px->print(); // which print() is used?

17
Student Class
class Student {
int student_id;
int year;
string name;
public:
Student(string name, int id, int year){
student_id = id;
this->year = year;
this->name = name;
}
virtual void print(){
cout <<name<<“,”<<student_id<<“,”<<year<<endl;
}
int year_group(){ return year; }
};

18
Dynamic Binding Reviewed

◼ Advantages:
❑ Extensions of the inheritance hierarchy leaves the
client’s code unaltered.
❑ Code is localised – each class is responsible for
the meaning of its functions (e.g. print()).

◼ Disadvantage:
❑ (Small) run-time overhead.

19
Constructors / Destructors
Input Output
#include<iostream>
using namespace std;
Constructing Base
class Base{ Constructing Derive
public: Destroying Derive
Base(){ cout<<"Constructing Base“ << Destroying Base
endl;}
// this is a destructor:
~Base(){ cout<<"Destroying Base“ <<
endl;}
};
class Derive: public Base{
public:
Derive(){ cout<<"Constructing
Derive“ << endl;}
~Derive(){ cout<<"Destroying Derive“
<< endl;}
};

int main(){
Derive d;
return 0;
}

Fall 2023 CS 212 20


Constructors / Destructors
Input Output
#include<iostream>
using namespace std;
Constructing Base
class Base{ Constructing Derive
public: Destroying Base
Base(){ cout<<"Constructing Base“ <<
endl;}
// this is a destructor:
~Base(){ cout<<"Destroying Base“ <<
endl;}
};
class Derive: public Base{
public:
Derive(){ cout<<"Constructing
Derive“ << endl;}
~Derive(){ cout<<"Destroying Derive“
<< endl;}
};

int main(){
Base *basePtr = new Derive();
delete basePtr;
}

Fall 2023 CS 212 21


Virtual Destructors
Input Output
#include<iostream>
using namespace std;
Constructing Base
class Base{ Constructing Derive
public: Destroying Derive
Base(){ cout<<"Constructing Base“ << Destroying Base
endl;}
// this is a destructor:
virtual ~Base(){ cout<<"Destroying
Base“ << endl;}
};
class Derive: public Base{
public:
Derive(){ cout<<"Constructing
Derive“ << endl;}
~Derive(){ cout<<"Destroying Derive“
<< endl;}
};

int main(){
Base *basePtr = new Derive();
delete basePtr;
}

Fall 2023 CS 212 22

You might also like