Javatpoint Logo
Javatpoint Logo

Difference between Base class and Derived class in C++

In this article, we will discuss the difference between the Base class and the Derived class in C++. But before discussing their differences, we must know about the inheritance, base class, and derived class with their examples.

What is Inheritance?

Inheritance creates an "is-a" connection, which means that a derived class "is a" specialized version of the base class. It allows the derived class to access the attributes and methods of a base class.

In object-oriented programming (OOP), inheritance is a fundamental notion that allows new classes (known as derived or child classes) to inherit features and behaviours from existing classes (known as base or parent classes). This creates hierarchical link between classes improves code reusability and code organization.

Syntax:

In C++, the inheritance syntax is a colon (:) followed by the access specifier and the name of the base class in the derived class declaration.

Program:

Let us take an example to illustrate the inheritance in C++.

Output:

Difference between Base class and Derived class in C++

Explanation:

  • In this example, Car is a derived class that derives from the base class Vehicle.
  • The Car class inherits the Vehicle's start() and stop() methods and adds its drive() function.

Understanding C++ Base and Derived Classes:

What is the Base Class?

A base class is the parent class from which additional classes, known as derived classes, acquire their attributes and behaviors. It has characteristics and methods in common that can be shared by several derived classes. Base classes are frequently designed to be abstract, which means they may include only virtual functions that must be overridden in derived classes.

Program:

Let us take an example to illustrate the Base Class in C++.

Output:

Difference between Base class and Derived class in C++

Explanation:

  • In this example, Shape is the base class, while Circle is a derived class that inherits from Shape.
  • The draw() method of the Circle class is overridden to provide a particular implementation.

What is the Derived Class?

A derived class is one that inherits properties and behaviours from its base class. It can add its own properties and methods while overriding or expanding the base class's functionality. Many inheritances allow a derived class to have many base classes.

Program:

Let us take an example to illustrate the Derived Class in C++.

Output:

Difference between Base class and Derived class in C++

Explanation:

  • In this example, square is another derived class that derives from the Shape base class.
  • Both the Circle and Square classes override the draw() method to offer customized implementations.

Differences between Base Class and Derived Class:-

Difference between Base class and Derived class in C++

There are several differences between the Base Class and Derived Class. Some main differences between the Base Class and Derived Class are as follows:

Features Base Class Derived Class
Definition The base class from which all others are derived. A base class's properties are inherited by this class.
Inheritance It is not possible to inherit from another class. The base class's properties and behaviors are inherited.
Creation Directly constructed without the use of inheritance. Developed by deriving from a base class.
Access Control It can gain access to its members as well as those who are public or protected. Members are inherited according to their access specifier (public, protected, or private).
Modifiers Derived classes cannot directly modify this class. Can change inherited members or methods.
Usage Serves as a template for derived classes. Extends or modifies the functionality of the underlying class.
Instance Creation If not abstract (no pure virtual functions), it can be created. Only through its derived classes may it be instantiated if it has pure virtual functions (abstract).
Function Overriding Derived classes can override virtual functions. Provides specialized implementations by overriding basic class methods.
Relationship There is no direct connection to other classes. Its base class has a hierarchical connection with it.
Example class Circle: public Shape{...}; class Shape: public Shape{.. };






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA