Anonymous classes in C++ - GeeksforGeeks

09 Mar, 2018
Anonymous class is a class which has no name given to it. C++ supports this feature.
These classes cannot have a constructor but can have a destructor.
These classes can neither be passed as arguments to functions nor can be used as return values from functions.
Examples to illustrate Anonymous Classes
Creating single object of Anonymous Class : In the first Example, Anonymous class is created with object name obj1. The scope of the obj1 is throughout the program. So, we can access this into the main function. In main, using obj1, a call is given to member functions of the anonymous class.// CPP program to illustrate // concept of Anonymous Class#include <iostream>using namespace std;  // Anonymous Class : Class is not having any nameclass{    // data member    int i; public:    void setData(int i)    {        // this pointer is used to differentiate        // between data member and formal argument.        this->i = i;    }    void print()    {        cout << "Value for i : " << this->i << endl;    }      } obj1;     // object for anonymous class  // Driver functionint main(){    obj1.setData(10);    obj1.print();    return 0;}Output :Value for i : 10
Creating two objects of Anonymous Class : In the Second example, we have created two objects obj1 and obj2 for Anonymous class and given a call to member functions of the class. The scope of the obj1 and obj2 is through out the program. Likewise, we can create multiple objects for an anonymous class.// CPP program to illustrate // concept of Anonymous Class#include <iostream>using namespace std;  // Anonymous Class : Class is not having any nameclass{    // data member    int i; public:    void setData(int i)    {        // this pointer is used to differentiate        // between data member and formal argument.        this->i = i;    }    void print()    {        cout << "Value for i : " << this->i << endl;    }      } obj1, obj2;    // multiple objects for anonymous class  // Driver functionint main(){    obj1.setData(10);    obj1.print();      obj2.setData(20);    obj2.print();    return 0;}Output :Value for i : 10
Value for i : 20
Restricting the scope of Anonymous class : To restrict the scope of the objects for the anonymous class, we can take a help of typedef. In the third example, by using typedef we can give a convenient name to class and use that name we have created multiple objects obje1 and obj2 for the anonymous class. Here we can control the scope of the obj1 and obj2 objects, which are inside the main function.// CPP program to illustrate // concept of Anonymous Class// by scope restriction#include<iostream>using namespace std;  // Anonymous Class : Class is not having any nametypedef class{    // data member    int i; public:    void setData(int i)    {        // this pointer is used to differentiate         // between data member and formal argument.        this->i = i;    }    void print()    {        cout << "Value for i :" << this->i << endl;    }      } myClass;      // using typedef give a proper name  // Driver functionint main(){    // multiple objects    myClass obj1, obj2;     obj1.setData(10);    obj1.print();      obj2.setData(20);    obj2.print();    return 0;}Output :Value for i : 10
Value for i : 20

Creating single object of Anonymous Class : In the first Example, Anonymous class is created with object name obj1. The scope of the obj1 is throughout the program. So, we can access this into the main function. In main, using obj1, a call is given to member functions of the anonymous class.// CPP program to illustrate // concept of Anonymous Class#include <iostream>using namespace std;  // Anonymous Class : Class is not having any nameclass{    // data member    int i; public:    void setData(int i)    {        // this pointer is used to differentiate        // between data member and formal argument.        this->i = i;    }    void print()    {        cout << "Value for i : " << this->i << endl;    }      } obj1;     // object for anonymous class  // Driver functionint main(){    obj1.setData(10);    obj1.print();    return 0;}Output :Value for i : 10

// CPP program to illustrate // concept of Anonymous Class#include <iostream>using namespace std;  // Anonymous Class : Class is not having any nameclass{    // data member    int i; public:    void setData(int i)    {        // this pointer is used to differentiate        // between data member and formal argument.        this->i = i;    }    void print()    {        cout << "Value for i : " << this->i << endl;    }      } obj1;     // object for anonymous class  // Driver functionint main(){    obj1.setData(10);    obj1.print();    return 0;}
Output :
Value for i : 10

Creating two objects of Anonymous Class : In the Second example, we have created two objects obj1 and obj2 for Anonymous class and given a call to member functions of the class. The scope of the obj1 and obj2 is through out the program. Likewise, we can create multiple objects for an anonymous class.// CPP program to illustrate // concept of Anonymous Class#include <iostream>using namespace std;  // Anonymous Class : Class is not having any nameclass{    // data member    int i; public:    void setData(int i)    {        // this pointer is used to differentiate        // between data member and formal argument.        this->i = i;    }    void print()    {        cout << "Value for i : " << this->i << endl;    }      } obj1, obj2;    // multiple objects for anonymous class  // Driver functionint main(){    obj1.setData(10);    obj1.print();      obj2.setData(20);    obj2.print();    return 0;}Output :Value for i : 10
Value for i : 20

// CPP program to illustrate // concept of Anonymous Class#include <iostream>using namespace std;  // Anonymous Class : Class is not having any nameclass{    // data member    int i; public:    void setData(int i)    {        // this pointer is used to differentiate        // between data member and formal argument.        this->i = i;    }    void print()    {        cout << "Value for i : " << this->i << endl;    }      } obj1, obj2;    // multiple objects for anonymous class  // Driver functionint main(){    obj1.setData(10);    obj1.print();      obj2.setData(20);    obj2.print();    return 0;}
Output :
Value for i : 10
Value for i : 20

Restricting the scope of Anonymous class : To restrict the scope of the objects for the anonymous class, we can take a help of typedef. In the third example, by using typedef we can give a convenient name to class and use that name we have created multiple objects obje1 and obj2 for the anonymous class. Here we can control the scope of the obj1 and obj2 objects, which are inside the main function.// CPP program to illustrate // concept of Anonymous Class// by scope restriction#include<iostream>using namespace std;  // Anonymous Class : Class is not having any nametypedef class{    // data member    int i; public:    void setData(int i)    {        // this pointer is used to differentiate         // between data member and formal argument.        this->i = i;    }    void print()    {        cout << "Value for i :" << this->i << endl;    }      } myClass;      // using typedef give a proper name  // Driver functionint main(){    // multiple objects    myClass obj1, obj2;     obj1.setData(10);    obj1.print();      obj2.setData(20);    obj2.print();    return 0;}Output :Value for i : 10
Value for i : 20

// CPP program to illustrate // concept of Anonymous Class// by scope restriction#include<iostream>using namespace std;  // Anonymous Class : Class is not having any nametypedef class{    // data member    int i; public:    void setData(int i)    {        // this pointer is used to differentiate         // between data member and formal argument.        this->i = i;    }    void print()    {        cout << "Value for i :" << this->i << endl;    }      } myClass;      // using typedef give a proper name  // Driver functionint main(){    // multiple objects    myClass obj1, obj2;     obj1.setData(10);    obj1.print();      obj2.setData(20);    obj2.print();    return 0;}
Output :
Value for i : 10
Value for i : 20

C++-Class and Object
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Inline Functions in C++
Pair in C++ Standard Template Library (STL)
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Destructors in C++