Local and Nested Classes in C++

In a C++ language we can define a class within a function and such class are called local class.

Inside the local class we can access global and static variable.

Local class can’t access the normal/non static variable of function.


Description: In the above program, class B is defined within main() function means class B is local class.

Local class B can access the static variable “z” of main() function as well as can access  global variable “w”.

Local class can’t access the non static variable of function


Nested  Classes

In the C++ programming, we can declare the class within another class and a  class declared as a member of another class is called as a nested class.

 Syntax of  nested class declaration:

Que. Write a C++ program to create a class “student”  which contains the data member such as name, roll number  and also consist one nested class “date” ,whose data are day, month and year. The value of student read from keyboard and display using nested class.