Nested Classes

1. Explain the use of Nested Classes.

2. Nested Classes as Private & Public.

3. Nested Class declaration inside the class and nested class definition outside the class.

4. Example showing the use of Nested Classes.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Nested Classes.
*/

#include

using namespace std;

class Outer
{
//Private Class.

class Inner
{
int rel;

public:

Inner()
{}
Inner(int ta)
{
rel=ta;
}
void display()
{
cout<<"\n Inner Class data is:"<<rel;
}
};

public:

//Public Class.
/*
class Inner
{
int rel;

public:

Inner()
{}
Inner(int ta)
{
rel=ta;
}
void display()
{
cout<<"\n Inner Class data is:"<<rel;
}
};
*/

//Only Declaration of Inner Class.
class SecondInner;

void set(int ta)
{
Inner obj(ta);
obj.display();
}
void display()
{ }
};

//Inner Class Definition.
class Outer::SecondInner
{
int rel;

public:

SecondInner()
{}
SecondInner(int ta)
{
rel=ta;
}
void display()
{
cout<<"\n Inner Class data is:"<<rel;
}
};

int main()
{

Outer obj;

obj.set(220);

Outer::SecondInner obj2(500);
obj2.display();

return 0;
}

About these ads

Leave a comment

Filed under C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s