Difference Between Namespace and Class in C++



In this article, we will see the differences between namespace and class in C++. Namespace and classes are two different concepts, so let's discuss them:

Classes are datatypes. It is an expanded version of the structures. Classes can contain data members and functions as members, but namespaces can contain variables and functions by grouping them into one and cannot be created as objects; it is used as additional information to differentiate similar functions, classes, variables, etc. Variables, functions with the same name can be placed in different namespaces.

What is Namespace?

The namespace is a feature that provides a way to group related identifiers, such as variables, functions, and classes, under a single name. It provides a scope to define or declare (the names of types, functions, variables, etc) inside it.

It is used to prevent name conflict in C++. When different parts of a program use the same name for variables, functions, or classes, causing confusion for the compiler. To avoid this, C++ introduces a namespace.

Namespace Definition

A namespace definition starts with keyword namespace and is followed by the namespace name as follows:

namespace nameOfNamespace {
   // type1 mamber1
   // type2 mamber2
   // type3 mamber3
   .
   .
   .
}

Syntax

Following is the syntax of the namespace:

namespace_name::member_name;

Implementation of namespace in C++

This is an example of the namespace without using the class:

#include <iostream>
using namespace std;
// Defining a namespace
namespace MathOperations {
   int add(int a, int b) {
      return a + b;
   }

   int multiply(int a, int b) {
      return a * b;
   }
}
int main() {
   cout << "Addition: " << MathOperations::add(5, 3) << endl;
   cout << "Multiplication: " << MathOperations::multiply(4, 2) << endl;
   return 0;
}

Following is an output:

Addition: 8
Multiplication: 8

What is Class?

Class is a user-defined data type that holds its own data members and member functions that can be accessed and used by creating an instance of that class. It is an expanded version of the structures.

Class Definition

We can define a class using the keyword class. A class must be defined before it is used.

class nameOfClass {
  access_specifier:
   // data member
   // member function
};

Implementation of Class in C++

Following is an example of the class without using the namespace ?

#include <iostream>
class Car {
   public:
      // Member variables
      std::string brand;
   int speed;

   // Member function
   void showInfo() {
      std::cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << std::endl;
   }
};

int main() {
   // Creating an object of the Car class
   Car myCar;
   myCar.brand = "Maruti Suzuki";
   myCar.speed = 120;
   myCar.showInfo();

   return 0;
}

Following is the output:

Brand: Maruti Suzuki, Speed: 120 km/h

Using a Class Inside a C++ Namespace

Here, is an example of C++ that combines both a namespace and a class:

#include <iostream>
using namespace std;
namespace MyNamespace {
   // A class inside namespace
   class MyClass {
      public: void showMessage() {
         cout << "Hello from MyClass inside MyNamespace!" << endl;
      }
   };
}

int main() {
   // Creating an object of MyClass inside MyNamespace
   MyNamespace::MyClass obj;
   obj.showMessage();

   return 0;
}

Following is the output of the above code:

Hello from MyClass inside MyNamespace!

Difference between Namespace and Class

Following are the key differences between classes and namespace:

Feature Namespace Class
Purpose Groups identifiers to avoid naming conflicts Defines objects with data and behavior
Instantiation Cannot be instantiated Must be instantiated to access members (unless static)
Reusability and Extension Can be reopened and extended across translation units Cannot be reopened once defined
Access Control No access specifiers: All members are public Supports public, private, and protected access modifiers
Updated on: 2025-06-18T18:44:13+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements