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.

#include 
using namespace std;
class A {
        private:
                int x;
        public:
                void read() {
                        x = 20;
                }
        void show() {
                cout << " x=" << x;
        }
};
int w = 100; // global variable w
int main() {
        static int z; // static variable z
        class B // local class
        {
                private:
                        int y;
                public:
                        void read() {
                                y = 30;
                                z = 50;
                        }
                void show() {
                        cout << " y=" << y;
                        cout << " z=" << z;
                        cout << " w=" << w;
                }
        };
        A a;
        a.read();
        a.show();
        B b;
        b.read();
        b.show();
}

Output

 
 x=20 y=30 z=50 w=100
 

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

#include 
using namespace std;
int main() {
        int z; // non static variable z
        class B // local class
        {
                private:
                        int y;
                public:
                        void read() {
                                y = 30;
                                z = 50;
                        }
                void show() {
                        cout << "y=" << y;
                        cout << "z=" << z; //error, non static variable cant access here 
                }
        };
        B b;
        b.read();
        b.show();
}

Output

 error, non static variable cant access here
 

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:

 
class outer_class_name { // outer class
        private:
                public:
                class inner_class_name { //inner class
                        private:
                                public:
                };
        end of inner class
};
end of outer class
 

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.

#include 
using namespace std;
class student { //outer class student
        private: char name[20];
        int rollno;
        public: void student_info();
        void display();
        class date { // inner class date
                private: int date, month, year;
                public: void getdate();
                void displaydate();
        }; //end of date class(inner class)
}; //end of student class(outer class)
void student::student_info() {
        cout << "enter name & roll number = ";
        cin >> name >> rollno;
}
void student::display() {
        cout << "\n name = " << name << endl;
        cout << " roll number = " << rollno << endl;
}
void student::date::getdate() {
        cout << " enter date month & year = ";
        cin >> date >> month >> year;
}
void student::date::displaydate() {
        cout << "\n date = " << date;
        cout << "\n month = " << month;
        cout << "\n year = " << year;
}
int main() {
        student x; // create an object x of outer class  student
        student::date y; // create an object y of inner class  student	
        x.student_info();
        y.getdate();
        x.display();
        y.displaydate();
}

Output

 enter name & roll number =
rajeev //user enter		
112 //user enter 
enter date month & year = 12 //user enter					
02		//user enter
2012                   //user enter   

Name = Rajeev roll number = 112 date = 12 month = 02 year = 2012

 

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++