Unit - 3
•Single, Multiple, Multilevel, Hierarchical, Hybrid Inheritance
•Inline, Friend Function
•Virtual Function
•Function Overriding
•Pure virtual function
•Abstract class and Interface
UML Diagrams
•State chart diagram
•Activity diagram
Inheritance
• The capability of a class to derive properties and
characteristics from another class is called Inheritance.
• Sub Class: The class that inherits properties from another
class is called Sub class or Derived Class.
• Super Class: The class whose properties are inherited by sub
class is called Base Class or Super class.
• Syntax
class derived_class_name : access_specifier base_class_name
{
//body of subclass
};
[Link] Seraphim AP/CSE
Properties of Inheritance
• A derived class inherits all base class methods except
1. Constructor, destructors and copy constructors
of the base class.
2. Overloaded operators of the base class.
3. The friend function of the base class.
[Link] Seraphim AP/CSE
Single Inheritance
Output
Total Area: 35
[Link] Seraphim AP/CSE
Multiple Inheritance
Output
Total Area: 35
Total Paint Cost: 2450
[Link] Seraphim AP/CSE
Hierarchical Inheritance
Protected:
[Link] Seraphim AP/CSE
Output
Enter a number: 5
Square of 5 is 25
Enter a number: 3
Cube of 3 is 27
[Link] Seraphim AP/CSE
Multi-Level Inheritance
Protected:
[Link] Seraphim AP/CSE
Output
Roll number: 5
Subject 1: 10
Subject 2: 40
Total:50
[Link] Seraphim AP/CSE
Hybrid Inheritance
Class B is derived from class A which is single inheritance and then
Class D is inherited from B and class C which is multiple
inheritance.
[Link] Seraphim AP/CSE
[Link] Seraphim AP/CSE
Output
Sum=15
[Link] Seraphim AP/CSE
Inline Member Function
• A member function that is defined inside its class member list is
called an inline member function. Member functions containing a
few lines of code are usually declared inline.
class rectangle
{
private:
float length;
float breadth;
public:
void get_data() { cin>>length>>breadth;}
void show_data();
};
inline void rectangle :: show_data(void)
{
cout<<“length”<<length<<“breadth”<<breadth;
}
Friend Function
• If a function is defined as a friend function then, the private
and protected data of a class can be accessed using
the function.
• The complier knows a given function is a friend function by
the use of the keyword friend.
• For accessing the data, the declaration of a friend function
should be made inside the body of the class (can be anywhere
inside class either in private or public section) starting with
keyword friend.
• Friend function help implement data encapsulation in c++.
• Declaration of friend function
Features of Friend Function
• Friend function is the normal external function that is given
special access privileges.
• The friend declaration can be placed either in the private or
public section.
• A friend function of the class can be a member and friend of
some other class.
• A friend function of one class can be friend of another class.
• Since friend function are non members of the class they do not
require this pointer.
• The keyword friend is placed only in the function declaration
and not in the function definition.
• A friend function can access the class members directly using
the object name and dot operator followed by the specific
member.
• A function can be declared as friend in any number of classes.
Friend function
Output
Mean value: 32.5
Friend Class
Output
Sum:55
Run Time Polymorphism
In C++ polymorphism is mainly divided into two types:
• Compile time Polymorphism
• Runtime Polymorphism
Compile time polymorphism
This type of polymorphism is achieved by function
overloading or operator overloading.
Runtime polymorphism
This type of polymorphism is achieved by Function
Overriding.
Function overriding
It occurs when a derived class has a definition for one of the
member functions of the base class. That base function is said
to be overridden.
Pointers to derived objects
Output
bptr points to base object
b=100
bptr points to derived object
b=200
dptr is derived type pointer
b=200
d=300
using ((DC *)bptr)
b=200
d=400
Non-Virtual Function
Output
Content of base class
Content of base class
Virtual Function
• The keyword Virtual on a member function in base class
indicates to the compiler to delay the binding till run time.
• If you want to execute the member function of derived class
then declare display() in the base class virtual.
• In order to make a function virtual, add keyword virtual in
front of a function.
Rules for virtual function
• Virtual function must be the member of the class.
• Virtual function must be declared in public section so
that object can invoke that function.
• If the virtual function is declared in the class but
defined outside the class, then virtual keyword is used
in function declaration not in definition.
• It cannot be static members.
• It must be accessed using a pointer to the object.
• It cannot be declared as a friend of another class.
• A class may have a virtual destructor, but it cannot
have a virtual constructor.
Virtual Function
Output
Content of first derived class
Content of second derived class
Abstract class and Pure virtual function
• A class with pure virtual function is known as abstract class. For
example the following function is a pure virtual function
virtual void fun() = 0
• A pure virtual function is marked with a virtual keyword and has =
0 after its signature.
• Can call this function an abstract function as it has no body.
• The derived class must give the implementation to all the pure
virtual functions of parent class else it will become abstract class by
default.
• If we try to create an object of a abstract class, compiler shows
error.
Note:
• Adding = 0 to virtual function does not assign value,
it simply indicates the virtual function is pure virtual
function.
• If a base class contains at least one pure virtual
function then that class is known as abstract class.
Features and uses of Abstract Base class
Features
• Abstract base class have at least one pure virtual function.
• Classes inheriting abstract classes must either define all pure
virtual functions or must themselves become an abstract class.
• There can be no object of an abstract class.
• Pointer or references to abstract classes can be created.
• An abstract class can have other data members and member
functions in addition to the pure virtual function.
Uses
• Abstract classes provide a common and standardized interface
to the sub classes so that all derived classes operate in a similar
fashion.
• Abstract class enables new applications to be easily added to a
predefined system.
Output
5
3
Area of square: 25
Area of circle: 28.26
Function Overloading Function Overriding
Providing multiple definition of the Redefinition of parent class function
function by changing signatures. inside its child class with same
(Change no of parameters, change data signature. (return type and parameters
type of parameters). must be same).
It can be done in parent class and inside It can only be done inside child class.
its child class.
Compile time polymorphism. Run time polymorphism.
It is also known as universal It is also known as adhoc polymorphism.
polymorphism.
Eg: Eg:
Void area(int l); Class circle
Void area(float r); { public: void area()
{
Or Cout<<“pi*r*r”;
}}
Void area(int r); Class sphere:public circle
Void area(int l, int b); { public: void area()
{
Cout<<“4*pi*r*r”;
}}
UML Activity Diagram
• Activity diagram is basically a flow chart to represent
the flow from one activity to another activity. The
activity can be described as an operation of the
system.
• It captures the dynamic behaviour of the system.
• Activity diagram is used to show message flow from
one activity to another.
A short heavy bar with two transitions entering it represents a
synchronization of control. The first bar is often called a fork where
a single transition splits into concurrent multiple transitions. The
second bar is called a join, where the concurrent transitions reduce
back to one.
• Activity is a particular operation of the system.
Activity diagrams are not only used for visualizing
dynamic nature of a system but they are also used to
construct the executable system by using forward and
reverse engineering techniques.
• The only missing thing in activity diagram is the
message part.
• The purposes can be described as
– Draw the activity flow of a system.
– Describe the sequence from one activity to
another.
– Describe the parallel, branched and concurrent
flow of the system.
Activity Diagram for Tests to Perform:-
Activity Diagram for Treatment and Operations:-
Activity Diagram Discharge:-
• Following are the main usages of activity
diagram:
– Modeling work flow by using activities.
– Modeling business requirements.
– High level understanding of the system's
functionalities.
– Investigate business requirements at a later stage.
UML Statechart Diagram
• A Statechart diagram describes a state machine.
• Now to clarify it state machine can be defined as a machine which defines
different states of an object and these states are controlled by external or
internal events.
• Statechart diagram is one of the five UML diagrams used to model
dynamic nature of a system.
• They define different states of an object during its lifetime. And these states
are changed by events.
• So Statechart diagrams are useful to model reactive systems. Reactive
systems can be defined as a system that responds to external or internal
events.
• Statechart diagram describes the flow of control from one state to another
state.
• States are defined as a condition in which an object exists and it changes
when some event is triggered.
• So the most important purpose of Statechart diagram is to model life time
of an object from creation to termination.
State Chart Notations
• Following are the main purposes of using Statechart
diagrams:
– To model dynamic aspect of a system.
– To model life time of a reactive system.
– To describe different states of an object during its life time.
– Define a state machine to model states of an object.
Note: A flowchart illustrates processes that are executed in the
system that change the state of objects. A state diagram shows the
actual changes in state, not the processes or commands that
created those changes
State Chart Diagram for Patient
State Diagram for Doctor
State Diagram for Ward Object