Local and Nested Classes in C++

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

Local And Nested Classes In C++
Local And Nested Classes In C++

Local Classes in C++

Inside the local class, we can access the global and static variables.

Local class can’t access the normal/nonstatic variable of a function.


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

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

The local class can’t access the non-static variable of the function


Nested  Classes in c++

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

 Syntax of the 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.


Local Vs Nested class in C++

A local class in similar to a local variable which is defined inside a function or block.

A nested class is a class inside another class. we have created a student class inside the student class there is an address class, so the address is the inside student. The student class is nested inside the student.

Hope you understood Local and Nested Classes in C++.

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Lowercase character to uppercase character
  4. Command Line Argument
  5. Local vs Global Object in C++
  6. Arithmetic operations using switch case
  7. Switch Case in C Programming
  8. Constructor in C++
  9. Know more about C++
Categories C++