
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Local Class in C++
A class declared inside a function is known as a local class in C++ as it is local to that function.
An example of a local class is given as follows.
#include<iostream> using namespace std; void func() { class LocalClass { }; } int main() { return 0; }
In the above example, func() is a function and class LocalClass is defined inside the function. So, it is known as a local class.
A local class name can only be used in its function and not outside it. Also, the methods of a local class must be defined inside it only. A local class cannot have static data members but it can have static functions.
A program that demonstrates a local class in C++ is given as follows.
Example
#include<iostream> using namespace std; void func() { class LocalClass { private: int num; public: void getdata( int n) { num = n; } void putdata() { cout<<"The number is "<<num; } }; LocalClass obj; obj.getdata(7); obj.putdata(); } int main() { cout<<"Demonstration of a local class"<<endl; func(); return 0; }
Output
Demonstration of a local class The number is 7
In the above program, the class LocalClass is declared in the function func() so it is a local class. The class a variable num and two member functions that initialize and display num. After the creation of the class, its object obj is defined in the function func() and getdata() and putdata() are called using obj. This is seen as follows.
void func() { class LocalClass { private: int num; public: void getdata( int n) { num = n; } void putdata() { cout<<"The number is "<<num; } }; LocalClass obj; obj.getdata(7); obj.putdata(); }
In the function main(), the function func() is called. This is shown below.
cout<<"Demonstration of a local class"<<endl; func();
- Related Articles
- Local inner class in Java
- Local Inner Class in C#
- How to get local date in android using local date API class?
- Can a method local inner class access the local final variables in Java?
- How to get local time in android using localtime API class?
- What are class variables, instance variables and local variables in Java?
- How to get local time and date in android using LocalDateTime API class?
- How to set local date/time in a table using LocalDateTime class in Java?
- How to get offsetdatetime local date in android using offset date time API class?
- Local variables in Java
- Upload from local drive to local filesystem in HTML with Filesystem API
- Storing Credentials in Local Storage
- Local procedure calls in Windows
- final local variable in Java
- Final local variables in C#
