In this tutorial, we will learn about inheritance in C++ with the help of examples.
Inheritance is a key feature of C++ object-oriented programming. It enables us to derive a new class from an existing class (base class).
The MyClass1 class is derived from the MyClass class in this case. Members of Animal are accessible to MyClass because MyClass1 is derived from MyClass.
Notice the use of the keyword Public while inheriting MyClass1 from MyClass.
Instead of Public , we can use the keywords Private and Protected. Later in this tutorial, we'll look at the differences between using Private, Public, and Protected.
Access Modes in C++ Inheritance
We've already covered C++ access specifiers like public, private, and protected in previous tutorials.
So far, we've used the Public keyword to inherit a class from an existing base class. We can, however, inherit classes by using the Private and Protected keywords. As an example,
class myclass{
public:
code;
};
class myclass1:private myclass{
Code;
};
public: If a derived class is declared in Public mode, then the members of the base class are inherited by the derived class just as they are.
private: In this case, all the members of the base class become Private members in the derived class.
protected: The Public members of the base class become protected members in the derived class.
Please Watching My Video is Below
No comments:
Post a Comment