Docsity

Object-Oriented Programming: Inheritance, Polymorphism, and Access Specifiers, Study notes of Object Oriented Programming

Partial preview of the text

Download Object-Oriented Programming: Inheritance, Polymorphism, and Access Specifiers and more Study notes Object Oriented Programming in PDF only on Docsity!

Chapter 13

Inheritance and Polymorphism

Object Oriented Programming

The 3 principles of object oriented programming:

Encapsulation - the process of combining data members

and functions in a single unit called class. This is to prevent

the access to the data directly, the access to them is

provided through the functions of the class.

Inheritance

Polymorphism

Inheritance

When creating a class, instead of writing completely new

data members and member functions, we can designate

that the class inherit these properties from an existing class

Base Class (super class)- The existing class in inheritance.

A class definition that provides basic, common data

attributes and/or member functions that will be extended by

a derived class.

Derived Class (sub class)- A class definition that inherits

data members and member functions from a Base Class.

Derived Class Definition

To define a derived class we need to specify the access-

specifier and the base class

Types of access specifiers:

  • Public
  • Private
  • Protected
class derived- class : access-specifier base- class

example: base.cpp

UML Diagram Basics

A Class Diagram is a UML Diagram that depicts a class’:

  • Name
  • Data Members ( public private and protected)
  • Member Functions ( public private and protected) Class Name Data Members Member Functions

UML Diagram Basics

We represent member access with the following:

  • means private

+ means public

# means protected

Employee

+ Name

  • Id

+ getName()

+ setName(string)

UML Diagram Basics

Defining Relationships

Derived class diagrams

only contain data members

not defined in the Base

Class

Note that Derived Class

still contains Base Class

Data Members and

Member Functions

GenericItem

  • itemName
  • itemQuantity

+ SetName()

+ SetQuantity()

+ PrintItem()

ProduceItem

  • expirationDate

+ SetExpiration()

+ GetExpiration()

Base Class Derived Class https://learn.zybooks.com/zybook/SMUCS1342Spring2020/chapter/13/section/

Access Specifiers

Three types of access specifiers:

Public: class members and functions accessible to any function

Private: class members and functions only accessible by

member functions and friends of a class (we will talk about

friend functions later)

Protected: class members and functions can be used by

member functions or friends of a class, as well as classes

derived from the class

Life Cycle of Inheritance Unlike other member functions within a class, Constructors and Destructors are NOT inherited by a derived class. Example Constructor: When an object is created:

  1. Memory for class is reserved
  2. The constructor is called
  3. Initializer list is called (if one exists)
  4. Body of constructor is executed
  5. Control returns to calling process class Employee { Employee(): name{ "No Name" }, age{ 0 } { // body of constructor } };

Life Cycle of Inheritance Inheritance Constructor: When an object is created (with inheritance):

  1. Memory for class is reserved ( both Derived AND Base class)
  2. The Derived constructor is called 3. The base class is constructed first using appropriate constructor
  3. Derived Initializer list is called (if one exists)
  4. Body of constructor is executed
  5. Control returns to calling process class Person { Person() {} }; class Employee: public Person { Employee( ): name{ "No Name" }, age{ 0 } { // body of constructor } };

Single Inheritance

Single Inheritance : In single inheritance, a class is allowed

to inherit from only one class. i.e. one sub class is inherited

by one base class only.

class DerivedClass: public Base { }

Multiple Inheritance Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes. class DerivedClass: public Base1, public Base2 { }

Hierarchical Inheritance Hierarchical Inheritance : In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class. class DerivedClass1: public Base { } class DerivedClass2: public Base { }

Is-a vs Has-a Relationship

Is-a relationship - an is-a relationship is representative of

Inheritance, where an object inherits specific properties

from a base class

Has-a relationship - a has-a relationship is when we use

Composition to represent the fact that an object is made up

of other objects (NOT inheritance).

example: abstract.cpp