C++ Programming - OOPS Concepts - Discussion

Discussion :: OOPS Concepts - General Questions (Q.No.47)

47. 

What will happen if a class is not having any name?

[A]. It cannot have a destructor.
[B]. It cannot have a constructor.
[C]. It is not allowed.
[D]. Both A and B.

Answer: Option D

Explanation:

No answer description available for this question.

Djain said: (Apr 29, 2012)  
Then, how will you instantiate that classes?

Ram Bidwai said: (Jun 2, 2012)  
If class is not having any name then how can we create object of that class and also we can't create constructor.

Dipty said: (Mar 27, 2013)  
What is then nameless object?

Lalit Kulkarni said: (May 21, 2013)  
How we can create the object of the class?

Sonu said: (Aug 26, 2013)  
Nameless class can be considered as Abstract class.

Sonu said: (Aug 26, 2013)  
As name of class is d essential requirement to create constructor and destructor. So it can not be created. And also we can not create object of an Abstract class.

So Answer is option :D.

Kushal Tewatia said: (Sep 17, 2013)  
Can somebody give the example of a class having without name.

Seema said: (Sep 26, 2013)  
We can create objects of nameless class but at the time of creation of the class.

For example this perfect.

class { public : intx }a; //a is the object of this class

main(){

a.x=10;
cout<<"a="<<a.x;

}

Rhuda said: (Nov 13, 2013)  
Its possible in C++ but is this true for java?

Rinku said: (Feb 14, 2014)  
How can possible without class name constructor and destructor?

Rahul said: (Aug 11, 2014)  
#include<iostream.h>
class { public : int x; }a; //a is the object of this class

int main(){

a.x=10;
cout<<"a="<<a.x;
return 0;

}

Execute the above code one can get to know about class without name.

Naveen Pateriya Vidisha said: (Oct 26, 2014)  
Nice discussion its possible blindly to create a name less class.

Tejas said: (Mar 4, 2015)  
But my compiler reports an error.

Chintan said: (Nov 24, 2015)  
If you create class but not having any name, gives compilation error. So I think answer C is right.

Jatin said: (Dec 21, 2015)  
@Rahul.

I'm not getting any output as per your code.

Monika said: (Mar 26, 2016)  
Not satisfied with the answer of this question please elaborate.

Aarti said: (Aug 14, 2016)  
As said above, I have written the below code,

#include

Class
{
public :
int x;
}a; //a is the object of this class.

int main ()
{

A. X=10;.

Cout<<"a="<
Return (0) ;

}

It is giving me warning in the same compiler given here "Online C++ Compiler",

"Line 3: warning: non-local variable a' uses anonymous type".

Abhishek Chouhan said: (Feb 15, 2017)  
@Aarti

Your code is quite wrong, are you trying to access x data member of the nameless class?

Use this code.

#include <iostream>
using namespace std;
class
{
public :
int x;
}a; //a is the object of this class.

int main ()
{
a.x=10;
cout<<a.x;
return (0) ;
}

Neha said: (Mar 16, 2017)  
Use this code.

#include <iostream>
using namespace std;
class
{
public :
int x;
}a; //a is the object of this class.

int main ()
{
a.x=10;
cout<<a.x;
return (0) ;
}

This code provides warning but gives output.

Ramanand said: (Jun 25, 2018)  
@All.

As per my knowledge.

#include<iostream>
using namespace std;
class
{
public:
void aa()
{
cout<<"aryan"<<endl;
}
}a;
int main()
{
a.aa();
return 0;
}

Nikhil said: (Dec 23, 2018)  
Can I do this for java too?

How can I do it for the class which contains main function.

Salvin Kajar said: (Jan 21, 2019)  
As per my knowledge, if a class is not having a name then it's an anonymous class and anonymous class can not have a constructor but it can have a destructor.

Sravani Gudimetla said: (Nov 7, 2019)  
@Jathin:

In Rahul's code , instead of "#include<iostream.h>" you add "#include<iostream>"
Then you won't get any error.

Harsh said: (Dec 11, 2019)  
If the constructor is not made by the user then the complier automatically makes default constructor. Answer is. It connot has destructor.

Navya said: (Mar 15, 2020)  
It can have a destructor for the deallocation of memory, but the constructor cannot be allocated.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.