C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 47)
47.
What will happen if a class is not having any name?
Discussion:
25 comments Page 1 of 3.
Aarti said:
9 years ago
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".
#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
Abhishek Chouhan said:
9 years ago
@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) ;
}
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) ;
}
Ramanand said:
7 years ago
@All.
As per my knowledge.
#include<iostream>
using namespace std;
class
{
public:
void aa()
{
cout<<"aryan"<<endl;
}
}a;
int main()
{
a.aa();
return 0;
}
As per my knowledge.
#include<iostream>
using namespace std;
class
{
public:
void aa()
{
cout<<"aryan"<<endl;
}
}a;
int main()
{
a.aa();
return 0;
}
Rahul said:
1 decade ago
#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.
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.
Seema said:
1 decade ago
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;
}
For example this perfect.
class { public : intx }a; //a is the object of this class
main(){
a.x=10;
cout<<"a="<<a.x;
}
Neha said:
8 years ago
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.
#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.
Sonu said:
1 decade ago
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.
So Answer is option :D.
Salvin kajar said:
7 years ago
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.
(2)
Sravani Gudimetla said:
6 years ago
@Jathin:
In Rahul's code , instead of "#include<iostream.h>" you add "#include<iostream>"
Then you won't get any error.
In Rahul's code , instead of "#include<iostream.h>" you add "#include<iostream>"
Then you won't get any error.
Harsh said:
6 years ago
If the constructor is not made by the user then the complier automatically makes default constructor. Answer is. It connot has destructor.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers