The main difference between base class and derived class in C++ is that base class is the already existing class while derived class is the class that can inherit the properties and methods of the base class.
C++ is a high-level, general purpose programming language developed by Bjarne Stroustrup at Bell Labs. It is a superset of C language. The programmer can implement Object Oriented Programming (OOP) concepts using C++. OOP helps to find solutions to real-world scenarios easily. There are various concepts in OOP. One of them is inheritance. Inheritance in C++ refers to the process of allowing a class to use properties and methods of an already existing class. The main advantage of inheritance is that it supports code reusability.
Key Areas Covered
1. What is Base Class in C++
– Definition, Functionality
2. What is Derived Class in C++
– Definition, Functionality
3. What is the Difference Between Base Class and Derived Class in C++
– Comparison of Key Differences
Key Terms
Base Class, C++, Derived Class, Inheritance, OOP
What is Base Class in C++
In inheritance, the already existing class is the parent class or the superclass. We also call it the base class. The members in a class can have access modifiers such as private, public and protected. The private members are accessible within the same class while the public members are accessible by any class. However, the protected members are accessible within the class and by its subclasses. Therefore, the derived class can access the public and protected members of the base class, but not the private members.
What is Derived Class in C++
Derived class is the class that can use the properties and methods of an already existing class. It is a new class. We also call it child class or subclass.
In the above program, Vehicle is a class. It has a public method called vehicleDetails. Car is another class, and it has a public method called carDetails. These classes are connected using inheritance. Vehicle is the base class whereas Car is the derived class. Therefore, Car class can use its own method (carDetails) as well as the method of the base class (vehicleDetails).
There are various types of inheritance in C++.
Single inheritance – It is a simple type of inheritance. Derived class inherits only from a one base class. A is the base class while B is the derived class.
Multiple inheritance – Derived class inherits from two or more base classes. A and B are base classes while C is a derived class.
Hierarchical inheritance – Many derived classes inherit from a single base class. A is the base class. B, C, D are derived classes.
Multi-level inheritance – Derived class inherits from a class, and that class inherits from another class. A is the base class of B. B is the base class of C. C is the derived class of B. B is the derived class of A.
Hybrid inheritance – It is a combination of multiple inheritance types. B, C are derived classes of A. D is the derived class of B and C.
Difference Between Base Class and Derived Class in C++
Definition
Base class is a class that helps to derive or create new classes while derived class is a class created or derived from an already existing class. Thus, this is the main difference between base class and derived class.
Synonyms
Base class is also called parent class or superclass while derived class is also called child class or subclass.
Inheritance
Inheritance is a major difference between base class and derived class. Base class cannot inherit properties and methods of the derived class. However, derived class can inherit the properties and methods of the base class.
Conclusion
Inheritance allows the programmer to inherit members of an existing class in a new class. The main difference between base class and derived class in C++ is that base class is the already existing class while derived class is the class that can inherit the properties and methods of the base class. In other words, base class is the old class whereas derived class is the new class.
Reference:
1. “C Inheritance – Javatpoint.” Www.javatpoint.com, Available here.
2. “Types of Inheritance in C .” Types of Network Topology in Computer Networks | Studytonight, Available here.
Leave a Reply