-->

Nested Class

Nested Class

What is nested class? Explain with example.
Nested class means a class within a class. The class which is nesting a class is known as enclosing class or outer class, and the nested class is known as inner class. Nested class can be defined anywhere means in the private, public or protected section of the enclosing class. The name of the nested class or inner class is inside the local scope of its enclosing class. Nested classes are very rarely used.
class enclose
{
    private:
    int a;
    public:
    void set_enclose();
    void get_enclose();
class inner
{
    private:
    int c;
    public:
    void set_inner()
    {
             c=5;
    }
    void get_inner()
    {
             cout<<c<<endl;
    }
};
};
void enclose::set_enclose()
{
    a=9;
}
void enclose::get_enclose()
{
    cout<<a<<endl;
}
int main()
{
    clrscr();
    enclose e;
    e.set_enclose();
    e.get_enclose();
    enclose::inner i;
    i.set_inner();
    i.get_inner();
    getch();
    return 0;
}