The power of abstraction of a class can be increased by including other class declarations inside a class. A class declared inside the declaration of another class is called nested class. Nested classes provide classes with non-global status. Host and nested classes follow the same access rules for members that exist between nonnested classes. Nested classes could be used to hide specialized clases and their instances within a host class.
A member of a class may itself be a class. Such nesting enables of very powerful data structures. The student class can be enhanced to accommodate the date of birth of a student. The new member data type date is a class by itself as show below.
Sample program
#include <iostream.h>
class Student
{
int rno;
char name[30];
char branch[40];
int marks;
public:
class date
{
int day;
int month;
int year;
public:
void setdate(void)
{
cin>>day>>month>>year;
}
void showdate(void)
{
cout<<day<<”-“<<month<<”-“<<year<<endl;
}
}dob;
void setstud(void)
{
cout<<”Enter the Student Number”;cin>>rno;
cout<<”Enter the Student Name”;cin>>name;
cout<<”Enter the Student Branch”;cin>>branch;
cout<<”Enter the Date of Birth”; dob.setdate();
}
void showstud( void)
{
cout<<”Student Number”<<rno;
cout<<”Student Name”<<name;
cout<<”Student Date of Birth”;dob.showdate();
}
};
void main()
{
Student A1;
A1.setstud();
A1.showstud();
}
No comments:
Post a Comment