top of page
Writer's pictureRosario Riley

Inheritance in C++ with Example

Meaning:- The literal meaning of the term "inheritance" is the derivation of a characteristic from an ancestor.

Similarly, the concept of inheritance in programming revolves around the idea of ​​inheriting the properties of a class to its subclasses.

The parent class whose properties are inherited is called the base class, and all classes that inherit from the base class are derived classes.

This is one of the most important features of object-oriented programming. Inheritance improves program efficiency by providing benefits such as code reusability and data hiding.

When we say that a derived class inherits a base class, we mean that the derived class inherits all the properties of the base class and may add new functions to its own class without changing the properties of the base class.

These new features in derived classes do not affect the base class.

A class that inherits properties from a subclass is called a base class or superclass.

Inheritance is the function or process of creating a new class from an existing class. The newly created class is called the "derived class" or "child class," and the existing class is called the "base class" or "parent class." Derived classes must inherit from a base class.

Example:-

#include <iostream>

#include <string>

using namespace std;

class Base_Class

{

public: string base_name = "Ram";

void show_Base()

{

cout << "Hello, Base Class! \n" ;

}

};

class Derived_Class: public Base_Class

{

public: string derived_name = "Shyam";

};

int main()

{

Derived_Class ob;

ob.show_Base();

cout << ob.base_name + " " + ob.derived_name;

return 0;

}

Inheritance in C++ is a powerful feature that allows a class to inherit properties and behaviors from another class. It is one of the key concepts of object-oriented programming, and it enables code reusability and the creation of a hierarchy of classes. Inheritance allows us to build upon existing classes and create new classes based on them, thereby promoting the concept of modularity and abstraction.

In C++, a class that is derived from another class is called a derived class, and the class from which it is derived is called the base class. The derived class inherits all the members (variables and functions) of the base class, except for its constructors and destructors. Inheritance is specified using the colon (:) followed by the access specifier (public, protected, or private) and the name of the base class in the derived class declaration.

Inheritance in C++ can be of different types, such as single inheritance (one base class and one derived class), multiple inheritance (multiple base classes and one derived class), hierarchical inheritance (one base class and multiple derived classes), and multilevel inheritance (a derived class becomes the base class for another derived class).

inheritance in C++ is a powerful feature that allows for code reusability and the creation of a class hierarchy. It enables the creation of new classes based on existing classes and promotes modularity and abstraction in programming. By understanding and using inheritance effectively, programmers can write more efficient and flexible code.

TCCI provides the best training in C++ programming through different learning methods/media is located in Bopal Ahmedabad and ISCON Ambli Road in Ahmedabad.

For More Information:                                    

Call us @ +91 9825618292

Recent Posts

See All

תגובות


bottom of page