0% found this document useful (0 votes)
11 views110 pages

Unit 3 Full PPT Content

The document outlines the syllabus for a course on inheritance in C++, covering various types of inheritance such as single, multiple, hierarchical, multilevel, and hybrid inheritance. It explains the concepts of access specifiers, constructors, and advanced functions like inline, friend, and virtual functions. Additionally, it provides examples and applications of each inheritance type, along with the advantages and limitations associated with them.

Uploaded by

sdiya4711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views110 pages

Unit 3 Full PPT Content

The document outlines the syllabus for a course on inheritance in C++, covering various types of inheritance such as single, multiple, hierarchical, multilevel, and hybrid inheritance. It explains the concepts of access specifiers, constructors, and advanced functions like inline, friend, and virtual functions. Additionally, it provides examples and applications of each inheritance type, along with the advantages and limitations associated with them.

Uploaded by

sdiya4711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 110

18CSC202J - Syllabus

Unit 3 :

Single and Multiple Inheritance - Multilevel inheritance -


Hierarchical - Hybrid Inheritance - Advanced Functions -
Inline - Friend - Virtual -Overriding - Pure virtual function -
Abstract class and Interface -UML State Chart Diagram -
UML Activity Diagram
Types of Inheritance
Inheritanc
e
Inheritance is the capability of one class to acquire properties and
characteristics from another class. The class whose properties are
01 Definition
inherited by other class is called the Parent or Base or Super class.
And, the class which inherits properties of other class is
called Child or Derived or Sub class.

02 Syntax

Single Inheritance, Multiple Inheritance, Hierarchical Inheritance,


Multilevel Inheritance, and Hybrid Inheritance (also known as Virtual
03 Types Inheritance)
Note : All members of a class except Private, are inherited

1. Code Reusability
04 Advantages
2. Method Overriding (Hence, Runtime Polymorphism.)
3. Use of Virtual Keyword
Inheritance Types
01 Single 02 Multiple 03 Hierarchical

04 Multilevel
05 Hybrid
Modes of
Inheritance
If we derive a sub class from a public base class. Then the public
01 Public member of the base class will become public in the derived class and
protected members of the base class will become protected in derived
class

If we derive a sub class from a Protected base class. Then both public
02 Protected
member and protected members of the base class will become
protected in derived class.
If we derive a sub class from a Private base class. Then both public
03 private member and protected members of the base class will become
Private in derived class.

The private members in the base class cannot be directly accessed in


the derived class, while protected members can be directly
Note:
accessed. For example, Classes B, C and D all contain the variables x,
y and z in below example
Inheritance Access Matrix
SINGLE INHERITANCE
Example

Base Class Base Class ClassA

Derived Derived Class Class B


Class
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. Based on the
visibility mode used or access specifier used while deriving, the
properties of the base class are derived. Access specifier can be private,
protected or public.

Syntax:
class Classname // base class
{
..........
};
class classname: access_specifier baseclassname
{

};
Example
void product()
#include <iostream> {
using namespace std; cout << "Product = " << x * y;
class base //single base class }
{ public: };
int x;
void getdata() int main()
{ {
cout << "Enter the value of x = "; derived a; //object of derived class
cin >> x;
} a.getdata();
};
class derived : public base //single derived a.readdata();
{
int y; a.product();
public:
void readdata() return 0;
{ }
cout << "Enter the value of y = ";
cin >> y;
}
Applications of Single Inheritance

Grading 1. University Grading System

System 2. Employee and Salary

Student
Multiple Inheritance

Base Class Base Class


Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or
more than two base classes.

Syntax:
class A // base class
{
..........
};
class B
{
..........
}
class c : access_specifier A, access_specifier B // derived class
{
...........
};
Example:

#include using namespace std; cout<<“enter n2″;


class sum1 cin>>n2;
{ cout<<“sum=”<
protected: int n1; cout<<“sum=”<<n1+n2<<endl;
}; }
class sum2 };
{ int main()
protected: int n2; {
}; show ob;
class show : public sum1, public ob.total();
sum2 }
{
public: int total()
{
cout<<“enter n1”;
cin>>n1;
Applications of Multiple Inhertiance

 Distributed Database
Multilevel Inheritance
A derived class can be derived from another derived class. A child class
can be the parent of another class.

Syntax:
class A // base class
{
..........
};
class B
{
..........
}
class C : access_specifier B
// derived class
{
...........
};
car()
// base class {
class Vehicle cout<<"Car has 4 Wheels”;
{ }
public: };
Vehicle() // main function
{ int main()
cout << "This is a Vehicle"; {
} //creating object of sub class will
}; //invoke the constructor of base classes
class fourWheeler: public Vehicle Car obj;
{ public: return 0;
fourWheeler() }
{
cout<<"Objects with 4 wheels are
vehicles"<<endl;
}
};
// sub class derived from two base classes
class Car: public fourWheeler{
public:
Hierarichal Inheritance
• In Hierarichal Inheritance we
have several classes that are
derived from a common base
class (or parent class).
• Here in the diagram Class 1,
Class 2 and Class 3 are derived
from a common parent class
called Base Class.
Hierarchical Inheritance
How to implement Hierarchal Inheritance in C++
class A {
// Body of Class A • In the example present in the
}; // Base Class
left we have class A as a parent
class and class B and class C
class B : access_specifier A
that inherits some property of
{
Class A.
// Body of Class B
}; // Derived Class
• While performing inheritance it
class C : access_specifier A is necessary to specify the
{ access_specifier which can be
// Body of Class C public, private or protected.
}; // Derived Class
Example
#include <iostream> class C : public A //C is also derived from
using namespace std; class base
class A //single base class {
{ public:
public: void sum()
int x, y; {
void getdata() cout << "\nSum= " << x + y;
{ }
cout << "\nEnter value of x and y:\n"; };
cin >> x >> y; int main()
} {
}; B obj1; //object of derived class B
class B : public A //B is derived from class base C obj2; //object of derived class C
{ obj1.getdata();
public: obj1.product();
void product() obj2.getdata();
{ obj2.sum();
cout << "\nProduct= " << x * y; return 0;
} }
};
Example of Hierarchical Inheritance

class Car { class Volkswagen: public Car {


public: public:
int wheels = 4;
string brand = "Volkswagen";
void show() {
cout << "No of wheels : "<<wheels ; string model = “Beetle”;
} };
};
class Honda: public Car {
class Audi: public Vehicle {
public:
public:
string brand = "Audi";
string brand = "Honda";
string model = “A6”; string model = “City”;
}; };
Hybrid Inheritance
Hybrid Inheritance
• Hybrid Inheritance involves
derivation of more than one
type of inheritance.
• Like in the given image we have
a combination of hierarichal and
multiple inheritance.
• Likewise we can have various
combinations.
Diagramatic Representation of Hybrid
Inheritance
How to implement Hybrid Inheritance in C++
class A
• Hybrid Inheritance is no different
{
// Class A body
than other type of inheritance.
}; • You have to specify the access
specifier and the parent class in
class B : public A
front of the derived class to
{
// Class B body
implement hybrid inheritance.
};

class C
{
// Class C body
};
Access Specifiers
In C++ we have basically three types of access specifiers :
• Public : Here members of the class are accessible outside the class as
well.
• Private : Here members of the class are not accessible outside the
class.
• Protected : Here the members cannot be accessed outside the class,
but can be accessed in inherited classes.
Example of Hybrid Inheritance
class A class C
{ { public:
public: int y;
int x;
C()
};
{
class B : public A y = 4;
{ }
public: };
B()
class D : public B, public C
{
x = 10; { public:
} void sum()
}; {
cout << "Sum= " << x + y;
Order of Constructor Call
Base class constructors are always called in the derived class constructors.
Whenever you create derived class object, first the base class default constructor is
executed and then the derived class's constructor finishes execution.

Points to Remember

 Whether derived class's default constructor is called or parameterised is called,


base class's default constructor is always called inside them.

 To call base class's parameterised constructor inside derived class's parameterised


constructor, we must mention it explicitly while declaring derived class's
parameterized constructor.
Example
class Base // parameterized constructor
{ Derived(int i)
int x; {
public: cout << "Derived parameterized
Base() constructor\n";
{ }
cout<<"Base default constructor"; };
}
}; int main()
{
class Derived : public Base Base b;
{ Derived d1;
int y; Derived d2(10);
public: }
Derived()
{
cout<"Derived def. constructor";
}
Order of Constructor Call
class Base
{
Example
int x; : Derived(int j):Base(j)
public: {
// parameterized constructor y = j;
Base(int i) cout << "Derived Parameterized Constructor\n";
{ }
x = i; };
cout<<"BaseParameterized Constructor\n";
} int main()
}; {
Derived d(10) ;
class Derived : public Base }
{
int y;
public:
// parameterized constructor
Note:
Constructors have a special job of initializing the object
properly. A Derived class constructor has access only to its own
class members, but a Derived class object also have inherited
property of Base class, and only base class constructor can
properly initialize base class members. Hence all the constructors
are called, else object wouldn't be constructed properly.
• Constructors and Destructors are never inherited and hence never
overridden.
01 Concept
• Also, assignment operator = is never inherited. It can be overloaded
but can't be inherited by sub class.

• They are inherited into the derived class.


• If you redefine a static member function in derived class, all the
0 Static other overloaded functions in base class are hidden.
2 Function • Static Member functions can never be virtual.

Derived class can inherit all base class methods except:


• Constructors, destructors and copy constructors of the base class.
0 Limitat
3 • Overloaded operators of the base class.
ion
• The friend functions of the base class.
Calling base and derived class method using base
reference
#include <iostream> Example {
using namespace std; : cout<<"derived Bar printStuff
called"<<endl;
class Foo }
{ };
public:
int x; int main()
{
virtual void printStuff()
{ Foo *foo=new Foo;
cout<<"BaseFoo foo->printStuff();/////this call the base
printStuff called"<<endl; function
} foo=new Bar;
}; foo->printStuff();
}
class Bar : public Foo Output:
{ Base Foo printStuff called
public: derived Bar printStuff called
int y;
Calling base and derived class method using derived
reference
Example
:
#include <iostream> int main()
{
class Base{ Derived bar;
public: //call Base::foo() from bar here?
void foo() bar.Base::foo(); // using a qualified-id
{ return 0;
std::cout<<"base"; }
}
}; Output:
class Derived : public Base base
{
public:
void foo()
{
std::cout<<"derived";
}
};
Advanced Functions: Inline, Friend,
Virtual function and Overriding
Friend Function
1. The main concepts of the object oriented programming paradigm are data hiding and data encapsulation.
2. Whenever data variables are declared in a private category of a class, these members are restricted from
accessing by non – member functions.
3. The private data values can be neither read nor written by non – member functions.
4. If any attempt is made directly to access these members, the compiler will display an error message as
“inaccessible data type”.
5. The best way to access a private data member by a non – member function is to change a private data member
to a public group.
6. When the private or protected data member is changed to a public category, it violates the whole concept or
data hiding and data encapsulation.
7. To solve this problem, a friend function can be declared to have access to these data members.
8. Friend is a special mechanism for letting non – member functions access private data.
9. The keyword friend inform the compiler that it is not a member function of the class.
Friend Function
Granting Friendship to another Class
1. A class can have friendship with another class. Syntax

2. For Example, let there be two classes, first and second. 01


If the class first grants its friendship with the other class class second; forward declaration
second, then the private data members of the class first class first
are permitted to be accessed by the public members of the
class second. But on the other hand, the public member {
functions of the class first cannot access the private private:
members of the class second.
--------------
Friend Function
Two classes having the same Friend
1. A non – member function may have friendship Syntax
with one or more classes. 01 friend return_type
function_name(parameters);
2. When a function has declared to have
friendship with more than one class, the friend classes
Example
should have forward declaration.
3. It implies that it needs to access the private
02 friend return_type fname(first one,
second two)
members of both classes. {}

Note:
where friend is a keyword used as a function
modifier. A friend declaration is valid only
03 within or outside the class definition.
Friend Function
Syntax: Case 2:
class second; forward declaration
class first class sample
{ {
private: private:
-------------- int x;
public: float y;
friend return_type public:
fname(first one, second two); virtual void display();
}; virtual static int sum(); //error
class second }
{ int sample::sum()
private: {}
------------------
public:
friend return_type
fname(first one, second two);
};
Friend Function Example
class sample Example: {
{ clrscr();
private:
int x; sample obj;
public:
void getdata(); obj.getdata();
friend void display(sample abc); cout<<"Accessing the private data by non -
}; member function"<<endl;
void sample::getdata() display(obj);
{
cout<<"Enter a value for x\n"<<endl; getch();
cin>>x; }*/
}
void display(sample abc)
{
cout<<"Entered Number is "<<abc.x<<endl;
}

void main()
Friend Function Example
class first Example:
{ void second::disp(first temp)
friend class second; {
private: cout<<"Entered Number is = "<<temp.x<<endl;
int x; }
public:
void getdata(); void main()
}; {

class second first objx;


{ second objy;
public: objx.getdata();
void disp(first temp); objy.disp(objx);
}; }

void first::getdata()
{
cout<<"Enter a Number ?"<<endl;
cin>>x;
}
Friend Function Example
class second; //Forwardvoid first::getdata() int temp;
Declaration { temp = one.x + two.y;
class first cout<<"Enter a Value for X"<<endl; return(temp);
{ cin>>x; }
private: } void main()
int x; void second::getdata() {
public: { first a;
void getdata(); cout<<"Enter a value for Y"<<endl; second b;
void display(); cin>>y; a.getdata();
friend int sum(first one,second two); } b.getdata();
}; void first::display() a.display();
class second { b.display();
{ cout<<"Entered Number is X = "; int te = sum(a,b);
private: } cout<<"Sum of the two
int y; void second::display() Private data variable (X + Y)";
public: { cout<<" = "<<te<<endl;
void getdata(); cout<<"Entered Number is Y = ";
void display(); } }
friend int sum(first one,second two); int sum (first one,second two)
}; {
Inline Member Function
Inline functions are used in C++ to reduce the overhead of a normal function call.
A member function that is both declared and defined in the class member list is called an inline member function.
The inline specifier is a hint to the compiler that inline substitution of the function body is to be preferred to the
usual function call implementation.

The advantages of using inline member functions are:


1. The size of the object code is considerably reduced.
2. It increases the execution speed, and
3. The inline member function are compact function calls.
Inline Member Function
Syntax: Syntax
class user_defined_name
{ Inline return_type function_name(parameters)
private: {
------------- ----------------
public: ----------------
inline return_type function_name(parameters); }
inline retrun_type function_name(parameters);
---------------
---------------
};
Compiler may not perform inlining in such circumstances like:

1) If a function contains a loop. (for, while, do-while)

2) If a function contains static variables.

3) If a function is recursive.

4) If a function return type is other than void, and the return statement doesn’t
exist in function body.

5) If a function contains switch or goto statement.


Example

#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}

//Output: The cube of 3 is: 27


Inline function and classes
 It is also possible to define the inline function inside the class.

 All the functions defined inside the class are implicitly inline. Thus, all the restrictions of inline
functions are also applied here.

 If you need to explicitly declare inline function in the class then just declare the function inside
the class and define it outside the class using inline keyword
#include <iostream>
using namespace std;
class operation inline void operation :: sum()
{ {
int a,b,add; add = a+b;
cout << "Addition of two numbers: " << a+b << "\n";
public: }
void get();
void sum(); int main()
}; {
inline void operation :: cout << "Program using inline function\n";
get() operation s;
{ s.get();
cout << "Enter first s.sum();
value:"; return 0;
cin >> a; }
cout << "Enter second
value:";
cin >> b;
}

t:
st value: 45 Enter second value: 15 Addition of two numbers: 60 Difference of two numbers: 30 Product of two numbers: 675 Division of two numbers: 3
Virtual function
• Virtual Function is a function in base class, which is
overridden in the derived class, and which tells the Syntax
compiler to perform Late Binding on this function.
01 virtual return_type function_name (arg);
• Virtual Keyword is used to make a member function of
the base class Virtual. Virtual functions allow the most Example
specific version of a member function in an inheritance virtual void show()
hierarchy to be selected for execution. Virtual functions 02 {
make polymorphism possible. cout << "Base class\n";
Key: }
• Only the Base class Method's declaration needs the
Note:
Virtual Keyword, not the definition.
We can call private function of derived class
• If a function is declared as virtual in the base class, it from the base class pointer with the help of
will be virtual in all its derived classes. 03 virtual keyword. Compiler checks for access
specifier only at compile time. So at run time
when late binding occurs it does not check
whether we are calling the private function or
public function.
Virtual function features
Case 1: Case 2:

class sample class sample


{ {
private: private:
int x; int x;
float y; float y;
public: public:
virtual void display(); virtual void display();
virtual int sum(); virtual static int sum(); //error
} }
virtual void sample::display() //Error int sample::sum()
{} {}
Virtual function features
Case 3: Case 4:

class sample class sample


{ {
private: private:
int x; int x;
float y; float y;
public: public:
virtual sample(int x,float y); virtual ~sample(int x,float y);
//error constructor //invalid
void display(); void display();
int sum(); int sum();
} }
Virtual function features
Case 5: Case 6:

class sample_1 virtual void display() //Error, non member


{ function
private: {
int x; ----------------
float y; ----------------
public: }
virtual int sum(int x,float y);
};
class sample_2:public sample_1
{
private:
int z;
public:
virtual float sum(int xx,float yy);
//error
};
Virtual Function Example
class Point
{
Example: {
public:
protected: void getdata()
float length,breath,side,radius,area,height; {
}; cout<<"Enter the Value of the Side of the Box:"<<endl;
class Shape: public Point cin>>side;
{ }
public: void display()
virtual void getdata()=0; {
virtual void display()=0; area = pow(side,4);
}; cout<<"The Area of the Square is:"<<area<<endl;
class Rectangle:public Shape }
{ };
public: void main()
void getdata() {
{ Shape *s;
cout<<"Enter the Breadth Value:"<<endl; Rectangle r;
cin>>breath; Square t;
cout<<"Enter the Length Value:"<<endl; s = &r;
cin>>length; s->getdata();
} s->display();
void display() s = &t;
{ s->getdata();
area = length * breath; s->display();
cout<<"The Area of the Rectangle is:"<<area<<endl; }
}
};
class Square:public Shape
Difference in invocation for virtual and non virtual function
class Base Example: { public:
{ public: virtual void show()
void show() {
{
cout << "Base class\n";
cout << "Base class";
}
} };
}; class Derived:public Base
class Derived:public Base { public:
{ public: void show()
void show() {
{ cout << "Derived Class";
cout << "Derived Class"; } };
} int main()
}
{
int main()
{
Base* b; //Base class pointer
Base* b; //Base class pointer Derived d; //Derived class object
Derived d; //Derived class object b = &d;
b = &d; b->show(); //Late Binding Occurs
b->show(); //Early Binding Occurs }
}
Difference in invocation for virtual and non virtual function
Example: }
#include<iostream> virtual void bar()
using namespace std; {
class Base { std::cout << "Derived::bar\n";
public: }
void foo() };
{
std::cout << "Base::foo\n"; int main() {
} Derived d;
virtual void bar() Base* b = &d;
{ b->foo(); // calls Base::foo
std::cout << "Base::bar\n"; b->bar(); // calls Derived::bar
} }
};
Output:
class Derived : public Base { Base::foo
public: Derived::bar
void foo()
{
std::cout << "Derived::foo\n";
Override
Example: {
#include <iostream> cout << "I am in derived class" << endl;
using namespace std; }
};
class Base {
public: int main()
{
// user wants to override this in the derived class Base b;
virtual void func() derived d;
{ cout << "Compiled successfully" << endl;
cout << "I am in base" << endl; return 0;
} }
}; Output:
Base::foo
class derived : public Base { Derived::bar
public:

// did a mistake by putting an argument "int a"

void func(int a) override


Pure Virtual function
• Pure virtual Functions are virtual functions with no
definition. They start with virtual keyword and ends with Syntax
= 0. Here is the syntax for a pure virtual function.
01 virtual void f() = 0;
• Pure Virtual functions can be given a small definition in Example
the Abstract class, which you want all the derived classes class Base
to have. Still you cannot create object of Abstract class. {
• Also, the Pure Virtual function must be defined outside 02 public:
the class definition. If you will define it inside the class virtual void show() = 0; // Pure
definition, complier will give an error. Inline pure virtual Virtual Function
definition is Illegal. };
Note:
• Abstract Class is a class which contains atleast one Pure
We can call private function of derived class
Virtual function in it. Abstract classes are used to provide
from the base class pointer with the help of
an Interface for its sub classes. Classes inheriting an
Abstract Class must provide definition to the pure virtual 03 virtual keyword. Compiler checks for access
specifier only at compile time. So at run time
function, otherwise they will also become abstract class. when late binding occurs it does not check
whether we are calling the private function or
public function.
Pure Virtual function
class pet Example: private: cout<<"Fish
{ char Environment="<<environment<<endl
private: environment[10]; ;
char name[5]; char food[10]; cout<<"Fish
public: public: Food="<<food<<endl;
virtual void getdata()=0; void getdata();
virtual void display()=0; void display(); cout<<"-------------------------------------"<
}; }; <endl;
class fish:public pet void fish::getdata() }
{ {
private: cout<<"Enter the Fish
char environment[10]; Environment required"<<endl;
char food[10]; cin>>environment;
public: cout<<"Enter the Fish food
void getdata(); require"<<endl;
void display(); cin>>food;
}; }
class dog: public pet void fish::display()
{ {
Pure Virtual function
void dog::getdata() Example: ptr=&f;
{ ptr->getdata();
cout<<"Enter the Dog Environment ptr->display();
required"<<endl; dog d;
cin>>environment; ptr=&d;
cout<<"Enter the Dog Food require"<<endl; ptr->getdata();
cin>>food; ptr->display();
} getch();
void dog::display() }
{
cout<<"Dog
Environment="<<environment<<endl;
cout<<"Dog Food="<<food<<endl;
cout<<"---------------------------------------"<<endl;
}
void main()
{
pet *ptr;
fish f;
Pure Virtual function with definition
• Pure Virtual function is allowed to have definition in the base class itself, but the definition has to be made outside the
class using scope resolution operator. Since inline pure virtual not supported compiler throws an error message.
• Pure virtual definition is useful when all the child will have certain common behavior to be implemented.

#include <iostream> Example: {


using namespace std; cout<<"Derived Version of pure virtual
class Base function";
{ }
public: };
virtual void display()=0; int main()
}; {
void Base::display() Derived ob;
{ Base *p;
cout<<"Base class version of pure virtual"; p = &ob;
} p->display();
return 0;
class Derived:public Base }
{
public:
Pure Virtual function with definition
• Pure virtual implementation of base class version is called inside the override version of derived class method using
class name and scope resolution operator

#include <iostream> Example: {


using namespace std; Base:: display();
class Base cout<<"Derived Version of pure virtual function";
{
public: }
virtual void display()=0; };
}; int main()
void Base::display() {
{ Derived ob;
cout<<"Base class version of pure virtual"; Base *p;
} p = &ob;
p->display();
class Derived:public Base return 0;
{ }
public:
Pure Virtual function with definition
Suppose that you’re modelling a game system for an epic adventure, and your game has a variety of weapons (swords,
arrows etc) that the hero uses to save the world from the evil.
You’ve decided to create an interface Weapon that models the abstract concept that must be the base for your weapons
system. This interface offers the abstract member function attack and it needs to be completed by every concrete weapon
in the game. The Weapon concept doesn’t have a concrete meaning, but perhaps it’s reasonable to have a default behavior
for the attack that the concrete classes may use.

#include <iostream> class Sword : public Weapon


Example:
{
class Weapon { public:
public: void attack() const override
virtual ~Weapon() = default; {
virtual void attack() const = 0; // Calls default member function (Weapon::attack)
}; Weapon::attack();
std::cout << "Sword attack...\n";
void Weapon::attack() const }
{ };
std::cout << "Default attack..\n";
}
Abstract Class
• Abstract class cannot be instantiated, but pointers and references of Abstract class type
can be created.
• Abstract class can have normal functions and variables along with a pure virtual function.
• Abstract classes are mainly used for Upcasting, so that its derived classes can use its
interface.
• Classes inheriting an Abstract Class must implement all pure virtual functions, or else they
will become Abstract too.
Pure virtual function
/Abstract base class Example: int main()
class Base {
{ Base obj; //Compile Time Error
public: Base *b;
virtual void show() = 0; // Pure Virtual Function Derived d;
}; b = &d;
b->show();
class Derived:public Base }
{
public:
void show()
{
cout << "Implementation of Virtual Function in
Derived class\n";
}
};
State chart diagram
18CS202J OBJECT ORIENTED DESIGN AND PROGRAMMING
State diagram
• A state diagram is used to represent the condition of the system or
part of the system at finite instances of time. It’s
a behavioural diagram and it represents the behaviour using finite
state transitions. State diagrams are also referred to as State
machines and State-chart Diagrams.
Uses of state chart diagram
• State chart diagrams are useful to model reactive systems
-Reactive systems can be defined as a system that responds to external or internal events.
• State chart diagram describes the flow of control from one state to
another state.
Purpose
Following are the main purposes of using State chart 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.
Difference between state diagram and
flowchart
The basic purpose of a state diagram is to portray various changes in state of the
class and not the processes or commands causing the changes.

 However, a flowchart on the other hand portrays the processes or commands that
on execution change the state of class or an object of the class.
When to use State charts
So the main usages can be described as:
 To model object states of a system.
 To model reactive system. Reactive system consists of reactive
objects.
 To identify events responsible for state changes.
 Forward and reverse engineering.
How to draw state charts
Before drawing a State chart diagram we must have clarified the following points:
Identify important objects to be analysed.
 Identify the states.
Identify the events.
Elements of state chart diagrams

• Initial State: This shows the starting point of the state chart diagram that is where
the activity starts.
Elements of state chart diagrams
• State: A state represents a condition of a modelled entity for which some action is
performed. The state is indicated by using a rectangle with rounded corners and
contains compartments
Elements of state chart diagrams
• Composite state – We use a rounded rectangle to represent a
composite state also. We represent a state with internal activities
using a composite state.
Elements of state chart diagrams
• Fork – We use a rounded solid rectangular bar to represent a Fork notation with
incoming arrow from the parent state and outgoing arrows towards the newly
created states. We use the fork notation to represent a state splitting into two or
more concurrent states.
Elements of state chart diagrams
• Join – We use a rounded solid rectangular bar to represent a Join notation with
incoming arrows from the joining states and outgoing arrow towards the common
goal state. We use the join notation when two or more states concurrently
converge into one on the occurrence of an event or events.
Elements of state chart diagrams
• Transition: It is indicated by an arrow. Transition is a relationship between two
states which indicates that Event/ Action an object in the first state will enter the
second state and performs certain specified actions.
Elements of state chart diagrams
• Transition – We use a solid arrow to represent the transition or change of control
from one state to another. The arrow is labelled with the event which causes the
change in state.
Elements of state chart diagrams
• Self transition – We use a solid arrow pointing back to the state itself to represent
a self transition. There might be scenarios when the state of the object does not
change upon the occurrence of an event. We use self transitions to represent such
cases.
Elements of state chart diagrams

• Final State: The end of the state chart diagram is represented by a solid circle
surrounded by a circle.
Example state chart for ATM card PIN Verification


Example state chart for order management system
ACTIVITY DIAGRAM

18CSC20 Object Oriented Design and Programming


Activity Diagram
❑ Activity diagram is UML behavior diagram which emphasis on the
sequence and conditions of the flow
❑ It shows a sequence of actions or flow of control in a system.
❑ It is like to a flowchart or a flow diagram.
❑ It is frequently used in business process modeling. They can also describe
the steps in a use case diagram.
❑ The modeled Activities are either sequential or concurrent.
Benefits
◻ It illustrates the logic of an algorithm.
◻ It describes the functions performed in use cases.
◻ Illustrate a business process or workflow between users and the system.
◻ It Simplifies and improves any process by descriptive complex use cases.
◻ Model software architecture elements, such as method, function, and
operation.
Symbols and Notations
Activity
◻ Is used to illustrate a set of actions.
◻ It shows the non-interruptible action of objects.
Symbols and Notations
Action Flow
◻ It is also called edges and paths
◻ It shows switching from one action state to another. It is represented as an
arrowed line.
Symbols and Notations
Object Flow
◻ Object flow denotes the making and modification of objects by activities.
◻ An object flow arrow from an action to an object means that the action
creates or influences the object.
◻ An object flow arrow from an object to an action indicates that the action
state uses the object.
Symbols and Notations
Decisions and Branching
◻ A diamond represents a decision with alternate paths.
◻ When an activity requires a decision prior to moving on to the next
activity, add a diamond between the two activities.
◻ The outgoing alternates should be labeled with a condition or guard
expression. You can also label one of the paths "else."
Symbols and Notations
Guards
◻ In UML, guards are a statement written next to a decision diamond that
must be true before moving next to the next activity.
◻ These are not essential, but are useful when a specific answer, such as
"Yes, three labels are printed," is needed before moving forward.
Symbols and Notations
Synchronization
◻ A fork node is used to split a single incoming flow into multiple
concurrent flows. It is represented as a straight, slightly thicker line in an
activity diagram.
◻ A join node joins multiple concurrent flows back into a single outgoing
flow.
◻ A fork and join mode used together are often referred to as
synchronization.
Symbols and Notations
Synchronization
Symbols and Notations
Time Event
◻ This refers to an event that stops the flow for a time; an hourglass depicts
it.
Symbols and Notations
Merge Event
◻ A merge event brings together multiple flows that are not concurrent.

Final State or End Point


◻ An arrow pointing to a filled circle nested inside another circle represents
the final action state.
Symbols and Notations
Swimlane and Partition
◻ A way to group activities performed by the same actor on an activity
diagram or to group activities in a single thread
Activity Diagram
Activity Diagram with Swimlane
Activity Diagram without Swimlane
State Chart and Activity
Diagram Scenarios
State Chart Diagram

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy