What are local classes in C++

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/04/local-classes-c/

A class which is declared inside a function is called a local class. A local class is accessible only within the function it is declared. Following guidelines should be followed while using local classes:

  • Local classes can access global variables only along with scope resolution operator.
  • Local classes can access static variables declared inside a function.
  • Local classes cannot access auto variables declared inside a function.
  • Local classes cannot have static variables.
  • Member functions must be defined inside the class.
  • Private members of the class cannot be accessed by the enclosing function if it is not declared as a friend function.

 

Below example demonstrates a local class:

 

Nested Classes

 

C++ allows programmers to declare one class inside another class. Such classes are called nested classes. When a class B is declared inside class A, class B cannot access the members of class A. But class A can access the members of class B through an object of class B. Following are the properties of a nested class:

  • A nested class is declared inside another class.
  • The scope of inner class is restricted by the outer class.
  • While declaring an object of inner class, the name of the inner class must be preceded by the outer class name and scope resolution operator.

 

Following program demonstrates the use of nested classes:

 

Take your time to comment on this article.

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/04/local-classes-c/

Add a Comment

Your email address will not be published. Required fields are marked *

Like the article? please consider sharing it. Thank you

Advertisment ad adsense adlogger