In C++, a derived class is a class that is based on another class (called the base class or parent class). The derived class inherits properties (data members) and behaviors (member functions) of the base class, and it can also have additional features or override base class functionality. This mechanism is central to inheritance, which is one of the four pillars of Object-Oriented Programming (OOP).
Inheritance:
Access to Base Class Members:
Overriding:
Constructor and Destructor:
Access Control:
public, protected, private).
class Base {
// Base class code
};
class Derived : access_modifier Base {
// Derived class code
};
access_modifier: Specifies the type of inheritance (public, protected, or private).In this example, we define a Base class and a Derived class. The derived class inherits properties from the base class.
#include <iostream>
using namespace std;
class Base {
public:
int baseValue;
// Constructor for Base class
Base(int val) {
baseValue = val;
}
void displayBase() {
cout << "Base Value: " << baseValue << endl;
}
};
class Derived : public Base {
public:
int derivedValue;
// Constructor for Derived class
Derived(int val1, int val2) : Base(val1) {
derivedValue = val2;
}
void displayDerived() {
cout << "Derived Value: " << derivedValue << endl;
}
};
int main() {
Derived derivedObj(5, 10);
derivedObj.displayBase(); // From Base class
derivedObj.displayDerived(); // From Derived class
return 0;
}
Explanation:
Derived inherits from Base using public inheritance. This means the public members of the Base class are accessible in the Derived class.Derived calls the constructor of Base to initialize the baseValue.displayBase() and displayDerived() are called to show the values from both classes.Output:
Base Value: 5
Derived Value: 10
In this example, the Derived class overrides the display() function of the Base class.
#include <iostream>
using namespace std;
class Base {
public:
// Virtual function
virtual void display() {
cout << "Display from Base class" << endl;
}
};
class Derived : public Base {
public:
// Override the base class function
void display() override {
cout << "Display from Derived class" << endl;
}
};
int main() {
Base* basePtr;
Derived derivedObj;
basePtr = &derivedObj;
// Calls the derived class version of display
basePtr->display();
return 0;
}
Explanation:
display() function is declared as virtual in the base class. This allows the Derived class to override it.basePtr->display() is called (even though basePtr is a pointer to Base), the derived class version of display() is invoked at runtime due to dynamic dispatch.Output:
Display from Derived class
This example demonstrates how different access specifiers (public, protected, and private) affect member inheritance in derived classes.
#include <iostream>
using namespace std;
class Base {
public:
int publicValue;
protected:
int protectedValue;
private:
int privateValue;
public:
Base(int pub, int prot, int priv) {
publicValue = pub;
protectedValue = prot;
privateValue = priv;
}
};
class DerivedPublic : public Base {
public:
DerivedPublic(int pub, int prot, int priv) : Base(pub, prot, priv) {}
void display() {
cout << "Public Value: " << publicValue << endl; // Accessible
cout << "Protected Value: " << protectedValue << endl; // Accessible
// cout << "Private Value: " << privateValue << endl; // Not accessible
}
};
int main() {
DerivedPublic obj(10, 20, 30);
obj.display();
return 0;
}
Explanation:
DerivedPublic, the public and protected members of the Base class are accessible, but the private members are not.privateValue is not accessible from the derived class.Output:
Public Value: 10
Protected Value: 20
Single Inheritance:
class Derived : public Base.Multiple Inheritance:
class Base1 { /* ... */ };
class Base2 { /* ... */ };
class Derived : public Base1, public Base2 { /* ... */ };
Multilevel Inheritance:
class Derived : public Base, class SubDerived : public Derived.Hierarchical Inheritance:
class Derived1 : public Base, class Derived2 : public Base.Hybrid Inheritance:
#include <iostream>
using namespace std;
class Base1 {
public:
void base1Function() {
cout << "Base1 Function" << endl;
}
};
class Base2 {
public:
void base2Function() {
cout << "Base2 Function" << endl;
}
};
class Derived : public Base1, public Base2 {
public:
void derivedFunction() {
cout << "Derived Function" << endl;
}
};
int main() {
Derived obj;
obj.base1Function(); // From Base1
obj.base2Function(); // From Base2
obj.derivedFunction(); // From Derived
return 0;
}
Explanation:
Derived class inherits from both Base1 and Base2.base1Function() and base2Function().Output:
Base1 Function
Base2 Function
Derived Function
Open this section to load past papers