Nested classes

What is Nested classes in C++?

Explanation

Nested class is a class defined inside a class, that can be used within the scope of the class in which it is defined. In C++ nested classes are not given importance because of the strong and flexible usage of inheritance. Its objects are accessed using "Nest::Display".

Example :



#include <iostream.h>
class Nest
{
public:
class Display
{
private:
int s;
public:
void sum( int a, int b)
{ s =a+b; }
void show( )
{ cout << "\nSum of a and b is:: " << s;}
};
};
void main()
{
Nest::Display x;
x.sum(12, 10);
x.show();
}

Result :

Sum of a and b is::22

In the above example, the nested class "Display" is given as "public" member of the class "Nest".

C++ Tutorial


Ask Questions

Ask Question