Inheritance is a fundamental concept in C++ that allows one class to inherit properties and behaviors from another. This promotes code reusability and establishes a hierarchical relationship between classes.
The class that is inherited from is called the base class, while the class that inherits is the derived class. This relationship forms the basis of inheritance in C++.
class Base {
public:
void display() {
cout << "Base class display function" << endl;
}
};
class Derived : public Base {
public:
void show() {
cout << "Derived class show function" << endl;
}
};
int main() {
Derived obj;
obj.display();
obj.show();
return 0;
}
Console Output:
Base class display function Derived class show function
Single inheritance allows a derived class to inherit from only one base class. This is the simplest form of inheritance.
In multiple inheritance, a derived class can inherit from more than one base class. This can lead to complex hierarchies and potential ambiguities.
class Base1 {
public:
void greet() {
cout << "Hello from Base1" << endl;
}
};
class Base2 {
public:
void greet() {
cout << "Hello from Base2" << endl;
}
};
class Derived : public Base1, public Base2 {
public:
void greet() {
Base1::greet();
Base2::greet();
}
};
int main() {
Derived obj;
obj.greet();
return 0;
}
Console Output:
Hello from Base1 Hello from Base2
In hierarchical inheritance, multiple derived classes inherit from a single base class. This structure is useful when several classes share common functionalities.
class Base {
public:
void display() {
cout << "Base class display function" << endl;
}
};
class Derived1 : public Base {
public:
void show1() {
cout << "Derived1 class show1 function" << endl;
}
};
class Derived2 : public Base {
public:
void show2() {
cout << "Derived2 class show2 function" << endl;
}
};
int main() {
Derived1 obj1;
Derived2 obj2;
obj1.display();
obj1.show1();
obj2.display();
obj2.show2();
return 0;
}
Console Output:
Base class display function Derived1 class show1 function Base class display function Derived2 class show2 function
Multilevel inheritance involves a chain of inheritance where a derived class becomes the base class for another derived class. This forms a linear hierarchy.
class Base {
public:
void display() {
cout << "Base class display function" << endl;
}
};
class Intermediate : public Base {
public:
void intermediateFunction() {
cout << "Intermediate class function" << endl;
}
};
class Derived : public Intermediate {
public:
void show() {
cout << "Derived class show function" << endl;
}
};
int main() {
Derived obj;
obj.display();
obj.intermediateFunction();
obj.show();
return 0;
}
Console Output:
Base class display function Intermediate class function Derived class show function
Hybrid inheritance is a combination of two or more types of inheritance. It is used to overcome the limitations of single and multiple inheritance.
class Base1 {
public:
void show() {
cout << "Base1 show function" << endl;
}
};
class Base2 {
public:
void display() {
cout << "Base2 display function" << endl;
}
};
class Intermediate : public Base1 {
};
class Derived : public Intermediate, public Base2 {
};
int main() {
Derived obj;
obj.show();
obj.display();
return 0;
}
Console Output:
Base1 show function Base2 display function
Access specifiers (public, protected, private) determine the accessibility of base class members in derived classes. They play a crucial role in inheritance.
class Base {
protected:
int protectedVar;
public:
int publicVar;
};
class Derived : public Base {
public:
void show() {
cout << "Protected Variable: " << protectedVar << endl;
cout << "Public Variable: " << publicVar << endl;
}
};
int main() {
Derived obj;
obj.publicVar = 10;
obj.show();
return 0;
}
Console Output:
Protected Variable: 0 Public Variable: 10
Virtual inheritance is used to solve the diamond problem in multiple inheritance by ensuring a single instance of the base class.
class Base {
public:
void show() {
cout << "Base class function" << endl;
}
};
class Derived1 : virtual public Base {
};
class Derived2 : virtual public Base {
};
class Final : public Derived1, public Derived2 {
};
int main() {
Final obj;
obj.show();
return 0;
}
Console Output:
Base class function
Function overriding allows a derived class to provide a specific implementation of a function that is already defined in its base class.
class Base {
public:
virtual void display() {
cout << "Display from Base class" << endl;
}
};
class Derived : public Base {
public:
void display() override {
cout << "Display from Derived class" << endl;
}
};
int main() {
Base* ptr;
Derived obj;
ptr = &obj;
ptr->display();
return 0;
}
Console Output:
Display from Derived class
An abstract class is a class that cannot be instantiated and is designed to be inherited from. It typically contains at least one pure virtual function.
class AbstractBase {
public:
virtual void pureVirtualFunction() = 0;
};
class Derived : public AbstractBase {
public:
void pureVirtualFunction() override {
cout << "Implementation of pure virtual function" << endl;
}
};
int main() {
Derived obj;
obj.pureVirtualFunction();
return 0;
}
Console Output:
Implementation of pure virtual function
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies