Unit-3 Oodp
Unit-3 Oodp
Session Topics
1 Inheritance – Types
2 Single and Multiple Inheritance
3 Multilevel Inheritance - Hierarchical Inheritance
4 Hybrid Inheritance
5 Advanced Functions - Inline, Friend
6 Virtual - Pure Virtual function
7 Abstract class
8 UML State Chart Diagrams
9 UML Activity Diagram
INHERITANCE
/*Inheritance Demo*/
#include <iostream>
using namespace std;
// Base Class
class Person {
protected:
string name;
int age;
public:
// Constructor
Person(string n, int a) : name(n), age(a) {}
public:
// Constructor (calling the base class constructor)
Student(string n, int a, int id) : Person(n, a), studentID(id) {}
int main() {
// Create a Student object
Student stud("Rahul", 20, 1001);
return 0;
}
Types of Inheritance
● Single Inheritance
● Multilevel Inheritance
● Hierarchical Inheritance
● Multiple Inheritance
● Hybrid Inheritance
Single Inheritance
In single inheritance an object or class may only inherit from one particular object or class.
Multilevel Inheritance
Multilevel inheritance is a type of inheritance where a class is derived from a class that is also
derived from another class. This forms a hierarchical chain where properties and methods are
passed from one class to the next through multiple levels.
Hierarchical Inheritance
Hierarchical inheritance is a type of inheritance where multiple child classes inherit from a
single base class. This forms a structure where one class is shared as a common parent for
several derived classes.
Multiple Inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in
which an object or class can inherit characteristics and features from more than one parent
object or parent class.
Multiple inheritance leads to the “Diamond Inheritance” problem. C++ uses the Virtual
Inheritance concept to solve this problem. Virtual inheritance is a C++ technique that ensures
only one copy of a base class's member variables are inherited by grandchild derived classes.
Without virtual inheritance, if two classes B and C inherit from a class A, and a class D inherits
from both B and C, then D will contain two copies of A's member variables: one via B, and
one via C.
Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance (single, multiple,
multilevel, or hierarchical) in a single program. It allows for a more flexible structure but can
introduce complexity and ambiguity, especially with diamond problems (when a class inherits
from two classes that both inherit from a common base class).
DYNAMIC POLYMORPHISM
○ It follows the Open/Closed Principle. It means that the system is open for
extension but closed for modification (i.e., no changes required in existing
code).
● Flexibility
○ It is the ability to change the implementation.
○ Derived class objects can be substituted where base class objects are expected
(Liskov Substitution Principle).
Virtual Functions
● A virtual function is a member function whose behavior can be redefined (overridden)
in derived classes.
● But it is not compulsory for a normal virtual function to get overridden in derived
class.
#include <iostream>
using namespace std;
class Batsman {
protected:
float average;
public:
Batsman(float average){
this->average = average;
}
public:
AggressiveBatsman(float average, float strikeRate):
Batsman(average),strikeRate(strikeRate) {}
void scoreRuns(){
cout<<"I bat aggressively"<<endl;
cout<<"My average is:"<<average<<endl;
cout<<"My strike rate is:"<<strikeRate<<endl;
}
};
int main(){
Batsman *B, BRef(56.67);
AggressiveBatsman AB(48.54,99.97);
B = &AB;
B->scoreRuns();
B = &BRef;
B->scoreRuns();
}
FRIEND FUNCTION
A friend function in C++ is a non-member function that is granted access to the private,
protected, and public members of a class when declared as a friend.
class ClassName
{
friend dataType functionName(arguments);
};
class Travel
{
private:
int speed;
int distance;
public:
void set_values (int a, int b)
{
speed = a;
distance = b;
}
int main ()
{
Travel t;
t.set_values(10, 30);
/*Call the global friend function to calculate the time taken for
the Travel.*/
cout << "Time of Travel: " << findTimeofTravel (t) << " hrs" <<
endl;
return 0;
}
Friend Class
● Classes can also be declared as a friend of another class.
● A friend class is a class all of whose member functions are friend functions of a class,
that is, whose member functions have access to the other class's private and protected
members.
class ClassName
{
friend class FriendClass; // declaring friend class
};
class FriendClass
{
};
class Square
{
private:
int side;
public:
void set_values (int s)
{
side = s;
}
friend class Shape; // friend class
};
class Shape
{
public:
void print_area (Square& s)
{
// Shape is a friend class of Square class.
// It can access the side member of the Square class.
int area = s.side*s.side;
cout<<"The area of the Square is: "<<area<<endl;
}
};
int main ()
{
Square s;
s.set_values(5);
Shape sh;
sh.print_area(s);
return 0;
}
INLINE FUNCTION
● The inline keyword suggests that the compiler substitute the code within the function
definition in place of each call to that function.
● Using inline functions can make your program faster because they eliminate the
overhead associated with function calls.
When to use
● Inline functions are best used for small functions, such as those that provide access to
data members. Short functions are sensitive to the overhead of function calls.
● Longer functions spend proportionately less time in the calling and returning sequence
and benefit less from inlining.
/*Inline Function*/
#include <iostream>
using namespace std;
class Computation{
private:
int num1,num2;
public:
int main(){
Computation comp;
cout<<"Addition:"<<comp.add(12,19)<<endl;
cout<<"Subtraction:"<<comp.sub(12,19)<<endl;
cout<<"Multiplication:"<<comp.mul(12,19)<<endl;
cout<<"Division:"<<comp.div(12,19)<<endl;
}
ABSTRACT CLASS
class Employee{
protected:
int employeeID;
string employeeName;
float experienceNumYears;
public:
virtual void getEmployeeDetails(){
cin>>employeeID;
cin>>employeeName;
cin>>experienceNumYears;
}
virtual void display()=0; //Pure Virtual Function
};
int main(){
Employee *emp;
Doctor doc;
emp=&doc;
emp->getEmployeeDetails();
emp->display();
}
Interface
● Interface: All functions are abstract.
● Abstract class: At least one function is abstract. All the functions need not be abstract.
● Java has both abstract class and interface constructs.
● C++ have only abstract class constructs and doesn't have interface constructs.
● But we can simulate interface in C++ by making all virtual functions pure.
● State Machine Diagrams (formerly called state chart diagrams in UML 1.x) depict the
dynamic behavior of an entity based on its response to events.
● It shows how the entity reacts to various events depending on the current state that it is
in.
ACTIVITY DIAGRAM