Nested classes
Involving other class declarations within a class can raise the power of abstraction. A class declared within the declaration of another class is known as nested class. Nested classes give classes with non-global status. Host and nested classes follow the similar access rules for members which exist among non-nested classes. It could be used to hide specialized classes and their instances inside a host class.
The common form of nested class declaration is:
Class outer_class_name
{
private:
// data;
public:
//methods;
class inner_class_name
{
private:
//data for inner class;
public:
//methods for inner class;
}; // end of inner class declaration
}; // end of outer class declaration
A member of a class might itself be a class. Like as nesting enables building of extremely powerful data structures. The student class could be enhanced to accommodate the date of birth of a student. A new member data type date is a class through itself as displays below
class student
{
private:
int rollno;
char name[25];
char branch [15];
int marks;
public:
class date
{
int day;
int month;
int year;
date ( )
{
read ( );
}
}birthday;;
student ( )
{
------------
------------
}
student ( )
{
------------
------------
}
read ( )
{
cin>>rollno;
-----------------
birthday.read ( );
}
};
The embedded class date is declared within the enclosing class declaration. An object of type student can be defined as follows:
student s1;
The year in which the student s1 was born can be accessed as follows:
s1.birthday.year
A statement such as,
s1.date.day = 2; //error
is invalid, because members of the nested class must be accessed using its object name.
The feature of nesting of classes is useful while implementing powerful data structures such as linked lists and trees. For instance, the stack data structure can be implemented having a node data member, which is an instance of another class (node class).
Member functions of a nested class have no special access to members of enclosing class. Member functions of an enclosing class have no special access to member of a nested class.
The following class declaration illustrates how the member functions of a nested class are accessed.
Class outer
{
int a;
void out_funt(int b);
class inner
{
int x;
void inner_funt(int y);
};
};
outer ob1; // for outer class creating an object
outer :: inner :: ob2; // for the inner class creating an object
ob1.outer_funt( ); //accessing an outer class member function
ob2.inner.funt( ); // accessing of inner class member function
Whenever a class is defined as a member of another class, it holds only the scooping of outer class. The object of an outer class does not hold the object of the inner class.
//classes inside classes demonstration
# include <iostream.h>
# include<string.h>
class student_info
{
private:
char name[20];
int rollno;
char sex;
public:
student_info(char *na, int rn, char sx);
void display ( );
class date
{
private:
int day;
int month;
int year;
public:
date (int dy, int mh, int yr);
void show_date ( );
class age_class
{
private:
int age;
public:
age_class(int age_value);
void show_age ( );
};
};
};
student_info :: student_info(char *na, int rn, char sx)
{
strcpy(name, na);
rollno = rn;
sex = sx;
}
student_info :: date :: date ( int dy, int mh, int yr)
{
day = dy;
month = mh;
year = yr;
}
student_info : : date : : age_class : : age_class(int age_value)
{
age = age_value;
}
void student_info :: display ( )
{
cout <<"student's name, roll number, sex, date of birth, age \n";
cout <<" -------------------------------------------------\n";
cout << name << " "<<'\t';
cout<<rollno<<" ";
cout<<sex<<" ";
}
void student_info ::date :: show_date ( )
{
cout<<day<< '/ '<< month << ' /' <<year<<'\t';
}
void student_info::date::age_class:: show_age ( )
{
cout<< '\t' << age << endl;
cout <<" --------------------------------------------- "<< endl;
}
void main( )
{
student_info obj1("Teresa", 78909,'f');
student_info::date obj2(25,3,67);
student_infor::date::age_class obj3(33);
obj1.display ( );
obj2.show_date ( );
obj3.show_age( );
}
Output of above program is
student's name, roll number, sex , date of birth, age
-----------------------------------------------------
Teresa 78909 f 25/3/67 33
------------------------------------------------------