[go: up one dir, main page]

0% found this document useful (0 votes)
6 views2 pages

Inheritance_in_Cpp_OOP

Inheritance in C++ is a mechanism that allows a new class (derived) to be created from an existing class (base), promoting code reuse and being a key feature of Object-Oriented Programming (OOP). There are various types of inheritance including single, multilevel, multiple, hierarchical, and hybrid, each with specific relationships between classes. Benefits of inheritance include code reusability, logical structure, easier maintenance, and enabling polymorphism.

Uploaded by

Ajay Maurya
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)
6 views2 pages

Inheritance_in_Cpp_OOP

Inheritance in C++ is a mechanism that allows a new class (derived) to be created from an existing class (base), promoting code reuse and being a key feature of Object-Oriented Programming (OOP). There are various types of inheritance including single, multilevel, multiple, hierarchical, and hybrid, each with specific relationships between classes. Benefits of inheritance include code reusability, logical structure, easier maintenance, and enabling polymorphism.

Uploaded by

Ajay Maurya
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/ 2

Inheritance in C++ (OOP)

What is Inheritance?

Inheritance is a mechanism in C++ where a new class (derived) is created from an existing class (base). It

promotes code reuse and is a key feature of OOP.

Syntax

class Base {

// base members

};

class Derived : access_specifier Base {

// derived members

};

Types of Inheritance

1. Single Inheritance -> A -> B

2. Multilevel Inheritance -> A -> B -> C

3. Multiple Inheritance -> A + B -> C

4. Hierarchical Inheritance -> A -> B, A -> C

5. Hybrid Inheritance -> Mix of above types

Access Specifiers

| Inheritance | Public Members | Protected Members | Private Members |

|-------------|----------------|-------------------|-----------------|

| public | public | protected | not inherited |

| protected | protected | protected | not inherited |

| private | private | private | not inherited |

Example: Single Inheritance


Inheritance in C++ (OOP)

class Parent {

public:

void show() {

cout << "I am Parent" << endl;

};

class Child : public Parent {

public:

void greet() {

cout << "Hello from Child" << endl;

};

Output

I am Parent

Hello from Child

Benefits of Inheritance

- Code Reusability

- Logical structure

- Easier maintenance

- Enables polymorphism

You might also like