CBSE Class 11 » Difference Between » Base Class and Derived Class in c++

Base Class and Derived Class in c++

This article contains detailed information on the base class and derived class in c++. Further, this article will also explain the basic difference between both.

Both base and derived classes are significant in object-oriented programming languages. A base class is a class that already exists, whereas a derived class inherits the properties of a base class. Let’s look at some fundamental differences between a base class and a derived class.

A base class is a pre-existing class from which all other classes and properties are derived in an object-oriented programming language. It’s also called a parent class or a superclass. In general, the class that inherits the base class can hold all of its members and some additional data. All members and member functions of a base class are passed down to the derived class. The derived class can access the base class more readily and have greater capability than the base class. A kid class or subclass is another name for a derived class.

Difference 

Base Class

  • Properties are inherited from this class
  • It’s sometimes referred to as a parent class or a superclass
  • It can’t inherit derived class’s attributes and methods

Derived Class

  • A class that is descended from the basic class
  • It’s also known as a subclass of a child class
  • It can inherit base class’s properties and methods

Outside of the class, the access specifier private is inaccessible. Outside of the class, the access specifier public is available. The protected access specifier is accessible in the derived class with some limitations, explained later.

Private inheritance is the first type of inheritance in C++.

The reserved term private is optional before the base class in the derived class, although it is good to use it for good programming practice. If not, it will be treated as private by default.Only class members where it is defined can access a member declared with the access specifier private. The derived class is only allowed to be used in the private area.

Inheritance by the public is form of inheritance must be indicated specifically. For example, use public instead of private, which is the default.As an example,Public base class derive a class.

Inheritance protection

The word protected in the derived class indicates that if another class is derived from this current class, the protected members in the base class will be private instead of the public in the first derived class. This prevents the derived class from accessing the base class’s protected members.

Inheritance regulations

We know that inheritance is a relationship between two classes that allows one class to use the members of another class without redefining them. Everything associated with a public access specifier is visible or available to all derived classes of a base class. Code outside the class can access the information given to and taken from the member function of the same access specifier. The following specifier is private, which is the maximum level of data concealment. Even the derived class or subclass cannot access the private members of the base or superclass. The protected access specifier is the third and final access specifier.

This access specifier’s data member and member function can be inherited by the sub-class, child, or derived class. The protected access specifier does not allow two objects of the same class to use each other’s data members. Both will have the same names, but the data will be different.

It should be noted that when a derived class object is formed, if the base class has a function Object() { [native code] }, it will be called first, followed by the derived class function Object() { [native code] }. When a derived object is destroyed, the destructor it contains is called first, followed by the base class’s destructor, if one exists.

Conclusion 

Maintenance and upgrade costs are reduced when there is less code. I often worry about how much code is copied unnecessarily nowadays. As we’ve seen, C++’s inheritance and polymorphism features can be used to great effect to create very functional and yet very general base classes. Many of the needs of the derived classes are then met by these base classes.

Ask yourself this question when defining base classes: “Have I made this base class as generic as possible?” Before adding code to derived classes, see whether you can put it in a more generalized form in the base class. It’s entirely likely that you’re simply repeating code in the derived classes.

faq

Frequently asked questions

Get answers to the most common queries related to the Programming Examination Preparation.

Q1 What may a derived class inherit from its source class?

Answer : All members and member functions of a base class are passed down to the derived class. The derived class ca...Read full

Q2 What may a derived class inherit from its source class?

Answer : All members and member functions of a base class are passed down to the derived class. The derived class ca...Read full

Q3 Which members of a derived class from the base class can never be accessed?

Answer : A derived class can access the base class members without restriction until and unless the members are priv...Read full

Q4 Is it when a derived class only inherits from one class and does not inherit from other classes?

Answer : Single Inheritance refers to when a derived class inherits only one base class. For example, a is a base cl...Read full