Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New entry Multilevel Inheritance #6049

Merged
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
---
Title: 'Multilevel Inheritance'
Description: 'Implements a chain of inheritance where a derived class inherits from another derived class, forming a parent-child-grandchild relationship between classes.'
Subjects:
- 'Computer Science'
- 'Code Foundations'
Tags:
- 'Inheritance'
- 'OOP'
- 'Classes'
- 'Objects'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

**Multilevel inheritance** is an Object-Oriented Programming (OOP) concept where a class can inherit properties and methods from a class that is already inherited from another class, creating a hierarchy of classes. This forms a parent-child-grandchild relationship between classes, allowing for the creation of specialized classes based on existing ones.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add docs link to OOP concepts -

Suggested change
**Multilevel inheritance** is an Object-Oriented Programming (OOP) concept where a class can inherit properties and methods from a class that is already inherited from another class, creating a hierarchy of classes. This forms a parent-child-grandchild relationship between classes, allowing for the creation of specialized classes based on existing ones.
**Multilevel inheritance** is an [Object-Oriented Programming (OOP)](https://www.codecademy.com/resources/docs/general/programming-paradigms/object-oriented-programming) concept where a class can inherit properties and methods from a class that is already inherited from another class, forming a hierarchical class structure. This forms a parent-child-grandchild relationship between classes, enabling the creation of specialized classes from existing ones.


## Syntax

In C++, multilevel inheritance follows this general syntax:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In C++, multilevel inheritance follows this general syntax:
In C++, multilevel inheritance follows this syntax:


```cpp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use pseudo code blocks for syntax -

Suggested change
```cpp
```pseudo

class BaseClass {
// Base class members
};

class DerivedClass1 : public BaseClass {
// First level derived class members
};

class DerivedClass2 : public DerivedClass1 {
// Second level derived class members
};
```

## Example

Simple example to demonstrate multilevel inheritance in C++
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be better written as following -

Suggested change
Simple example to demonstrate multilevel inheritance in C++
Here's an example demonstrating multilevel inheritance in C++:


```cpp
#include <iostream>
using namespace std;

class Animal {
public:
void eat() {
cout << "Animal is eating." << endl;
}
};

class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking." << endl;
}
};

class Puppy : public Dog {
public:
void weep() {
cout << "Puppy is weeping." << endl;
}
};

int main() {
Puppy myPuppy;

myPuppy.eat(); // Inherited from Animal
myPuppy.bark(); // Inherited from Dog
myPuppy.weep(); // Inherited from Puppy

return 0;
}
```

The output of the above code will be:

```
Animal is eating.
Dog is barking.
Puppy is weeping.
```

## Codebyte Example

```codebyte/cpp
#include <iostream>
#include <string>

class Employee {
protected:
std::string name;
int id;

public:
Employee(std::string n, int i) : name(n), id(i) {}

virtual void displayInfo() {
std::cout << "Name: " << name << "\nID: " << id << std::endl;
}
};

class Developer : public Employee {
protected:
std::string programmingLanguage;

public:
Developer(std::string name, int id, std::string lang)
: Employee(name, id), programmingLanguage(lang) {}

void displayInfo() override {
Employee::displayInfo();
std::cout << "Role: Developer\n";
std::cout << "Programming Language: " << programmingLanguage << std::endl;
}
};

class SeniorDeveloper : public Developer {
private:
int teamSize;
std::string projectName;

public:
SeniorDeveloper(std::string name, int id, std::string lang,
int team, std::string project)
: Developer(name, id, lang), teamSize(team), projectName(project) {}

void displayInfo() override {
Developer::displayInfo();
std::cout << "Position: Senior Developer\n";
std::cout << "Team Size: " << teamSize << "\n";
std::cout << "Current Project: " << projectName << std::endl;
}
};

int main() {
SeniorDeveloper lead("Alice Johnson", 1001, "C++", 5, "Payment Gateway");
lead.displayInfo();
return 0;
}
```

This example demonstrates how multilevel inheritance allows a class to inherit features from multiple levels of parent classes, creating a clear and organized hierarchy of related classes. The C++ implementation includes additional features like virtual functions for proper polymorphic behavior and access specifiers (public, protected, private) to control member accessibility.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add relevant doc links -

Suggested change
This example demonstrates how multilevel inheritance allows a class to inherit features from multiple levels of parent classes, creating a clear and organized hierarchy of related classes. The C++ implementation includes additional features like virtual functions for proper polymorphic behavior and access specifiers (public, protected, private) to control member accessibility.
This example demonstrates how multilevel inheritance allows a class to inherit features across multiple levels, forming a structured class hierarchy. The implementation includes virtual functions for proper [polymorphic behavior](https://www.codecademy.com/resources/docs/cpp/polymorphism) and access specifiers (`public`, `protected`, `private`) to control member accessibility.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this above the code block - we try to keep the explanation very short for codebyte examples and they are the last part of the entry.