Nested Class in C++ with example



In the C++, we can declare a class within another class.
A class declares as a member of another class is called as a nested class or a class within another class.
The name of a nested class is local to the enclosing class. The nested class is in the scope of its enclosing class.

Example:
#include<iostream.h>
#include<conio.h>
class student
               {
                              private :
int id;
   public:
void get();
void put();
class result
{
int marks;
   public:
void getdata();
void putdata ();
};
};
void student :: get()
{
cout<<”Enter the Student Info=”;
cin>>id;    
}
void student :: put()
{
               cout<<”Student Info”;
               cout<<id;
}
void student :: result :: getdata()
{
cout<<”Enter the Student marks=”;
cin>>marks;    
}
void student :: result :: putdata ()
{
               cout<<”Student marks=”;
               cout<<marks;
}

void main()
{
clrscr();
student st;
student :: result rt;
st.get();
rt.getdata();
st.put();
rt.putdata();
getch();
}

No comments:

Post a Comment