Inner Classes in C++

Inner Classes in C++ with Examples:

In this article, I am going to discuss Nested or Inner Classes in C++ with Examples. Please read our previous article where we discussed Static Members in C++ with Example.

Nested or Inner Classes in C++

A class can also contain another class definition inside itself, which is called “Inner Class” in C++. In this case, the containing class is referred to as the “Enclosing Class”. The Inner Class definition is considered to be a member of the Enclosing Class.

An Inner Class in C# is a class that is declared in another enclosing class. An Inner class is a member and as such has the same access rights as any other member of the class. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed. Let us learn nested or inner classes in C++. For explanation, we have taken one class here,

Nested or Inner Classes in C++ with Examples

So, what is inner class?

Writing a class inside another class is called inner class. It is useful only within that class. That is the inner class. If you are writing a class that is not useful everywhere or limited to one class. Then you can write a class inside of another one. So, it is just like reducing the complexity of a bigger class, we can write smaller classes inside.

The idea is to separate the logic inside a major class we can write inner classes in C++. Let us see. For that, we have taken an example that is the Outer class which is having two members that are non-static and static members, and then there is a function that is not doing anything. Now inside this class, we will write one class called Inner,

Inner Classes in C++ with Examples

This Inner class is having one non-static data member that is x of value 20. Then it is having a function show. So, we have written Inner class inside Outer class. Only classes are there but we have to create objects also. So, after the definition of Inner class, we have created an object of Inner class that is i. Can we create this object before the declaration of the Inner class? No, it must be done after the definition.

So, what are the members of the Outer class?

Three members are there, non-static, static, and object of the Inner class. Now let us learn a few things.

The first point is, can the Inner class access a and b members of the outer class? In the Inner class function show, can we write cout << a? No, it cannot access a. Why? Because a is a non-static member. Can we access b? Yes, we can access it. So the first point we learned is that the Inner class can access the members of the Outer class if they are static.

Second point, can the Outer class create the object of the Inner class? Yes, it can. Now using that object can it access all the members of a class? Yes, the Outer class can access all the members of the class. It is just like as if it is a class outside. Instead of writing it outside, we are writing inside the class so that it is visible only here.

We already have a function in the Outer class that is fun(). Can we say Yes, we can call this function. Can we display this cout << i.x? Can it access this member? Yes. So which members we can access? We can access only those members which are public. We cannot access private and protected members of the Inner class. So, these are public members and hence we can access it.

When to use Inner Classes in C++?

Let us see where it is useful. Suppose we are making a class called LinkedList. LinkedList is a list in which a node has a pointer to the next and so on and the first node is called as a head. LinkedList is a type of Data structure. Suppose we are making a class for this LinkedList then,

When to use Inner Classes in C++?

Here inside the LinkedList class, we have another class that is Node. The Node will contain data and a Node type pointer to the next node. So here Node is an inner class. Then LinkedList is having Node type Pointer that is Head. Then we can have functions for insert, delete, and so on. But we haven’t written here.

We guess you know something about data structure. So LinkedList is an outer class which is having inner class Node. Where this node is useful? It is only useful on LinkedList so why write this class outside. We shouldn’t write the class separately. We can have it inside the LinkedList class. So, it is useful only within that class. That is the only idea of inner classes or nested classes in C++. So now the outer class that is LinkedList can create objects of the inner class that is Node.

A LinkedList can have a class inside instead of having it outside and it can create that object and use it. A limited scope class that is visible only inside the outer class.

Example to Understand Inner or Nested Classes in C++:
#include <iostream>
using namespace std;

class Outer
    void Fun ()
        i.Display ();
    class Inner
        void Display ()
            cout << "Display of Inner" << endl;
    Inner i;

int main()
    Outer::Inner i;
    return 0;

Output: Display of Inner

In the next article, I am going to discuss Exception Handling in C++ with Examples. Here, in this article, I try to explain Inner or Nested Classes in C++ with Examples and I hope you enjoy this Inner or Nested Classes in C++ with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.