-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Changes from 6 commits
2308551
21029b3
35ea46c
aa1cf2d
5135402
aa0317a
5403a56
7d57618
43cde08
eb8fd05
71b2742
6940e98
f2101b9
3f28900
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||||||
|
||||||
## Syntax | ||||||
|
||||||
In C++, multilevel inheritance follows this general syntax: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```cpp | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use
Suggested change
|
||||||
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++ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be better written as following -
Suggested change
|
||||||
|
||||||
```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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add relevant doc links -
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
There was a problem hiding this comment.
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 -