Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a local class?
3.
Important points to note about local classes
4.
Frequently Asked Questions
4.1.
What is a local class?
4.2.
What is local class and global class?
4.3.
How do you create a local class?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Local Class

Introduction

Classes are the blueprint or prototype referring to which objects are created. Classes possess specific methods or properties common to all objects of the same type. You can define classes either locally or globally. Global classes are defined in the main function and not in any particular function, so all the functions in the code can access them. In contrast, local classes are declared inside a specific function and are local to that function itself. This blog will focus on Local classes and their characteristics.

Also see, Literals in C.Fibonacci Series in C++

What is a local class?

A class declared within a function is local to that function; hence it is known as a local class. The name of a local class can only be used inside the function, i.e., locally and not outside it. The local class must define its methods inside it only. A local class may have static functions but not static data members. 

Let us see an example of a local class:

#include<iostream>
using namespace std;
int main() {
   return 0;
}
void funct() {
   class Local{
   };
}

In the above code, the function is funct() and it has defined a local class inside it named Local.

Important points to note about local classes

1. We must declare the methods of a local class within the class itself. 

For example, look at the following program.

#include <iostream>
using namespace std;
void func()
{
    class Trial 
    {
    public:
        // Here, the method is defined inside the local class
        void method()
        {
            cout << "Successful";
        }
    }
    Trial x;
    x.method(); 
}
int main()
{
    func();
    return 0;
}

The above code runs successfully without any error because the method is declared inside the local class.

Output

Successful

2. You can only use a local class type name in the enclosing function.

#include <iostream>
using namespace std; 
void function()
{
    class Trial {
    };
    Trial x; 
    Trial* xp; 
}
  
int main()
{
    Trial x; 
    Trial* xp; 
    return 0;
}

In the above code, declaring the variable x and pointer xp works only inside the function() but not in the main() function.

3. A Local class can never have static data members but may have static functions. 

#include <iostream>
using namespace std;
void func()
{
    class Trial
    {
        static int var;
    };
}
int main() { 
return 0; 
}

The above code will give a compilation error on running it since it has static data members inside a local class.

4. Member methods of the local class can only access static and enum variables of the enclosing function.

#include <iostream>
using namespace std;  
void function()
{
    static int x=0;
    enum { y = 10 };
  
    class Trial {
    public:
        void method()
        {
            cout << "x = " << x << endl; 
            cout << "y = " << y << endl;        }
        };
  
    Trial t;
    t.method();
}
  
int main()
{
    function();
    return 0;
}

Output

x=0;
y=10;

This code will run without any error and give the output because static and enum variables of the enclosing function can be accessed by local classes.

5. Local classes can access global types, variables, and functions.

#include <iostream>
using namespace std; 
string s = "Coding Ninjas"; 
void ninja()
{
    class first_trial {
    public:
        first_trial() { cout << "First test successful" << endl; }
    };
 
    class second_trial {
        first_trial t1; //Local classes can use other local classes of the same function
  
    public:
        void method()
        {
            //Local class member methods can access global variables.
            cout << "s = " << s << endl;
        }
    };
  
    second_trial t2;
    t2.method();
}
  
int main()
{
    ninja();
    return 0;
}

Output

First test successful
Coding Ninjas

The above code shows that a local class can access another local class of the same function and also can use global variables and functions.

You practice by yourself with the help of online c++ compiler.

Frequently Asked Questions

What is a local class?

A class declared within a function is local to that function; hence it is known as a local class. 

What is local class and global class?

Global classes are defined in the main function and not in any particular function, so all the functions in the code can access them. In contrast, local classes are declared inside a specific function and are local to that function itself.

How do you create a local class?

A local class can be declared within the function in which you want it. That class cannot be used outside that function. 

Conclusion

In this article, we have extensively discussed local classes and their characteristics with the help of codes. Having gone through this article, I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here is a similar blog to redirect: Introduction to inner class. We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Here are some courses provided by Coding Ninjas: Basics of C++ with DSACompetitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.

Recommended Readings:

Happy Learning! 

Live masterclass