October 13, 2018
pramod kumar mishra
Nested Class:-
If a class declared inside another class is known as nesting of classes. The outer class is known as enclosing class and the inner class is known as nested class.
A nested class is a member of outer or enclosing class so all the so it will have same access right as other member function contains.
Inner class object must be preceded the outer class name with scope resolution operator(: :).
#include <iostream>
#include<conio.h>
using namespace std;
class Outer
{
int a;
public:
int c;
//private:
class inner
{
int d;
public:
int e;
void show1()
{
cout<<"private variable is called in public member function="<<d*d<<endl;
}
inner()
{
d=10; // Initialize the inner class variable at the time of object call.
e=20;
}
};
inner ob1; //private scope object of inner class.
public:
inner ob2; // public scope object of inner class.
void show2()
{
ob1.show1(); //ob1 Behaves as a private variable of outer class.
show();
}
Outer()
{
a=30;
c=40;
}
private:
void show()
{
ob2.show1();
}
};
int main()
{
Outer ob;
//inner in; // we can not make object of inner class directly.
Outer::inner oi; // we can not make object of private inner class. inner class must be public.
oi.show1();
ob.show2();
return 0;
}
Posted in: