Anonymous Object in C++

Anonymous Object is a Object without any name.

In C++ programming an object is created with names but It is still possible to create objects without names such objects are known as anonymous objects.

When the constructor and destructor are called, the data members are initialized and destroyed respectively.

Without an object, we can initialize and destroy the content of the class.

All these operations are carried out without an object or we can also assume that operations are carried out using an anonymous object.

Example: Create a Simple C++ class and call it without reference variable.

  1. Create a simple class.
  2. Call a constructor from main()
#include 

using namespace std;

class A {
    public: A() //   constructor  
    {
        cout << "Constructor executed" << endl;
    }~A() // Destructor  							
    {
        cout << "Destructor executed" << endl;
    }
};
int main() {
    A(); // calling constructor without any name anonymous object
    A(); // calling constructor without any name anonymous object
}

Output

Constructor executed
Destructor executed
Constructor executed
Destructor executed

 

Note: In the above program, we have created class “A” which contains the constructor and destructor.

In the main() function, no object is created.

The constructor is called directly.

Calling a constructor directly means creating an object ( Local object and global object in C++) but not accessing it with any name. It is not possible to create more than one anonymous object at a time.                              

When the constructor execution ends, the destructor is executed to destroy the object.

Example: Showing student details Using an Ananonyous object in C++

Steps for developing a program

  1. Create a class name Student
  2. Declare private members of the class
  3. Declare constructor to take arguments
  4. Assign local variable to Object variable
  5. Show student data in the show()
  6. Define Destructor
  7. Call Constructor( without creating object) from the main method.
#include 
#include 
using namespace std;
class Student {
    private:
        char name[30];
        long rollNo;
        char branch[30];
    public:
        Student(const char * name, long rollNo,
            const char * branch) //   constructor  
    {
        cout << "Constructor executed" << endl;
        strcpy(this -> name, name);
        this -> rollNo = rollNo;
        strcpy(this -> branch, branch);
        show();
    }~Student() // Destructor   
    {
        cout << "Destructor executed" << endl;
    }
    void show() {
        cout << "Student Details are" << endl;
        cout << "Name " << name << endl;
        cout << "Roll No " << rollNo << endl;
        cout << "Branch " << branch << endl;
    }
};
int main() {
    Student("Ram", 12, "CSE"); // calling constructor
    return 0;
}

Result:

Constructor executed
Student Details are
Name Ram
Roll No 12
Branch CSE
Destructor executed

Read More

  1. C++ program for student details
  2. Lowercase character to uppercase character
  3. Command Line Argument
  4. Local vs Global Object in C++
  5. Local and Nested Classes in C++
  6. Arithmetic operations using switch case
  7. Switch Case in C Programming
  8. Constructor in C++
  9. Know more about C++

Reference

C++ classes