In a C++ language we can define a class within a function and such classes are called local classes.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <iostream> 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
1 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> 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
1 | 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:
1 2 3 4 5 6 7 8 9 10 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <iostream> 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
1 2 3 4 5 6 7 8 | 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++.