SlideShare a Scribd company logo
21CSC101T OBJECT ORIENTED
DESIGN AND PROGRAMMING
Dr.M.Sivakumar
AP/NWC
SRMIST
Course Outcomes (CO)
At the end of this course, learners will be able to:
• CO-1: Create programs using object-oriented approach
and design methodologies
• CO-2: Construct programs using method overloading
and operator overloading
• CO-3: Create programs using inline, friend and virtual
functions, construct programs using standard
templates
• CO-4: Construct programs using exceptional handling
and collections
• CO-5: Create Models of the system using UML
Diagrams
Unit-3 - Inheritance
Inheritance – Types -Single and Multiple
Inheritance - Multilevel Inheritance -
Hierarchical Inheritance - Hybrid Inheritance -
Advanced Functions - Inline, Friend- Virtual -
Pure Virtual function - Abstract class – UML
State Chart Diagram - UML Activity Diagram
Inheritance
• the mechanism by which a class can inherit properties
(fields) and behaviors (methods) from another class.
• enables code reuse and promotes modularity and
extensibility in software development
• establishes an "is-a" relationship between classes,
where a subclass (or derived class) inherits
characteristics from a superclass (or base class).
• The subclass inherits all non-private members
(methods and fields) of the superclass, allowing it to
reuse the code defined in the superclass.
Inheritance Examples
Inheritance Examples
Inheritance Examples
Inheritance Examples
Inheritance
• Syntax:
class DerivedClass : accessSpecifier BaseClass
{
// Derived class members and methods
};
• DerivedClass
– the name of the derived class.
• accessSpecifier
– specifies the access level for the inherited members.
– It can be public, protected, or private.
– If not explicitly specified, it defaults to private.
• BaseClass
– the name of the base class being inherited from.
Inheritance
• Public
– Public members of the base class remain public in the
derived class.
– They are accessible from outside the class hierarchy.
• Protected
– Public members of the base class become protected in the
derived class.
– They are accessible within the derived class and its
subclasses but not from outside.
• Private
– Public members of the base class become private in the
derived class.
– They are not accessible from the derived class or outside.
Inheritance
class Base {
public:
int publicMember;
protected:
int protectedMember;
private:
int privateMember;
};
class DerivedPublic : public Base {
// publicMember is accessible
// protectedMember is accessible
// privateMember is not accessible
};
class DerivedProtected : protected Base {
// publicMember is accessible
// protectedMember is accessible
// privateMember is not accessible
};
class DerivedPrivate : private Base {
// publicMember is accessible
// protectedMember is accessible
// privateMember is not accessible
};
Types
Types
• Single Inheritance
• Multiple Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
Single Inheritance
• Single inheritance refers to the inheritance
mechanism where a derived class inherits
from only one base class.
Single Inheritance Syntax
class BaseClass
{
// Base class members and methods
};
class DerivedClass : accessSpecifier BaseClass
{
// Derived class members and methods
};
Single Inheritance Example
#include <iostream>
using namespace std;
// Base class
class Animal
{
public:
void eat()
{
cout << "Animal is eating.n";
}
};
// Derived class inheriting from Animal
class Dog : public Animal
{
public:
void bark()
{
cout << "Dog is barking.n";
}
};
int main()
{
Dog myDog;
myDog.eat(); // Output: Animal is eating.
myDog.bark(); // Output: Dog is barking.
return 0;
}
•Animal is the base class with a method eat().
•Dog is the derived class inheriting from Animal.
•Dog adds its own method bark().
•main() function demonstrates the use of the
derived class Dog.
Multiple Inheritance
• Multiple inheritance is a feature in object-
oriented programming where a class can
inherit attributes and methods from more
than one base class.
Multiple Inheritance Syntax
class BaseClass1
{
// Base class 1 members and methods
};
class BaseClass2
{
// Base class 2 members and methods
};
class DerivedClass : accessSpecifier BaseClass1, accessSpecifier BaseClass2
{
// Derived class members and methods
};
Multiple
Inheritance
Example
#include <iostream>
using namespace std;
// Base class representing a vehicle's movement capabilities
class Movement {
public:
void move() {
cout << "Vehicle is movingn";
}
};
// Base class representing a vehicle's fuel-related functionalities
class Fuel {
public:
void refillFuel() {
cout << "Refilling fueln";
}
};
// Derived class representing a car, which inherits from both Movement and Fuel
class Car : public Movement, public Fuel {
public:
void honk() {
cout << "Car is honkingn";
}
};
int main() {
Car myCar;
myCar.move(); // Accessing method from Movement
myCar.refillFuel(); // Accessing method from Fuel
myCar.honk(); // Accessing method from Car
return 0;
}
Multilevel Inheritance
• Multilevel inheritance is a feature in object-
oriented programming where a class serves as
a base class for another class, and that derived
class, in turn, serves as the base class for yet
another class.
Multilevel Inheritance Syntax
class BaseClass
{
// Base class members and methods
};
class DerivedClass1 : accessSpecifier BaseClass
{
// Derived class 1 members and methods
};
class DerivedClass2 : accessSpecifier DerivedClass1
{
// Derived class 2 members and methods
};
Multilevel
Sample
Program
#include <iostream>
using namespace std;
// Base class
class Animal
{
public:
void eat() {
cout << "Animal is eating.n";
}
};
// Derived class inheriting from Animal
class Dog : public Animal
{
public:
void bark() {
cout << "Dog is barking.n";
}
};
// Another derived class inheriting from Dog
class Bulldog : public Dog
{
public:
void guard() {
cout << "Bulldog is guarding.n";
}
};
int main() {
Bulldog myBulldog;
myBulldog.eat(); // Accessing method from Animal
myBulldog.bark(); // Accessing method from Dog
myBulldog.guard(); // Accessing method from Bulldog
return 0;
}
Hierarchical Inheritance
• Hierarchical inheritance is a type of inheritance in
C++ where more than one derived class inherits from
a single base class.
• In hierarchical inheritance, the derived classes share
common features inherited from the same base
class, but they may have their own unique attributes
or behaviors.
Hierarchical Inheritance Syntax
class BaseClass
{
// Base class members and methods
};
class DerivedClass1 : accessSpecifier BaseClass
{
// Derived class 1 members and methods
};
class DerivedClass2 : accessSpecifier BaseClass
{
// Derived class 2 members and methods
};
Hierarchical
Inheritance
Sample
Program
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
void draw() {
cout << "Drawing a shapen";
}
};
// Derived class 1 inheriting from Shape
class Circle : public Shape {
public:
void drawCircle() {
cout << "Drawing a circlen";
}
};
// Derived class 2 inheriting from Shape
class Rectangle : public Shape {
public:
void drawRectangle() {
cout << "Drawing a rectanglen";
}
};
int main() {
Circle circle;
circle.draw(); // Accessing method from Shape
circle.drawCircle(); // Accessing method from Circle
Rectangle rectangle;
rectangle.draw(); // Accessing method from Shape
rectangle.drawRectangle(); // Accessing method from Rectangle
return 0;
}
Hybrid Inheritance
• Hybrid inheritance is a combination of multiple types
of inheritance, typically mixing single inheritance,
multiple inheritance, and/or multilevel inheritance.
• It forms a complex inheritance hierarchy. This type of
inheritance can lead to the diamond problem, which
arises when two base classes of a class have a
common base class themselves.
Hybrid Inheritance Syntax
class BaseClass1
{
// Base class 1 members and methods
};
class BaseClass2
{
// Base class 2 members and methods
};
class DerivedClass1 : accessSpecifier BaseClass1
{
// Derived class 1 members and methods
};
class DerivedClass2 : accessSpecifier BaseClass1, accessSpecifier BaseClass2
{
// Derived class 2 members and methods
};
class DerivedClass3 : accessSpecifier DerivedClass1, accessSpecifier DerivedClass2
{
// Derived class 3 members and methods
};
#include <iostream>
using namespace std;
// Base class 1
class Animal {
public:
void eat() {
cout << "Animal is eating.n";
}
};
// Base class 2
class Machine {
public:
void work() {
cout << "Machine is working.n";
}
};
// Derived class 1 inheriting from Animal (Single Inheritance)
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking.n";
}
};
// Derived class 2 inheriting from Machine
(Single Inheritance)
class Car : public Machine {
public:
void drive() {
cout << "Car is driving.n";
}
};
// Derived class 3 inheriting from both Dog
and Car (Multiple Inheritance)
class RobotDogCar : public Dog, public Car {
public:
void move() {
cout << "RobotDogCar is moving.n";
}
};
// Derived class inheriting from RobotDogCar
(Multilevel Inheritance)
class SuperRobot : public RobotDogCar {
public:
void fly() {
cout << "SuperRobot is flying.n";
}
};
int main() {
SuperRobot mySuperRobot;
mySuperRobot.eat(); // Accessing method from Animal (via Dog)
mySuperRobot.work(); // Accessing method from Machine (via Car)
mySuperRobot.bark(); // Accessing method from Dog
mySuperRobot.drive(); // Accessing method from Car
mySuperRobot.move(); // Accessing method from RobotDogCar
mySuperRobot.fly(); // Accessing method from SuperRobot
return 0;
}
Constructor in Inheritance
• Constructors in inheritance play a crucial role
in initializing base class and derived class
objects.
• In C++, when you create a derived class object,
the constructors of both the base class and
derived class are called in a certain order.
• The base class constructor is called before the
derived class constructor.
Constructor in Inheritance
• Base Class Constructor
– When a derived class object is created, the
constructor of the base class is invoked first to
initialize the base class part of the object.
– This happens automatically before the derived class
constructor body executes.
• Derived Class Constructor:
– After the base class constructor completes its
execution, the derived class constructor is invoked.
– It can initialize the derived class members and
perform additional initialization tasks specific to the
derived class.
Constructor in Inheritance Example
#include <iostream>
using namespace std;
// Base class
class Base
{
public:
Base() {
cout << "Base class constructorn";
}
};
// Derived class
class Derived : public Base
{
public:
Derived() {
cout << "Derived class constructorn";
}
};
int main() {
Derived d; // Creating an object of the derived class
return 0;
}
When an object of the Derived class is
created in the main() function, the
constructor of the Base class is called
first, followed by the constructor of the
Derived class.
Advanced Functions
• Inline
• Friend
• Virtual - Pure Virtual function
Inline Functions
• In C++, the inline keyword is used to suggest the
compiler to perform inline expansion of a function.
• When a function is declared as inline, the compiler
replaces the function call with the actual function
code during the compilation process, thereby
avoiding the overhead of function call and improving
performance.
Syntax:
inline returnType functionName(parameters)
{
// Function body
}
Inline Functions Example
#include <iostream>
using namespace std;
// Inline function definition
inline int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(3, 5); // Function call
cout << "Result: " << result << endl;
return 0;
}
Inline Functions Features
• Inline functions are typically used for small functions.
• They are often defined in header files to allow for
inclusion in multiple source files without violating
the One Definition Rule.
• Using inline for large or complex functions may result
in larger executable size and may not yield
performance benefits.
• Functions defined inside class definitions are
implicitly inline.
Friend Functions
• In C++, a friend function is a function that is
not a member of a class but has access to the
private and protected members of the class.
• It is declared within the class using the friend
keyword.
• Friend functions are useful when you need to
allow an external function to access private or
protected data of a class without making that
function a member of the class.
Friend Functions Syntax
class ClassName
{
private:
// Private members
public:
// Public members
// Declaration of friend function
friend returnType functionName(parameters);
};
// Definition of the friend function
returnType functionName(parameters)
{
// Function body
}
Features of friend functions
• Access to Private and Protected Members
• Non-Member Functions
• Declaration Inside Class
• Symmetric Relationship
• Granular Access Control
• No Inheritance Implications
• Friendship is Not Transitive
• Use with Operator Overloading
Friend Function
example
#include <iostream>
using namespace std;
// Forward declaration of class Rectangle
class Rectangle;
// Class Square
class Square {
private:
int side;
public:
Square(int s) : side(s) {}
// Declaration of friend function
friend void compareArea(const Square& sq, const
Rectangle& rect);
};
// Class Rectangle
class Rectangle {
private:
int length;
int width;
public:
Rectangle(int l, int w) : length(l), width(w) {}
// Declaration of friend function
friend void compareArea(const Square& sq, const
Rectangle& rect);
};
// Definition of friend function
void compareArea(const Square& sq, const Rectangle& rect) {
int areaSq = sq.side * sq.side;
int areaRect = rect.length * rect.width;
if (areaSq > areaRect) {
cout << "Area of square is greater than area of rectanglen";
} else if (areaSq < areaRect) {
cout << "Area of rectangle is greater than area of squaren";
} else {
cout << "Area of square is equal to area of rectanglen";
}
}
int main() {
Square square(5);
Rectangle rectangle(4, 6);
// Call to the friend function
compareArea(square, rectangle);
return 0;
}
Virtual function
• In C++, a virtual function is a member function
declared within a base class that can be
overridden by derived classes.
• When a virtual function is called through a
base class pointer or reference pointing to a
derived class object, the appropriate version
of the function is invoked based on the actual
object type.
Virtual function
• Declaration: To declare a function as virtual, use
the virtual keyword in the base class.
• Override: Derived classes can override the virtual
function by providing their own implementation
of the function with the same signature.
• Dynamic Binding: The appropriate version of the
virtual function is determined at runtime based
on the type of the object being referred to, not
the type of the pointer or reference.
Virtual function
Example
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void area()
{
cout<<"Area of shape"<<endl;
}
};
class Rectangle : public Shape
{
public:
void area()
{
cout<<"Area of Rectangle"<<endl;
}
};
class Circle : public Shape
{
public:
void area()
{
cout<<"Area of Circle"<<endl;
}
};
int main()
{
Rectangle rect;
Circle cir;
Shape* sp;
sp = &rect;
sp->area();
sp = &cir;
sp->area();
return 0;
}
Pure Virtual function
• A pure virtual function is a virtual function that has no
implementation in the base class and is meant to be
overridden by derived classes.
• It serves as a placeholder for the derived classes to
provide their own implementations.
• A class containing at least one pure virtual function
becomes an abstract base class, and instances of
abstract base classes cannot be created.
• Derived classes must provide an implementation for all
pure virtual functions to be considered concrete
classes.
Virtual function
Example
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void area() =0;
};
class Rectangle : public Shape
{
public:
void area() override
{
cout<<"Area of Rectangle"<<endl;
}
};
class Circle : public Shape
{
public:
void area() override
{
cout<<"Area of Circle"<<endl;
}
};
int main()
{
Rectangle rect;
Circle cir;
Shape* sp;
sp = &rect;
sp->area();
sp = &cir;
sp->area();
return 0;
}
Abstract class
• In C++, an abstract class is a class that cannot
be instantiated on its own.
• It serves as a blueprint for other classes,
defining a common interface through which
derived classes can inherit functionality.
• A class containing at least one pure virtual
function becomes an abstract base class, and
instances of abstract base classes cannot be
created.
UML State Chart Diagram
• Depicts the various states that an object can
be in during its lifetime and the transitions
between those states
• Used for modeling the dynamic behavior of a
system or component
• Used for showing how it responds to events
and changes over time
Components of a UML State Chart
Diagram
• States: Represents the various conditions or situations in which an object
or system can exist. States are typically depicted as rounded rectangles
with the state name inside.
• Transitions: Represents the movement from one state to another in
response to events or triggers. Transitions are usually depicted as arrows
connecting states, with labels indicating the events or conditions that
cause the transition.
• Events: External stimuli or occurrences that trigger a transition from one
state to another. Events are usually represented by labels on transitions.
• Actions: Activities or behaviors that occur when a transition is triggered.
Actions are often associated with transitions and can be depicted as labels
or separate elements.
• Initial State: Represents the starting point of the state machine. It's
typically depicted as a filled circle.
• Final State: Represents the ending point of the state machine or the
termination of a process. It's usually depicted as a filled circle surrounded
by a larger circle.
State Chart Diagram
State
Chart
Diagram
UML Activity Diagram
• Depicts the flow of control or workflow of a
system, process, or algorithm
• Illustrates the sequence of activities or actions
that need to be performed and the order in
which they occur
• Used for modeling business processes,
workflow systems, use cases, and software
behaviors.
Components of a UML Activity Diagram
• Activity
– Represents a specific action, operation, or step in the workflow. Activities are typically
depicted as rounded rectangles with the activity name inside.
• Control Flow
– Represents the sequence of activities or actions and the order in which they occur. Control
flow arrows (also called control edges) connect activities, indicating the flow of control from
one activity to another.
• Decision Nodes
– Represents a decision point where the flow of control can diverge based on certain conditions
or criteria. Decision nodes are depicted as diamonds, and control flow arrows leaving the
decision node represent different possible outcomes or paths.
• Merge Nodes
– Represents a point where multiple control flows converge back into a single flow. Merge
nodes are depicted as diamonds with multiple incoming control flow arrows.
• Initial Node
– Represents the starting point of the activity diagram. It's typically depicted as a filled circle.
• Final Node
– Represents the ending point of the activity diagram or the completion of the process. It's
usually depicted as a filled circle surrounded by a larger circle.
• Fork and Join Nodes
– Represents parallel execution of activities. Fork nodes split the control flow into multiple
concurrent flows, while join nodes synchronize the concurrent flows back into a single flow.
• Object Nodes
– Represents objects or data involved in activities. Object nodes can be used to show the input,
output, or intermediate data of activities
UML Activity Diagram
Components
Activity
Control
Flow
Decision
Nodes
Merge
Nodes
Initial Node
Final Node
Fork and
Join Nodes
Object
Nodes
UML Activity Diagram
UML Activity Diagram Example
UML Activity Diagram Example
UML Activity Diagram Example
UML Activity Diagram Example
Ad

More Related Content

Similar to Object Oriented Design and Programming Unit-03 (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
sanketkashyap2023
 
Week 8 - OOP Inheritance11111111111.pptx
Week 8 - OOP Inheritance11111111111.pptxWeek 8 - OOP Inheritance11111111111.pptx
Week 8 - OOP Inheritance11111111111.pptx
NajamUlHassan73
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
AteeqaKokab1
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
NAVANEETCHATURVEDI2
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
JP2B1197685ARamSaiPM
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
Inheritance
Inheritance Inheritance
Inheritance
sourav verma
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
MananPasricha
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
sushamaGavarskar1
 
Inheritance
InheritanceInheritance
Inheritance
zindadili
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
SadiqullahGhani1
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
TYPES OF INHERITANCE CONCEPT IN C++.pptx
TYPES OF INHERITANCE CONCEPT IN C++.pptxTYPES OF INHERITANCE CONCEPT IN C++.pptx
TYPES OF INHERITANCE CONCEPT IN C++.pptx
SPECIALISESPECIALISE
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
LalfakawmaKh
 
UNIT 4.pdf.........................................
UNIT 4.pdf.........................................UNIT 4.pdf.........................................
UNIT 4.pdf.........................................
nikhithanikky1212
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
sanketkashyap2023
 
Week 8 - OOP Inheritance11111111111.pptx
Week 8 - OOP Inheritance11111111111.pptxWeek 8 - OOP Inheritance11111111111.pptx
Week 8 - OOP Inheritance11111111111.pptx
NajamUlHassan73
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
MananPasricha
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
TYPES OF INHERITANCE CONCEPT IN C++.pptx
TYPES OF INHERITANCE CONCEPT IN C++.pptxTYPES OF INHERITANCE CONCEPT IN C++.pptx
TYPES OF INHERITANCE CONCEPT IN C++.pptx
SPECIALISESPECIALISE
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
LalfakawmaKh
 
UNIT 4.pdf.........................................
UNIT 4.pdf.........................................UNIT 4.pdf.........................................
UNIT 4.pdf.........................................
nikhithanikky1212
 

More from Sivakumar M (12)

Introduction to Operating Systems and its basics
Introduction to Operating Systems and its basicsIntroduction to Operating Systems and its basics
Introduction to Operating Systems and its basics
Sivakumar M
 
Operating Systems Process Management.pptx
Operating Systems Process Management.pptxOperating Systems Process Management.pptx
Operating Systems Process Management.pptx
Sivakumar M
 
Operating Systems Protection and Security
Operating Systems Protection and SecurityOperating Systems Protection and Security
Operating Systems Protection and Security
Sivakumar M
 
Operating Systems CPU Scheduling and its Algorithms
Operating Systems CPU Scheduling and its AlgorithmsOperating Systems CPU Scheduling and its Algorithms
Operating Systems CPU Scheduling and its Algorithms
Sivakumar M
 
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-4
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-418CSC311J WEB DESIGN AND DEVELOPMENT UNIT-4
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-4
Sivakumar M
 
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-2
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-218CSC311J WEB DESIGN AND DEVELOPMENT UNIT-2
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-2
Sivakumar M
 
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-118CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
Sivakumar M
 
18CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-318CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-3
Sivakumar M
 
Object Oriented Design and Programming Unit-05
Object Oriented Design and Programming Unit-05Object Oriented Design and Programming Unit-05
Object Oriented Design and Programming Unit-05
Sivakumar M
 
Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04
Sivakumar M
 
Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02
Sivakumar M
 
Object Oriented Design and Programming Unit-01
Object Oriented Design and Programming  Unit-01Object Oriented Design and Programming  Unit-01
Object Oriented Design and Programming Unit-01
Sivakumar M
 
Introduction to Operating Systems and its basics
Introduction to Operating Systems and its basicsIntroduction to Operating Systems and its basics
Introduction to Operating Systems and its basics
Sivakumar M
 
Operating Systems Process Management.pptx
Operating Systems Process Management.pptxOperating Systems Process Management.pptx
Operating Systems Process Management.pptx
Sivakumar M
 
Operating Systems Protection and Security
Operating Systems Protection and SecurityOperating Systems Protection and Security
Operating Systems Protection and Security
Sivakumar M
 
Operating Systems CPU Scheduling and its Algorithms
Operating Systems CPU Scheduling and its AlgorithmsOperating Systems CPU Scheduling and its Algorithms
Operating Systems CPU Scheduling and its Algorithms
Sivakumar M
 
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-4
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-418CSC311J WEB DESIGN AND DEVELOPMENT UNIT-4
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-4
Sivakumar M
 
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-2
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-218CSC311J WEB DESIGN AND DEVELOPMENT UNIT-2
18CSC311J WEB DESIGN AND DEVELOPMENT UNIT-2
Sivakumar M
 
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-118CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
Sivakumar M
 
18CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-318CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-3
Sivakumar M
 
Object Oriented Design and Programming Unit-05
Object Oriented Design and Programming Unit-05Object Oriented Design and Programming Unit-05
Object Oriented Design and Programming Unit-05
Sivakumar M
 
Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04
Sivakumar M
 
Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02
Sivakumar M
 
Object Oriented Design and Programming Unit-01
Object Oriented Design and Programming  Unit-01Object Oriented Design and Programming  Unit-01
Object Oriented Design and Programming Unit-01
Sivakumar M
 
Ad

Recently uploaded (20)

How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
Taqyea
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Journal of Soft Computing in Civil Engineering
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
roshinijoga
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
Taqyea
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
roshinijoga
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
Ad

Object Oriented Design and Programming Unit-03

  • 1. 21CSC101T OBJECT ORIENTED DESIGN AND PROGRAMMING Dr.M.Sivakumar AP/NWC SRMIST
  • 2. Course Outcomes (CO) At the end of this course, learners will be able to: • CO-1: Create programs using object-oriented approach and design methodologies • CO-2: Construct programs using method overloading and operator overloading • CO-3: Create programs using inline, friend and virtual functions, construct programs using standard templates • CO-4: Construct programs using exceptional handling and collections • CO-5: Create Models of the system using UML Diagrams
  • 3. Unit-3 - Inheritance Inheritance – Types -Single and Multiple Inheritance - Multilevel Inheritance - Hierarchical Inheritance - Hybrid Inheritance - Advanced Functions - Inline, Friend- Virtual - Pure Virtual function - Abstract class – UML State Chart Diagram - UML Activity Diagram
  • 4. Inheritance • the mechanism by which a class can inherit properties (fields) and behaviors (methods) from another class. • enables code reuse and promotes modularity and extensibility in software development • establishes an "is-a" relationship between classes, where a subclass (or derived class) inherits characteristics from a superclass (or base class). • The subclass inherits all non-private members (methods and fields) of the superclass, allowing it to reuse the code defined in the superclass.
  • 9. Inheritance • Syntax: class DerivedClass : accessSpecifier BaseClass { // Derived class members and methods }; • DerivedClass – the name of the derived class. • accessSpecifier – specifies the access level for the inherited members. – It can be public, protected, or private. – If not explicitly specified, it defaults to private. • BaseClass – the name of the base class being inherited from.
  • 10. Inheritance • Public – Public members of the base class remain public in the derived class. – They are accessible from outside the class hierarchy. • Protected – Public members of the base class become protected in the derived class. – They are accessible within the derived class and its subclasses but not from outside. • Private – Public members of the base class become private in the derived class. – They are not accessible from the derived class or outside.
  • 11. Inheritance class Base { public: int publicMember; protected: int protectedMember; private: int privateMember; }; class DerivedPublic : public Base { // publicMember is accessible // protectedMember is accessible // privateMember is not accessible }; class DerivedProtected : protected Base { // publicMember is accessible // protectedMember is accessible // privateMember is not accessible }; class DerivedPrivate : private Base { // publicMember is accessible // protectedMember is accessible // privateMember is not accessible };
  • 12. Types
  • 13. Types • Single Inheritance • Multiple Inheritance • Multilevel Inheritance • Hierarchical Inheritance • Hybrid Inheritance
  • 14. Single Inheritance • Single inheritance refers to the inheritance mechanism where a derived class inherits from only one base class.
  • 15. Single Inheritance Syntax class BaseClass { // Base class members and methods }; class DerivedClass : accessSpecifier BaseClass { // Derived class members and methods };
  • 16. Single Inheritance Example #include <iostream> using namespace std; // Base class class Animal { public: void eat() { cout << "Animal is eating.n"; } }; // Derived class inheriting from Animal class Dog : public Animal { public: void bark() { cout << "Dog is barking.n"; } }; int main() { Dog myDog; myDog.eat(); // Output: Animal is eating. myDog.bark(); // Output: Dog is barking. return 0; } •Animal is the base class with a method eat(). •Dog is the derived class inheriting from Animal. •Dog adds its own method bark(). •main() function demonstrates the use of the derived class Dog.
  • 17. Multiple Inheritance • Multiple inheritance is a feature in object- oriented programming where a class can inherit attributes and methods from more than one base class.
  • 18. Multiple Inheritance Syntax class BaseClass1 { // Base class 1 members and methods }; class BaseClass2 { // Base class 2 members and methods }; class DerivedClass : accessSpecifier BaseClass1, accessSpecifier BaseClass2 { // Derived class members and methods };
  • 19. Multiple Inheritance Example #include <iostream> using namespace std; // Base class representing a vehicle's movement capabilities class Movement { public: void move() { cout << "Vehicle is movingn"; } }; // Base class representing a vehicle's fuel-related functionalities class Fuel { public: void refillFuel() { cout << "Refilling fueln"; } }; // Derived class representing a car, which inherits from both Movement and Fuel class Car : public Movement, public Fuel { public: void honk() { cout << "Car is honkingn"; } }; int main() { Car myCar; myCar.move(); // Accessing method from Movement myCar.refillFuel(); // Accessing method from Fuel myCar.honk(); // Accessing method from Car return 0; }
  • 20. Multilevel Inheritance • Multilevel inheritance is a feature in object- oriented programming where a class serves as a base class for another class, and that derived class, in turn, serves as the base class for yet another class.
  • 21. Multilevel Inheritance Syntax class BaseClass { // Base class members and methods }; class DerivedClass1 : accessSpecifier BaseClass { // Derived class 1 members and methods }; class DerivedClass2 : accessSpecifier DerivedClass1 { // Derived class 2 members and methods };
  • 22. Multilevel Sample Program #include <iostream> using namespace std; // Base class class Animal { public: void eat() { cout << "Animal is eating.n"; } }; // Derived class inheriting from Animal class Dog : public Animal { public: void bark() { cout << "Dog is barking.n"; } }; // Another derived class inheriting from Dog class Bulldog : public Dog { public: void guard() { cout << "Bulldog is guarding.n"; } }; int main() { Bulldog myBulldog; myBulldog.eat(); // Accessing method from Animal myBulldog.bark(); // Accessing method from Dog myBulldog.guard(); // Accessing method from Bulldog return 0; }
  • 23. Hierarchical Inheritance • Hierarchical inheritance is a type of inheritance in C++ where more than one derived class inherits from a single base class. • In hierarchical inheritance, the derived classes share common features inherited from the same base class, but they may have their own unique attributes or behaviors.
  • 24. Hierarchical Inheritance Syntax class BaseClass { // Base class members and methods }; class DerivedClass1 : accessSpecifier BaseClass { // Derived class 1 members and methods }; class DerivedClass2 : accessSpecifier BaseClass { // Derived class 2 members and methods };
  • 25. Hierarchical Inheritance Sample Program #include <iostream> using namespace std; // Base class class Shape { public: void draw() { cout << "Drawing a shapen"; } }; // Derived class 1 inheriting from Shape class Circle : public Shape { public: void drawCircle() { cout << "Drawing a circlen"; } }; // Derived class 2 inheriting from Shape class Rectangle : public Shape { public: void drawRectangle() { cout << "Drawing a rectanglen"; } }; int main() { Circle circle; circle.draw(); // Accessing method from Shape circle.drawCircle(); // Accessing method from Circle Rectangle rectangle; rectangle.draw(); // Accessing method from Shape rectangle.drawRectangle(); // Accessing method from Rectangle return 0; }
  • 26. Hybrid Inheritance • Hybrid inheritance is a combination of multiple types of inheritance, typically mixing single inheritance, multiple inheritance, and/or multilevel inheritance. • It forms a complex inheritance hierarchy. This type of inheritance can lead to the diamond problem, which arises when two base classes of a class have a common base class themselves.
  • 27. Hybrid Inheritance Syntax class BaseClass1 { // Base class 1 members and methods }; class BaseClass2 { // Base class 2 members and methods }; class DerivedClass1 : accessSpecifier BaseClass1 { // Derived class 1 members and methods }; class DerivedClass2 : accessSpecifier BaseClass1, accessSpecifier BaseClass2 { // Derived class 2 members and methods }; class DerivedClass3 : accessSpecifier DerivedClass1, accessSpecifier DerivedClass2 { // Derived class 3 members and methods };
  • 28. #include <iostream> using namespace std; // Base class 1 class Animal { public: void eat() { cout << "Animal is eating.n"; } }; // Base class 2 class Machine { public: void work() { cout << "Machine is working.n"; } }; // Derived class 1 inheriting from Animal (Single Inheritance) class Dog : public Animal { public: void bark() { cout << "Dog is barking.n"; } }; // Derived class 2 inheriting from Machine (Single Inheritance) class Car : public Machine { public: void drive() { cout << "Car is driving.n"; } }; // Derived class 3 inheriting from both Dog and Car (Multiple Inheritance) class RobotDogCar : public Dog, public Car { public: void move() { cout << "RobotDogCar is moving.n"; } }; // Derived class inheriting from RobotDogCar (Multilevel Inheritance) class SuperRobot : public RobotDogCar { public: void fly() { cout << "SuperRobot is flying.n"; } };
  • 29. int main() { SuperRobot mySuperRobot; mySuperRobot.eat(); // Accessing method from Animal (via Dog) mySuperRobot.work(); // Accessing method from Machine (via Car) mySuperRobot.bark(); // Accessing method from Dog mySuperRobot.drive(); // Accessing method from Car mySuperRobot.move(); // Accessing method from RobotDogCar mySuperRobot.fly(); // Accessing method from SuperRobot return 0; }
  • 30. Constructor in Inheritance • Constructors in inheritance play a crucial role in initializing base class and derived class objects. • In C++, when you create a derived class object, the constructors of both the base class and derived class are called in a certain order. • The base class constructor is called before the derived class constructor.
  • 31. Constructor in Inheritance • Base Class Constructor – When a derived class object is created, the constructor of the base class is invoked first to initialize the base class part of the object. – This happens automatically before the derived class constructor body executes. • Derived Class Constructor: – After the base class constructor completes its execution, the derived class constructor is invoked. – It can initialize the derived class members and perform additional initialization tasks specific to the derived class.
  • 32. Constructor in Inheritance Example #include <iostream> using namespace std; // Base class class Base { public: Base() { cout << "Base class constructorn"; } }; // Derived class class Derived : public Base { public: Derived() { cout << "Derived class constructorn"; } }; int main() { Derived d; // Creating an object of the derived class return 0; } When an object of the Derived class is created in the main() function, the constructor of the Base class is called first, followed by the constructor of the Derived class.
  • 33. Advanced Functions • Inline • Friend • Virtual - Pure Virtual function
  • 34. Inline Functions • In C++, the inline keyword is used to suggest the compiler to perform inline expansion of a function. • When a function is declared as inline, the compiler replaces the function call with the actual function code during the compilation process, thereby avoiding the overhead of function call and improving performance. Syntax: inline returnType functionName(parameters) { // Function body }
  • 35. Inline Functions Example #include <iostream> using namespace std; // Inline function definition inline int add(int a, int b) { return a + b; } int main() { int result = add(3, 5); // Function call cout << "Result: " << result << endl; return 0; }
  • 36. Inline Functions Features • Inline functions are typically used for small functions. • They are often defined in header files to allow for inclusion in multiple source files without violating the One Definition Rule. • Using inline for large or complex functions may result in larger executable size and may not yield performance benefits. • Functions defined inside class definitions are implicitly inline.
  • 37. Friend Functions • In C++, a friend function is a function that is not a member of a class but has access to the private and protected members of the class. • It is declared within the class using the friend keyword. • Friend functions are useful when you need to allow an external function to access private or protected data of a class without making that function a member of the class.
  • 38. Friend Functions Syntax class ClassName { private: // Private members public: // Public members // Declaration of friend function friend returnType functionName(parameters); }; // Definition of the friend function returnType functionName(parameters) { // Function body }
  • 39. Features of friend functions • Access to Private and Protected Members • Non-Member Functions • Declaration Inside Class • Symmetric Relationship • Granular Access Control • No Inheritance Implications • Friendship is Not Transitive • Use with Operator Overloading
  • 40. Friend Function example #include <iostream> using namespace std; // Forward declaration of class Rectangle class Rectangle; // Class Square class Square { private: int side; public: Square(int s) : side(s) {} // Declaration of friend function friend void compareArea(const Square& sq, const Rectangle& rect); }; // Class Rectangle class Rectangle { private: int length; int width; public: Rectangle(int l, int w) : length(l), width(w) {} // Declaration of friend function friend void compareArea(const Square& sq, const Rectangle& rect); }; // Definition of friend function void compareArea(const Square& sq, const Rectangle& rect) { int areaSq = sq.side * sq.side; int areaRect = rect.length * rect.width; if (areaSq > areaRect) { cout << "Area of square is greater than area of rectanglen"; } else if (areaSq < areaRect) { cout << "Area of rectangle is greater than area of squaren"; } else { cout << "Area of square is equal to area of rectanglen"; } } int main() { Square square(5); Rectangle rectangle(4, 6); // Call to the friend function compareArea(square, rectangle); return 0; }
  • 41. Virtual function • In C++, a virtual function is a member function declared within a base class that can be overridden by derived classes. • When a virtual function is called through a base class pointer or reference pointing to a derived class object, the appropriate version of the function is invoked based on the actual object type.
  • 42. Virtual function • Declaration: To declare a function as virtual, use the virtual keyword in the base class. • Override: Derived classes can override the virtual function by providing their own implementation of the function with the same signature. • Dynamic Binding: The appropriate version of the virtual function is determined at runtime based on the type of the object being referred to, not the type of the pointer or reference.
  • 43. Virtual function Example #include <iostream> using namespace std; class Shape { public: virtual void area() { cout<<"Area of shape"<<endl; } }; class Rectangle : public Shape { public: void area() { cout<<"Area of Rectangle"<<endl; } }; class Circle : public Shape { public: void area() { cout<<"Area of Circle"<<endl; } }; int main() { Rectangle rect; Circle cir; Shape* sp; sp = &rect; sp->area(); sp = &cir; sp->area(); return 0; }
  • 44. Pure Virtual function • A pure virtual function is a virtual function that has no implementation in the base class and is meant to be overridden by derived classes. • It serves as a placeholder for the derived classes to provide their own implementations. • A class containing at least one pure virtual function becomes an abstract base class, and instances of abstract base classes cannot be created. • Derived classes must provide an implementation for all pure virtual functions to be considered concrete classes.
  • 45. Virtual function Example #include <iostream> using namespace std; class Shape { public: virtual void area() =0; }; class Rectangle : public Shape { public: void area() override { cout<<"Area of Rectangle"<<endl; } }; class Circle : public Shape { public: void area() override { cout<<"Area of Circle"<<endl; } }; int main() { Rectangle rect; Circle cir; Shape* sp; sp = &rect; sp->area(); sp = &cir; sp->area(); return 0; }
  • 46. Abstract class • In C++, an abstract class is a class that cannot be instantiated on its own. • It serves as a blueprint for other classes, defining a common interface through which derived classes can inherit functionality. • A class containing at least one pure virtual function becomes an abstract base class, and instances of abstract base classes cannot be created.
  • 47. UML State Chart Diagram • Depicts the various states that an object can be in during its lifetime and the transitions between those states • Used for modeling the dynamic behavior of a system or component • Used for showing how it responds to events and changes over time
  • 48. Components of a UML State Chart Diagram • States: Represents the various conditions or situations in which an object or system can exist. States are typically depicted as rounded rectangles with the state name inside. • Transitions: Represents the movement from one state to another in response to events or triggers. Transitions are usually depicted as arrows connecting states, with labels indicating the events or conditions that cause the transition. • Events: External stimuli or occurrences that trigger a transition from one state to another. Events are usually represented by labels on transitions. • Actions: Activities or behaviors that occur when a transition is triggered. Actions are often associated with transitions and can be depicted as labels or separate elements. • Initial State: Represents the starting point of the state machine. It's typically depicted as a filled circle. • Final State: Represents the ending point of the state machine or the termination of a process. It's usually depicted as a filled circle surrounded by a larger circle.
  • 51. UML Activity Diagram • Depicts the flow of control or workflow of a system, process, or algorithm • Illustrates the sequence of activities or actions that need to be performed and the order in which they occur • Used for modeling business processes, workflow systems, use cases, and software behaviors.
  • 52. Components of a UML Activity Diagram • Activity – Represents a specific action, operation, or step in the workflow. Activities are typically depicted as rounded rectangles with the activity name inside. • Control Flow – Represents the sequence of activities or actions and the order in which they occur. Control flow arrows (also called control edges) connect activities, indicating the flow of control from one activity to another. • Decision Nodes – Represents a decision point where the flow of control can diverge based on certain conditions or criteria. Decision nodes are depicted as diamonds, and control flow arrows leaving the decision node represent different possible outcomes or paths. • Merge Nodes – Represents a point where multiple control flows converge back into a single flow. Merge nodes are depicted as diamonds with multiple incoming control flow arrows. • Initial Node – Represents the starting point of the activity diagram. It's typically depicted as a filled circle. • Final Node – Represents the ending point of the activity diagram or the completion of the process. It's usually depicted as a filled circle surrounded by a larger circle. • Fork and Join Nodes – Represents parallel execution of activities. Fork nodes split the control flow into multiple concurrent flows, while join nodes synchronize the concurrent flows back into a single flow. • Object Nodes – Represents objects or data involved in activities. Object nodes can be used to show the input, output, or intermediate data of activities
  翻译: