Open In App
Related Articles

Difference between Base class and Derived class in C++

Improve Article
Improve
Save Article
Save
Like Article
Like

Base Class: A base class is a class in Object-Oriented Programming language, from which other classes are derived. The class which inherits the base class has all members of a base class as well as can also have some additional properties. The Base class members and member functions are inherited to Object of the derived class. A base class is also called parent class or superclass.
Derived Class: A class that is created from an existing class. The derived class inherits all members and member functions of a base class. The derived class can have more functionality with respect to the Base class and can easily access the Base class. A Derived class is also called a child class or subclass.
Syntax for creating Derive Class: 
 

class BaseClass{
  // members....
  // member function 
}
     
class DerivedClass : public BaseClass{
  // members....
  // member function 
}

Difference between Base Class and Derived Class:
 

S.No. BASE CLASS DERIVED CLASS
1. 
 
A class from which properties are inherited. A class from which is inherited from the base class.
2. 
 
It is also known as parent class or superclass. It is also known as child class subclass.
3. 
 
It cannot inherit properties and methods of Derived Class. It can inherit properties and methods of Base Class.
4. 
 

Syntax: 

class BaseClass{
 // members….
 // member function 
}

Syntax: 

class DerivedClass : access_specifier BaseClass{
 // members….
 // member function 
}

Below is the program to illustrate Base Class and Derived Class:
 

CPP




// C++ program to illustrate
// Base & Derived Class
 
#include <iostream>
using namespace std;
 
// Declare Base Class
class Base {
public:
    int a;
};
 
// Declare Derived Class
class Derived : public Base {
public:
    int b;
};
 
// Driver Code
int main()
{
    // Initialise a Derived class geeks
    Derived geeks;
 
    // Assign value to Derived class variable
    geeks.b = 3;
 
    // Assign value to Base class variable
    // via derived class
    geeks.a = 4;
 
    cout << "Value from derived class: "
         << geeks.b << endl;
 
    cout << "Value from base class: "
         << geeks.a << endl;
 
    return 0;
}


Output: 

Value from derived class: 3
Value from base class: 4

 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 22 Mar, 2023
Like Article
Save Article
Similar Reads