Tải bản đầy đủ (.ppt) (41 trang)

Object oriented programming with C++ - Session 5 Inheritance pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (266.51 KB, 41 trang )


Object Oriented Programming with C++/ Session 5/ 1 of 41
Inheritance
Session 5
Object Oriented
Programming with C++/
Session 5 / 2 of 41
Session Objectives

Describe Single Inheritance

Describe Base class and Derived class

Access Base class members and use pointers in
classes

Describe types of inheritance

Describe Constructors and Destructors under
inheritance

Describe how to call Member Functions of the
Base Class and Derived Class

Describe Container Classes
Object Oriented
Programming with C++/
Session 5 / 3 of 41
Single Inheritance

To maintain and reuse class objects easily,


we need to be able to relate classes of
similar nature to another.

Single inheritance is the process of
creating new classes from an existing base
class.

For example let us consider a program in
which we are dealing with people
employed in an organisation.
Object Oriented
Programming with C++/
Session 5 / 4 of 41
Single Inheritance (Contd.)

Each of the subclasses is considered to be
derived from the class Employee. The class
Employee is called the base class and the newly
created class is called the derived class.
Employee
Director Manager Secretary Clerk
Object Oriented
Programming with C++/
Session 5 / 5 of 41
Single Inheritance (Contd.)

In a class hierarchy,
the derived classes
inherit the methods
and variables of the

base class.

They can also have
properties and
methods of their
own.
Class: Manager
m_name, m_age,
m_Emp_id, m_salary
m_department
perks, no_of_employees
reporting
Class: Employee
Name, Age, Emp_id
Salary, Department
Object Oriented
Programming with C++/
Session 5 / 6 of 41
Advantages

Most important advantage: reusability of code.
• Once a base class has been created it can be adapted
to work in different situations.

Result of reusability of code is the development
of class libraries.

A class library consists of data and methods
encapsulated in a class.
• Deriving a class from an existing one allows redefining

a member function of the base class and also adding
new members to the derived class.
• The base class remains unchanged in the process.
Object Oriented
Programming with C++/
Session 5 / 7 of 41
Base Class and Derived Class

Derivation can be
represented graphically
with an arrow from the
derived class to the base
class.

The arrow pointing towards
the base class signifies
that the derived class
refers to the functions and
data in the base class,
while the base class has no
access to the derived
class.
Employee
Manager
Base class
Derived class
Derived from
Object Oriented
Programming with C++/
Session 5 / 8 of 41

Base Class and Derived Class

Declaration of a singly derived class is
similar to that of any ordinary class.

We also have to give the name of the base
class. For example,
class Manager : public Employee

Any class can be used as a base class.

A base class can be classified into two
types:

direct base

indirect base
Object Oriented
Programming with C++/
Session 5 / 9 of 41
Direct and Indirect Base

A base class is called direct if it is mentioned in the base
list. For example:
class A
{ };
class B : public A
{ }; // where class A is a direct class.

An indirect class can be written as:

class A
{ };
class B : public A
{ };
class C : public B
{ }; //Can be extended to an arbitrary number of levels.
Object Oriented
Programming with C++/
Session 5 / 10 of 41
Accessibility

Accessibility: Knowing when a member
function or data member of a base class
can be used by objects of the derived class.


Class members can always be accessed by
member functions within their own class,
whether the members are private or public.

Objects defined outside the class can access
class members only if the members are public.
Object Oriented
Programming with C++/
Session 5 / 11 of 41
Accessing Base Class Members

With inheritance:

Derived class members can access members

of the base class if its members are public.

Derived class members cannot access the
private members of the base class.
– For example, if emp1 is an instance of class
Employee, and display() is a member
function of Employee, then in main() the
statement emp1.display(); is valid if
display() is public.

The object emp1 cannot access private members
of the class Employee.
Object Oriented
Programming with C++/
Session 5 / 12 of 41
Protected Access Specifier

The protected section is like the private
section in terms of scope and access.

Protected members can be accessed only by
members of that class.

Protected members cannot be accessed by
objects or functions from outside the class,
such as main().

The difference between private and protected
appears only in derived classes.
Object Oriented

Programming with C++/
Session 5 / 13 of 41
Accessing Base Class members (Contd)

Members of the derived class can access public
and protected members; they cannot access the
private members of the base class.
• In conformance with the object-oriented concept of
information hiding.

No access to some of the class members. Those
members can be put in the private section.

Allow controlled access by providing some
protected members.

Inheritance does not work in reverse.
Object Oriented
Programming with C++/
Session 5 / 14 of 41
Access rules for Base class members
Access
specifier
Accessible
from own class
Accessible
from derived
class
Accessible
from objects

outside the
class
Public Yes Yes Yes
Protected Yes Yes No
Private Yes No No
Object Oriented
Programming with C++/
Session 5 / 15 of 41
Example
class Employee{ //base class
private:
int privA;
protected:
int protA;
public:
int pubA;
};
Object Oriented
Programming with C++/
Session 5 / 16 of 41
Example (Contd.)
//derived class
class Manager : public Employee{
public:
void fn()
{
int a;
a = privA; //error:not accessible
a = protA; //valid
a = pubA; //valid

}
};
Object Oriented
Programming with C++/
Session 5 / 17 of 41
Example (Contd.)
void main()
{
Employee emp; //base class object
emp.privA = 1; //error:not accessible
emp.protA = 1; //error:not accessible
emp.pubA = 1; //valid
Manager mgr; //derived class object
mgr.privA = 1; //error:not accessible
mgr.protA = 1; //error:not accessible
mgr.pubA = 1; //valid
}
Object Oriented
Programming with C++/
Session 5 / 18 of 41
Pointers in classes

We can use a pointer, which has been
declared to point to one class, to actually
refer to another class.

If a derived class has a public base class, then a
pointer to the derived class can be assigned to a
variable of type pointer to the base.


For example, because a Manager is an
Employee, a Manager* can be used as an
Employee*. However, an Employee*
cannot be used as a Manager*.
Object Oriented
Programming with C++/
Session 5 / 19 of 41
Example of pointers
void main()
{
Manager mgr;
Employee* emp = &mgr;
//valid:every Manager is an Employee
Employee eml;
Manager* man = &eml;
//error: not every Employee is a Manager
}

An object of a derived class can be treated as
an object of its base class when manipulated
through pointers. However, the opposite is not
true.
Object Oriented
Programming with C++/
Session 5 / 20 of 41
Types of Inheritance

A derived class can be declared with one of the
specifiers i.e., public, private and protected.


The keyword public in the class declaration of the
derived class specifies that objects of the derived
class are able to access public member functions
of the base class.

With the keyword private in the derived class
declaration, objects of the derived class in
main() cannot even access public member
functions of the base class.
Object Oriented
Programming with C++/
Session 5 / 21 of 41
Example of inheritance types
class A{ //base class
private: int privA;
protected: int protA;
public: int pubA;
};
class B : public A{ //publicly derived class
public:
void fn(){
int a;
a = privA; //error:not accessible
a = protA; //valid
a = pubA; //valid
}
};
Object Oriented
Programming with C++/
Session 5 / 22 of 41

Example (Contd.)
class C : private A{ //privately derived class
public:
void fn()
{ int a;
a = privA; //error:not accessible
a = protA; //valid
a = pubA; //valid
}
};
void main()
{
int m;
B obj1; //object of publicly derived class

Object Oriented
Programming with C++/
Session 5 / 23 of 41
Example (Contd.)
m = obj1.privA; //error:not accessible
m = obj1.protA; //error:not accessible
m = obj1.pubA; //valid: B is publicly
//derived from class A
C obj2; //object of privately derived class
m = obj2.privA; //error:not accessible
m = obj2.protA; //error:not accessible
m = obj2.pubA; //error:not accessible:
//C is privately derived from class A
}
 If no access specifier is given while creating

the class, private is assumed.
Object Oriented
Programming with C++/
Session 5 / 24 of 41
Types of Inheritance (contd.)

Functions in the derived classes can access
protected and public members in the base class.

Objects of the derived classes outside the class
or in main() cannot access private or protected
members of the base class.

The difference is between publicly and privately
derived classes.
• Objects of the class B, which is publicly derived from A
can access public members of the base class.

However, objects of the class C, which is privately
derived from A, cannot access any of the members of
the base class.
Object Oriented
Programming with C++/
Session 5 / 25 of 41
Types of Inheritance (contd.)

The functions in a protected derived class can
access protected and public members of the
base class. However, objects of the derived class
(in main or outside the class) cannot access any

of the members of the base class.
Base Class
Members
Public
Inheritance
Private
Inheritance
Protected
Inheritance
Public Public Private Protected
Protected Protected Private Protected
Private Not inherited Not inherited Not inherited

×