×

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions. Objects are also called instances of the class. A class in C++ is referred to as a blueprint of an object.

Let us understand the definition of a class with a real-world example.

Example 1

Consider a class that contains bikes. There are many brands of bikes, such as Honda, Suzuki, Royal Enfield, etc. All these have some common attributes: bikes are 2-wheeled vehicles and have handles and mileage range. Etc. Here the brands are objects of the class bike.

Syntax

class Classname{
//block of code
};

In C++, the class keyword represents classes.

Global class

In C++, a class declared(defined) outside the body of a program's functions is referred to as a global class.

In simple words, a class definition occurring outside the body of all the functions is the global class.

Within the program body, the objects of the global class are declared at any desired place. Global class is the everywhere accessible class.

Syntax

class Global// Global class
{
      public:
      void gfunc()  //function
      {
          cout<<"global class \n";  //function body
      }
}obj;  //object 

1) Program code for Global class in C++

#include <iostream>
using namespace std;
class Global   // Global class
{
      public:
      void gfunc() // this is the global function
      {
          cout<<"global class \n"; //function body(prints “global class” as output)
      }
}obj; // obj is the global object


int main()
{
 obj.gfunc();
return 0;
}

Output

global class

Explanation

In the above program, a global object, "obj", is created to access the global function through a global class. Here the global class has the name “Global”. The global class “Global” contains the function gfunc(); this function prints "global class" as the output on the output screen. The global class is accessed through the object from the main function of C++.

2) Program code for Global class in C++

#include <iostream>
using namespace std;
class Global   // Global class
{
      public:
      void gfunc() // this is the global function
      {
          cout<<"global class \n"; //function body(prints “global class” as output)
      }
};


int main()
{
 Global object; // object is created from the main function
object.gfunc(); //function “gfunc()” accessed through the global class
return 0;
}

Output

global class

Explanation

The above program creates a global object "object" to access the global function through a global class. Here the global class has the name "Global". The global class "Global" contains the function “gfunc()”; this function prints "global class" as the output on the output screen. The object is directly created in the main function, and the function "gfunc()” is accessed through the object of the global class.

The objects of global classes can be created anywhere, even in the main function.

Local class

In C++, a class declared(defined) inside the body of any program function is referred to as a local class.

In simple words, a class definition occurring inside the body of a function is the local class.

The objects of the local class cannot be declared everywhere, and the local objects have their scope limited to that particular local class only. The local classes cannot be accessed outside the function.

Syntax

void fun()  // function name “fun”
{
     class Local        // local class (class defined inside the function)
   {
       public:
       void lfunc()    // a function defined inside the local class
       {
           cout<<"local class \n";        // function body
       }     
   };
Local o;        //local object
o.lfunc();
}

Program code for Local class in C++

#include <iostream>
using namespace std;
void fun()      // function name “fun()
{
     class Local             // local class defined inside the function
   {
       public:
       void lfunc()    //function defined inside the local class.
       {
           cout<<"local class \n";   //function body
       }     
   };
Local o;  //object “o” (local class object)
o.lfunc();
}
int main()
{
fun();  //function call
return 0;
}

Output

local class

Explanation

In the above program, a local object “o” is created to access the local function “lfunc()” through a local class inside the function “fun()”. Here the local class has the name “Local”. The function “fun()” has the local class “Local” that contains the function lfunc(), this function prints “local class” as the output on the output screen. The local class is accessed through the object from the function “fun()” of the above program.

The local objects cannot be created in other functions; the compiler gives an error.

Example of global and local classes in C++

#include <iostream>
using namespace std;
class Global              // Global class name “Global”
{
      public:
      void gfunc()   // function “gfunc()”
      {
          cout<<"global class \n";  //function body
      }
}obj;  //object of the class “Global”
void fun()    //function “fun()”
{
     class Local    // local class defined inside the function “fun()”
   {
       public:
       void lfunc()  //function defined inside the local class
       {
           cout<<"local class \n";  //function body of “lfunc()”
       }     
   };
Local o;    //local object “o”
o.lfunc();
}
int main()
{
obj.gfunc();   
fun();
return 0;
}

Output

global class 
local class

Conclusion

A class declared outside all functions is global because the objects can be created everywhere (at any desired block) in the program. If a class is declared within a function body, it is referred to as a local class because objects of this class are local to its defined function.


Related Topics

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

C++ Virtual Destructor

In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The...

4 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

Array of Object in C++

What is an Array? In C++, an array is a group of related data types, such as int, char, float, double, etc., that are easily accessed by index value alone and...

12 minutes read.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

C++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Multilevel Inheritance

C++ Multilevel Inheritance Multilevel inheritance is such an inheritance in which a derived class is created from another derived class. C++ Multilevel Inheritance Example In this example, a base class Student is inherited in...

2 minutes read.

C++ Bitset

Overview In C++, bitset represents a fixed-sequence of some bits values by either 0 and 1. Zero represents the value as false or unset, while 1 represents the value as true...

4 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

4 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

C++ cin and cout

In this article, we will discuss the C++ cin and cout with their library and examples. C++ Standard Input/Output: User-program communication is made possible by C++’s usage of input and output (I/O)...

5 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.