In C++, inheritance is a mechanism that allows a new class to inherit the properties and behavior of an existing class. The three types of inheritance are public, private, and protected.
Public inheritance makes all the members of the base class public in the derived class. This means that the members of the base class can be accessed by the derived class and by any other class that uses the derived class.
Private inheritance makes all the members of the base class private in the derived class. This means that the members of the base class can only be accessed by the derived class, and not by any other class that uses the derived class.
Protected inheritance makes all the members of the base class protected in the derived class. This means that the members of the base class can be accessed by the derived class and by any class that is derived from the derived class.
The type of inheritance that you use will depend on the specific needs of your application. If you want the derived class to have full access to the members of the base class, then you should use public inheritance. If you want to restrict access to the members of the base class, then you should use private or protected inheritance.
Here is an example of how to use public inheritance:
C++
class Base {
public:
int x;
void print() {
cout << x << endl;
}
};
class Derived : public Base {
public:
void doSomething() {
x = 10; // This is allowed because x is public in Base
print(); // This is allowed because print() is public in Base
}
};
In this example, the Derived class inherits from the Base class using public inheritance. This means that all the members of the
Base class are public in the Derived class. Therefore, the
Derived class can access the x variable and the print() method from the
Base class.
Here is an example of how to use private inheritance:
C++
class Base {
private:
int x;
void print() {
cout << x << endl;
}
};
class Derived : private Base {
public:
void doSomething() {
// This is not allowed because x is private in Base
// x = 10;
// This is also not allowed because print() is private in Base
// print();
}
};
In this example, the Derived class inherits from the Base class using private inheritance. This means that all the members of the
Base class are private in the Derived class. Therefore, the
Derived class cannot access the x variable or the
print() method from the Base class.
Here is an example of how to use protected inheritance:
C++
class Base {
protected:
int x;
void print() {
cout << x << endl;
}
};
class Derived : protected Base {
public:
void doSomething() {
// This is allowed because x is protected in Base
x = 10;
// This is also allowed because print() is protected in Base
print();
}
};
In this example, the Derived class inherits from the Base class using protected inheritance. This means that all the members of the
Base class are protected in the Derived class. Therefore, the
Derived class can access the x variable and the print() method from the
Base class, but only from within the Derived class or from any class that is derived from the
Derived class.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C++, inheritance is a mechanism that allows a new class to inherit the properties and behavior of an existing class. The three types of inheritance are public, private, and protected.
The type of inheritance that you use will depend on the specific needs of your application. If you want the derived class to have full access to the members of the base class, then you should use public inheritance. If you want to restrict access to the members of the base class, then you should use private or protected inheritance.
Here is an example of how to use public inheritance:
C++
In this example, the
Derivedclass inherits from theBaseclass using public inheritance. This means that all the members of theBaseclass are public in theDerivedclass. Therefore, theDerivedclass can access thexvariable and theprint()method from theBaseclass.Here is an example of how to use private inheritance:
C++
In this example, the
Derivedclass inherits from theBaseclass using private inheritance. This means that all the members of theBaseclass are private in theDerivedclass. Therefore, theDerivedclass cannot access thexvariable or theprint()method from theBaseclass.Here is an example of how to use protected inheritance:
C++
In this example, the
Derivedclass inherits from theBaseclass using protected inheritance. This means that all the members of theBaseclass are protected in theDerivedclass. Therefore, theDerivedclass can access thexvariable and theprint()method from theBaseclass, but only from within theDerivedclass or from any class that is derived from theDerivedclass.