0% found this document useful (0 votes)
17 views

Lecture 8

CS-212 covers object oriented programming and inheritance in C++. Inheritance allows for code reuse and specialization by defining a base class that can be inherited from to create derived classes. This avoids duplicating code and increases reliability. Access specifiers like public, private, and protected determine which members are accessible from derived classes.

Uploaded by

Ayesha Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture 8

CS-212 covers object oriented programming and inheritance in C++. Inheritance allows for code reuse and specialization by defining a base class that can be inherited from to create derived classes. This avoids duplicating code and increases reliability. Access specifiers like public, private, and protected determine which members are accessible from derived classes.

Uploaded by

Ayesha Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

CS-212

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

Mountain Racing Tandem


Bikes Bikes Bikes
Inheritance
Base class
SPECIALIZATION

Shape class
Derived Derived
class class

Circle Triangle
class class

Isosceles Equilateral Right-angle


Triangle Triangle Triangle
GENERALIZATION
Inheritance
Why and when to use inheritance?

• Above process results in duplication of same


code 3 times. This increases the chances of
error and data redundancy. To avoid this type
of situation, inheritance is used
Inheritance
• Single Inheritance
• Multiple Inheritance
Inheritance
Inheritance
• Access specifiers without inheritance
• Access specifiers with inheritance
Inheritance
• An object of a derived class inherits all the
member data and functions of the base class.
• Private members
• Not visible in the derived class.
• The derived class may access them only through the
public interface of the base class.
• Protected Data members
• Visible in the derived class.
• The derived class may access protected members
directly.
Inheritance
• Private members of the base class are
inherited by the derived class, but they are
not visible in the derived class.
• The derived class may access them only
through the public interface of the base class.
Access Control for Public Inheritance

Access Specifier Accessible from Accessible from Accessible from


In Base Class Base Class Derived Class Objects Outside Class

public yes yes yes

protected yes yes no

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;
}};

You might also like