Anonymous classes in C++ Last Updated : 09 Mar, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // CPP program to illustrate // concept of Anonymous Class #include <iostream> using namespace std; // Anonymous Class : Class is not having any name 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; } } obj1; // object for anonymous class // Driver function int 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 // CPP program to illustrate // concept of Anonymous Class #include <iostream> using namespace std; // Anonymous Class : Class is not having any name 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; } } obj1, obj2; // multiple objects for anonymous class // Driver function int 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 // CPP program to illustrate // concept of Anonymous Class // by scope restriction #include<iostream> using namespace std; // Anonymous Class : Class is not having any name typedef 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 function int 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 Comment More infoAdvertise with us Next Article Abstraction in C++ A AlankarShelar Follow Improve Article Tags : C++ C++-Class and Object Practice Tags : CPP Similar Reads Anonymous Objects in C++ Prerequisite: Class and Objects in C++ An Object is an instance of a Class. A class has no memory allocated it is just like a blueprint, but when it is instantiated (i.e. an object is created) memory is allocated just like the real-world instance of that blueprint. An object that does not have a ref 4 min read C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and 9 min read Friend Class and Function in C++ In C++, friend functions and friend classes are concepts that allow certain functions or classes to access the private and protected members of another class. These are useful concepts in such situations where you need to give a function or another class access to internal data, while still keeping 5 min read attributes in C++ Attributes are one of the key features of modern C++ which allows the programmer to specify additional information to the compiler to enforce constraints(conditions), optimise certain pieces of code or do some specific code generation. In simple terms, an attribute acts as an annotation or a note to 11 min read Abstraction in C++ Data abstraction is one of the most essential and important features of object-oriented programming in C++. Abstraction means displaying only essential information and ignoring the details. Data abstraction refers to providing only essential information about the data to the outside world, ignoring 4 min read Inline Functions in C++ In C++, inline functions provide a way to optimize the performance of the program by reducing the overhead related to a function call. When a function is specified as inline the whole code of the inline function is inserted or substituted at the point of its call during the compilation instead of us 6 min read Like