×

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

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.

C++ Polymorphism

Polymorphism refers to the fact that something exists in several forms. Polymorphism, in simple terms, is the ability of a message to be presented in several formats. A real-life example...

4 minutes read.

Decimal to Octal in C++

We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of...

3 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

C++ storage classes

Storage Classes are used to characterize a variable's or function's characteristics. These characteristics include scope, visibility, and life-time, which allow us to track the presence of a variable over the...

5 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

Do-While Loop Examples in C++

Before we move on the examples of Do-While loop, let’s learn little bit about the Do-While loop in C++ language. Do While loop An iterative loop that checks the condition at the...

5 minutes read.

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

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++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.