How to use Nested Classes in C++

 

How to use Nested Classes in C++ Programming language?

In this tutorial we will discuss,

How to define nested class as private member of enclosing class?

How to define nested class as public member of enclosing class?

Define member functions of Nested class outside the enclosing class?

How to define Nested class outside the enclosing class?

Definition of Nested Class:

A nested class is a class defined inside another class. The class which contains the nested class is called as Enclosing class.

Video Tutorial

How to define nested class as private member of enclosing class?

The nested class can be defined as private member of enclosing class. The object of enclosing class can be used to access the member function of the nested calss.

Following programm demonstrates the concept:

 #include<iostream>
 using namespace std;
 class A
 {
     private:
         class B
         {
             public:
                 void display()
                 {
                     cout<<"Nested class"<<endl;
                 }
         };
     B in;
 public:
     void show()
     {
         in.display();
     }
 };
 int main()
 {
     A out;
     out.show();
     return 0;   
 }

Output:

Netsed Class

Program Explanation:

Here the class A is enclosing class and B is nested class. The member function display of nested class is accessed using the object of enclosing class.

How to define nested class as public member of enclosing class?

We can define the nested class as a public member of enclosing class. In this case, the public member function of nested class can be accessed from the object of enclosing class directly.

Following programm demonstrates the concept:

 #include<iostream>
 using namespace std;
 class A
 {
     public:
         class B
         {
             public:
                 void display()
                 {
                     cout<<"Nested class"<<endl;
                 }
         };
 };
 int main()
 {
     A::B in;
     in.display();
     return 0;   
 }

Output:

Nested Class

How to define member functions of Nested class outside the enclosing class?

The member function of the nested class can be defined outside the enclosing class. Here we need to use the scope resolution operator.

Following programm demonstrates the concept:

#include<iostream>
 using namespace std;
 class A
 {
     public:
         class B
         {
             public:
                 void display();
         };
 };
 void A::B::display()
 {
     cout<<"Nested class"<<endl;
 }
 int main()
 {
     A::B in;
     in.display();
     return 0;   
 }

Output:

Nested Class

How to define Nested class outside the enclosing class?

The nested class can be defined outside the enclosing class. Here we need to use the scope resolution operator.

Following programm demonstrates the concept:

 #include<iostream>
 using namespace std;
 class A
 {
     public:
         class B;
 };
 class A::B
 {
     public:
         void display()
         {
             cout<<"Nested class"<<endl;
         }
 };
 int main()
 {
     A::B in;
     in.display();
     return 0;   
 }

In this tutorial, we have discussed How to use Nested Classes in C++ Programming language?

If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds