Lecture 8
Lecture 8
OBJECT ORIENTED
PROGRAMMING
MEHREEN TAHIR
Objects as arguments
Inheritance
Why customize or enhance classes?
•Reusability
– Saves time during program development by taking
advantage of proven, high-quality software.
•Reliability
•Conceptualization and overall design of program
Inheritance
• Only define the specialization while reusing
the existing code.
• A class defines the behavior of a set of objects
– E.g., Vehicle, Animals, Books, etc.
• There may be another set of objects that posses a
specialization of this behavior
– E.g., Cars, Jeeps, Busses, Reptiles, Notebooks etc
Think about bicycle
Inheritance
• Be able to create specialized program objects
without starting from scratch
• Inheritance is the object-oriented
programming mechanism for specialization
Bicycle
is-a relationships
Shape class
Derived Derived
class class
Circle Triangle
class class
private yes no no
Modes of Inheritance: Public
class Base
{ };
class Derived : public Base
{ };
• public members of the base class become public
members of the derived class;
• protected members of the base class become
protected members of the derived class; and
• private members of the base class become
inaccessible within the derived class.
Modes of Inheritance: Public
class Base
{ };
class Derived : public Base
{ };
cBase Derived
private: x x not accessible
protected: y y protected
public: z z public
19
Modes of Inheritance: Protected
class Base
{ };
class Derived : protected Base
{ };
• public members of the base class become
protected members of the derived class;
• protected members of the base class become
protected members of the derived class; and
• private members of the base class become
inaccessible within the derived class.
Protected Inheritance
class Base
{ };
class Derived : protected Base
{ };
Base Derived
private: x x not accessible
protected: y y protected
public: z z protected
21
Modes of Inheritance: Private
class Base
{ };
class Derived : private Base
{ };
• public members of the base class become private
members of the derived class;
• protected members of the base class become
private members of the derived class; and
• private members of the base class become
inaccessible within the derived class.
Modes of Inheritance: Private
class Base
{ };
class Derived : private Base
{ };
Base Derived
private: x x not accessible
protected: y y private
public: z z private
23
Modes of Inheritance
24
Access Specifiers
Example
• Design an application, teachers and the
principal (director) in a school based on UML
diagram.
• Create a test program to show functionality of
both classes by only using Principal class
objects.
Generalization in UML Class Diagrams
teacher
name, age,
numOfStudents
setName()
inherits (is a)
Principal
school_name,
numOfTeachers
setSchool()
Inheritance Example
class Teacher{
// Base class int main()
private: {Teacher t1;
string name; Principal p1;
int age, numOfStudents; t1.setName(" Teacher 1");
public: p1.setName(" Principal 1");
void setName (new_name){ p1.setSchool(“ElementarySchool");
name = new_name; return 0;
} }
};
class Principal : public Teacher {
string school_name; p1 object can also access, in
int numOfTeachers; addition to its own member
function setSchool(), the member
public: function from Parent (Base), which
void setSchool(s_name){ is setName().
school_name = s_name;
}};