Welcome to the "Computer Programming with C++" course repository! This section provides an overview of inheritance, a fundamental concept in C++ and object-oriented programming (OOP).
Inheritance is a mechanism in C++ that allows one class (called a derived or child class) to inherit attributes and methods from another class (called a base or parent class). It's a way to form new classes using classes that have already been defined. The primary goal of inheritance is to provide code reusability and establish a relationship between different classes.
- Base Class (Parent Class): The class whose properties and functions can be inherited.
- Derived Class (Child Class): The class that inherits from another class.
- Single Inheritance: When a class inherits from only one base class.
- Multiple Inheritance: When a class inherits from more than one base class.
Here's a simple syntax for defining a derived class from a single base class:
class BaseClass {
// Base class members
};
class DerivedClass : public BaseClass {
// Derived class members
};
- Single Inheritance: Deriving a new class from a single parent class.
- Multiple Inheritance: Deriving a new class from more than one parent class.
- Hierarchical Inheritance: Multiple classes are derived from a single base class.
- Multilevel Inheritance: Deriving a class from another derived class.
- Hybrid Inheritance: A combination of two or more types of inheritance.
Inheritance in C++ is controlled by three access specifiers:
public
: Members of the base class are public in the derived class.protected
: Members of the base class are protected in the derived class.private
: Members of the base class are private in the derived class.
- Code Reusability: Reuse attributes and methods of the existing class when creating a new class.
- Method Overriding: Redefine the base class's methods in the derived class.
- Polymorphism: Allows us to use the same interface for different underlying forms (data types).
Below is a simple example of inheritance in C++:
#include <iostream>
// Base class
class Animal {
public:
void speak() {
std::cout << "Some sound" << std::endl;
}
};
// Derived class
class Dog : public Animal {
public:
void speak() {
std::cout << "Woof woof!" << std::endl;
}
};
int main() {
Dog myDog;
myDog.speak(); // Output: Woof woof!
return 0;
}
In this example, Dog
is a derived class that inherits from the Animal
base class. We have overridden the speak
method in the Dog
class to provide a more specific implementation.
Inheritance is a powerful feature of C++ that promotes code reusability and logical relationships among classes. It's a cornerstone of OOP and essential for creating complex software systems with C++. Explore this repository for more examples and exercises related to inheritance.