Coupon Accepted Successfully!


Local Classes

When classes are declared inside the function then such classes are called as local classes. The local classes have access permission to global variables as well as static variables. The global variables need to be accessed using scope access operator when the class itself contains member variable with same name as global variable. The local classes should not have static data member and static member functions. If at all they are declared, the compiler provides an error message. The following programs illustrate the local classes.

8.33 Write a program to define classes inside and outside main() function and access the elements.

Explanation: In the above program, class A is declared before main() function as usual. The class B is declared inside the main() function. Both the functions have two member functions get() and show(). The get() function reads integers through the keyboard. The show() functions display the values of data members on the screen.

8.34 Write a program to declare global variables, read and display data using member functions.

Explanation: The above program is so far same as previous one. In addition, in this program global variables j, k, l, and m are declared. The member functions get() and show() read and display values of member variables as well as global variables. Here, both the classes contain a single data member variable with the same name as global variables. Thus, to access the global variable where necessary, scope access operator is used. The output of the program is as shown in the above program.

Test Your Skills Now!
Take a Quiz now
Reviewer Name