Single inheritance in C++ is a type of inheritance where a derived class inherits from a single base class. In other words, a class inherits the properties and methods of only one other class. Single inheritance is the most basic form of inheritance and is the simplest type of inheritance to understand and implement.
Here is an example of single inheritance in C++:
class Base {
public:
int x;
void funct() { /* code */ }
};
class Derived : public Base {
// Derived class inherits x and funct from Base
};
In this example, the class "Derived" inherits the public members of the class "Base", which includes the integer variable 'x' and the function 'funct'.
Single inheritance allows for code reuse, as the derived class can use the properties and methods of the base class without having to rewrite the same code. It also allows for creating a hierarchy of classes and can be used as a building block for more complex forms of inheritance such as multiple and multi-level inheritance.
It is important to note that in C++, the default inheritance type is private, so if we don't specify the type of inheritance, it will be private inheritance.
In summary, single inheritance in C++ is a type of inheritance where a derived class inherits from a single base class. It is the most basic form of inheritance, it allows for code reuse, creating a hierarchy of classes, and it can be used as a building block for more complex forms of inheritance. The default inheritance type in C++ is private, so you need to specify if you want public or protected inheritance.
Comments
Post a Comment