Home » C++ programs » C++ Class and Objects programs

Local Class with Example in C++

C++ | Local class with Example: Here, we are going to learn about the Local class in C++ with an example. What is a local class, how declare/define a local class and how to use it?
Submitted by IncludeHelp, on September 19, 2018

Local Class in C++

In C++, generally a class is declared outside of the main() function, which is global for the program and all functions can access that class i.e. the scope of such class is global.

A local class is declared inside any function (including main() i.e. we can also declare a class within the main() function) and the scope of local class is local to that function only i.e. a local class is accessible within the same function only in which class is declared.

Example:

Here, we are declaring and defining two classes "Test1" and "Test2", "Test1" is declared inside a user-defined function named testFunction() and "Test2" is declares inside the main() function.

Since classes "Test1" and "Test2" are declared within the functions, thus, their scope will be local to those functions. Hence, "Test1" and "Test2" are local classes in C++.

Program:

#include <iostream>
using namespace std;

//A user defined function
void testFunction(void)
{
    //declaring a local class
    //which is accessible within this function only
    class Test1
    {
        private:
            int num;
        public:
            void setValue(int n)
            {
                num = n;
            }
            int getValue(void)
            {
                return num;
            }
    };
    
    //any message of the function
    cout<<"Inside testFunction..."<<endl;
    
    //creating class's object
    Test1 T1;
    T1.setValue(100);
    cout<<"Value of Test1's num: "<<T1.getValue()<<endl;
}

//Main function
int main()
{
    //declaring a local class
    //which is accessible within this function only
    class Test2
    {
        private:
            int num;
        public:
            void setValue(int n)
            {
                num = n;
            }
            int getValue(void)
            {
                return num;
            }
    };
    
    
    //calling testFunction
    cout<<"Calling testFunction..."<<endl;
    testFunction();
    
    //any message of the function
    cout<<"Inside main()..."<<endl;
    
    //creating class's object
    Test2 T2;
    T2.setValue(200);
    cout<<"Value of Test2's num: "<<T2.getValue()<<endl;
    
    return 0;
}

Output

Calling testFunction...
Inside testFunction...
Value of Test1's num: 100
Inside main()...
Value of Test2's num: 200





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.