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

Unit - 3 OOP 1

This document covers the concept of inheritance in C++ as part of an Object-Oriented Programming course, detailing various types of inheritance such as single, multiple, hybrid, multilevel, and hierarchical inheritance. It also discusses the advantages of inheritance, methods of derivation (public, protected, private), and the concept of aggregation in C++. Additionally, it explains the use of inheritance constructors to simplify object construction in derived classes.

Uploaded by

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

Unit - 3 OOP 1

This document covers the concept of inheritance in C++ as part of an Object-Oriented Programming course, detailing various types of inheritance such as single, multiple, hybrid, multilevel, and hierarchical inheritance. It also discusses the advantages of inheritance, methods of derivation (public, protected, private), and the concept of aggregation in C++. Additionally, it explains the use of inheritance constructors to simplify object construction in derived classes.

Uploaded by

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

OOP Using C++

Unit-3 (CIC-211)
Part-1
Prepared By: Dr. Ankita Sharma
Assistant Professor
Department of Computer Science
GTBIT, GGSIPU
Agenda
1. Inheritance
2. Inheritance Methods
3. Class Hierarchy
4. Derivation-Public, Private & Protected
5. Aggregation
6. Inheritance Constructors

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Inheritance
• Inheritance in C++ is a key concept of object-oriented programming
(OOP) that allows one class to inherit the properties and behaviors
(data members and member functions) of another class.
• The class that inherits is called the derived class (child class), and the
class from which it inherits is called the base class (parent class).
Inheritance promotes code reusability and hierarchical classification.
Inheritance is a process in which one object acquires all the properties
and behaviors of its parent object automatically. In such way, we can
reuse, extend or modify the attributes and behaviors which are defined
in other class.
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Inheritance
class Base {
// Base class members
};
class Derived : access-specifier Base {
// Derived class members
};

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Inheritance
class Base {
// Base class members
};
class Derived : access-specifier Base {
// Derived class members
};
This means that we have created a derived class from the base class in public
mode. Alternatively, we can also derive classes in protected or private modes.
These 3 keywords (public, protected, and private) are known as access
specifiers in C++ inheritance.

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Types of Inheritance
1. Single inheritance
2. Multiple inheritance
3. Hybrid inheritance
4. Multilevel inheritance
5. Hierarchical inheritance

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Single Inheritance
Single inheritance is defined as the inheritance in which a derived class
is inherited from the only one base class.

Where 'A' is the base class, and 'B' is the derived class.

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Single Inheritance
A class inherits from only one base class.
class Base {
// Base class members
};

class Derived : public Base {


// Derived class members
};

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Multiple Inheritance

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Multiple Inheritance
A class inherits from more than one base class.
class Base1 {
// Base1 members
};

class Base2 {
// Base2 members
};
class Derived : public Base1, public Base2 {
// Derived class members
};

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Hybrid Inheritance

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Hybrid Inheritance
A combination of two or more types of inheritance (e.g., multiple and multilevel).
class Base {
// Base members
};
class Intermediate1 : public Base {
// Intermediate1 members
};
class Intermediate2 : public Base {
// Intermediate2 members
};
class Derived : public Intermediate1, public Intermediate2 {
// Derived members
};

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Multilevel Inheritance

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Multilevel Inheritance
A class is derived from a class that is also derived from another class, creating a
chain.
class Base {
// Base members
};
class Intermediate : public Base {
// Intermediate members
};
class Derived : public Intermediate {
// Derived members
};

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Hierarchical Inheritance

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Hierarchical Inheritance
Multiple derived classes inherit from a single base class.
class Base {
// Base members
};
class Derived1 : public Base {
// Derived1 members
};
class Derived2 : public Base {
// Derived2 members
};

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Advantages of Inheritance in C++
1. Code Reusability: Reuse code from the base class in derived
classes without rewriting.
2. Extensibility: Extend or modify the base class functionality in
derived classes.
3. Maintainability: Changes in the base class automatically apply to
all derived classes.
4. Data Abstraction: Hide implementation details and expose only
necessary functionality.
5. Polymorphism: Enables dynamic method dispatch and flexible
interface design.

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Advantages of Inheritance in C++
6. Modularity: Organize code into smaller, manageable, and
understandable parts.
7. Avoids Redundancy: Reduces the need to duplicate code,
improving efficiency.
8. Logical Grouping: Provides clear class hierarchies, making
relationships intuitive.
9. Readability: Enhances code readability and structure, making it
easier to understand.
10. Supports Design Patterns: Essential for implementing various
object-oriented design patterns.

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Inheritance Methods
1. Public Inheritance makes public members of the base class public
in the derived class, and the protected members of the base class
remain protected in the derived class.
2. Protected Inheritance makes the public and protected members of
the base class protected in the derived class.
3. Private Inheritance makes the public and protected members of the
base class private in the derived class.
Note: private members of the base class are inaccessible to the derived
class.

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Derivation-Public, Private & Protected
Visibility of Base Class Members
• Public inheritance: Public and protected members of the base class
retain their visibility in the derived class.
• Protected inheritance: Public and protected members of the base
class become protected in the derived class.
• Private inheritance: All members of the base class become private in
the derived class.

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


class Base {
public:
int x;
protected:
int y;
private:
int z;
};
class PublicDerived: public Base {
// x is public
// y is protected
// z is not accessible from PublicDerived
};
class ProtectedDerived: protected Base {
// x is protected
// y is protected
// z is not accessible from ProtectedDerived
};
class PrivateDerived: private Base {
// x is private
// y is private
// z is not accessible from PrivateDerived
};
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Public Inheritance

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


// C++ program to demonstrate the working of public inheritance

#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


// C++ program to demonstrate the working of public inheritance

#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Private Inheritance

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


// C++ program to demonstrate the working of protected inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access public member from Base
int getPub() {
return pub;
}
};
int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access public member from Base
int getPub() {
return pub;
}
};
int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Protected Inheritance

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


// C++ program to demonstrate the working of protected inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access public member from Base
int getPub() {
return pub;
}
};
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access public member from Base
int getPub() {
return pub;
}
};
int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Aggregation
• Aggregation in C++ (HAS-A Relationship)
• Aggregation in C++ is a relationship where one class (the whole)
contains objects of another class (the part), but both objects can exist
independently.
• This relationship is referred to as a HAS-A relationship because the
whole class "has" the part class, but does not own or control its
lifecycle.

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Key Characteristics of Aggregation
1. Independence:
The part class (contained object) can exist independently of the whole class
(container object).
The whole class does not manage the lifetime of the part class object. If the
whole is destroyed, the part can still exist.
2. Weak Association:
Aggregation is a weaker form of association compared to composition, where
the container owns and controls the lifecycle of the contained object.
3. Implementation:
Aggregation is implemented by having a class contain a reference or a pointer
to another class object.
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Aggregation Vs Composition
• Aggregation is a "HAS-A" relationship where the part can exist
independently.
• Composition is a stronger "HAS-A" relationship where the whole class
fully owns and controls the part class’s lifecycle (the part is destroyed when
the whole is destroyed).
• Aggregation:
• Independent lifecycle.
• Example: A company has employees, but the employees can exist without
the company.
• Composition:
• Dependent lifecycle.
• Example: A house has rooms, but the rooms cannot exist without the house.
Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)
Inheritance Constructors
Inheritance constructors in C++ allow derived classes to inherit and use
constructors from their base classes without having to explicitly define them
in the derived class. This feature simplifies object construction in inheritance
hierarchies.
Key Benefits:
• Code Reusability: No need to rewrite base class constructors in derived
classes.
• Simplified Code: Avoid redundant code when initializing base class
members.
• Consistency: Ensures that objects of derived classes can be initialized in
the same way as base class objects.

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Inheritance Constructors
• In C++11 and later, you can use the using keyword in a derived class
to inherit constructors from the base class.
• When you inherit constructors, the derived class automatically gets
access to all of the base class constructors, except copy/move
constructors or those marked as private.
Syntax
class Derived : public Base {
using Base::Base; // Inherit constructors from Base
};

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


Inheritance Constructors
How it Works:
Inherited constructors allow the derived class to initialize itself in the
same way as the base class.
The using declaration only copies the signatures of the constructors, and
if needed, the derived class can still have its own constructors in
addition to the inherited ones.

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


#include <iostream>
using namespace std;
class Base {
public:
Base(int x) {
cout << "Base class constructor called with value: " << x << endl;
}
};
class Derived : public Base {
public:
using Base::Base; // Inheriting Base class constructor
};
int main() {
Derived d(10); // Base class constructor is invoked
return 0;
}

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


#include <iostream>
using namespace std;
class Base {
public:
Base(int x) {
cout << "Base class constructor called with value: " << x << endl;
}
};
class Derived : public Base {
public:
using Base::Base; // Inheriting Base class constructor
};
int main() {
Derived d(10); // Base class constructor is invoked
return 0;
}

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)


THANK YOU

Prepared by Dr. Ankita Sharma (Assistant Professor of CSE)

You might also like