Professional Documents
Culture Documents
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;
}
}; }
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
Student* ps1;
GradStudent gs1;
ps1 = &gs1;
}
};
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
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
◼ 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”?
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;
}
int main(){
Base *basePtr = new Derive();
delete basePtr;
}
int main(){
Base *basePtr = new Derive();
delete basePtr;
}