0% found this document useful (0 votes)
10 views3 pages

Inheritance CPP Notes

This document covers the concept of inheritance in C++, an essential feature of Object-Oriented Programming that allows a derived class to inherit properties and behaviors from a base class. It discusses various types of inheritance, including single, multilevel, multiple, hierarchical, and hybrid inheritance, along with visibility modes and their effects. Additionally, it explains virtual base classes, abstract classes, and the order of constructor calls in derived classes.

Uploaded by

Rajashri Khadke
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)
10 views3 pages

Inheritance CPP Notes

This document covers the concept of inheritance in C++, an essential feature of Object-Oriented Programming that allows a derived class to inherit properties and behaviors from a base class. It discusses various types of inheritance, including single, multilevel, multiple, hierarchical, and hybrid inheritance, along with visibility modes and their effects. Additionally, it explains virtual base classes, abstract classes, and the order of constructor calls in derived classes.

Uploaded by

Rajashri Khadke
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/ 3

UNIT – III: Extending Classes using Inheritance (C++)

3.1 Introduction to Inheritance


Inheritance is an Object-Oriented Programming (OOP) feature where one class (derived/child) can
acquire the properties (data members) and behaviors (member functions) of another class
(base/parent).
Purpose: Code reusability, avoids redundancy, improves maintainability, supports polymorphism.
Syntax:
class DerivedClass : visibility-mode BaseClass { // members };
Example:
#include using namespace std; class Animal { public: void eat() { cout <<
"Eating..." << endl; } }; class Dog : public Animal { public: void bark() { cout
<< "Barking..." << endl; } }; int main() { Dog d; d.eat(); d.bark(); }
Visibility Modes and Effects:
Visibility Mode Public members of base become
Protected members of base become
Private members of base
Public Public in derived Protected in derived Not inherited
Protected Protected in derived Protected in derived Not inherited
Private Private in derived Private in derived Not inherited
3.2 Types of Inheritance
Single Inheritance: One base class → One derived class
class A { }; class B : public A { };

Multilevel Inheritance: Chain of inheritance (grandparent → parent → child)


class A { }; class B : public A { }; class C : public B { };

Multiple Inheritance: One class derived from two or more base classes
class A { }; class B { }; class C : public A, public B { };

Hierarchical Inheritance: One base class → Multiple derived classes


class A { }; class B : public A { }; class C : public A { };

Hybrid Inheritance: Combination of two or more types of inheritance (can cause ambiguity)
// Example: Multiple + Multilevel
3.3 Virtual Base Class, Abstract Class, Constructor in Derived Class
Virtual Base Class: Used to avoid multiple copies of the same base class in hybrid inheritance
(Diamond Problem).
class A { public: int x; }; class B : virtual public A { }; class C : virtual
public A { }; class D : public B, public C { };
Abstract Class: A class with at least one pure virtual function. Cannot be instantiated.
class Shape { public: virtual void draw() = 0; };
Constructor in Derived Class: Base class constructor is called first, then derived class
constructor.
class Base { public: Base() { cout << "Base constructor\n"; } }; class Derived :
public Base { public: Derived() { cout << "Derived constructor\n"; } };

You might also like