C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 21)
21.
Which of the following is correct about class and structure?
Discussion:
23 comments Page 1 of 3.
Shivam said:
1 decade ago
Even the option A is correct. We don't have member functions declared in class.
Harsh said:
1 decade ago
I agree with shivam. Because only class have the membr function structure don't have the member function.
Samba said:
1 decade ago
In C++ we can do
>
> struct Box{
> int x;
> int y;
> void function(){cout<<x<<" "<<y<<endl;};
> }
>
> struct Box{
> int x;
> int y;
> void function(){cout<<x<<" "<<y<<endl;};
> }
Nitin Awchat said:
1 decade ago
Both ans A and B are correct
Tushar Bhimarao Dongare said:
1 decade ago
1.The data members of class are private by default and the data members of structure is public by default.
2.class contain member function and structure does not contain member function.
2.class contain member function and structure does not contain member function.
Thas said:
1 decade ago
So structure can hold member functions?
Kumar said:
1 decade ago
Yes @Thas, structures can hold functions. Here is example,
struct SomeStruct {
int x, y, z;
void someFunction();
};
void SomeStruct::someFunction() {
// Code for someFunction...
}
struct SomeStruct {
int x, y, z;
void someFunction();
};
void SomeStruct::someFunction() {
// Code for someFunction...
}
Shrinath said:
1 decade ago
In C++, a structure is a class defined with the struct keyword. Its members and base classes are public by default. A class defined with the class keyword has private members and base classes by default. This is the only difference between structs and classes in C++.
Chinmoy said:
1 decade ago
In C++ structure can contain member variable.
Ravi_Mayank said:
1 decade ago
I tried the following code (test.c) with GCC and G++ compiler.
With GCC compiler its generating a compile error.
But with G++ its working fine.
-------------------------------------------------------------------------
#include<stdio.h>
struct mystruct{
int a ;
void display(void){
printf("Hello world...[%d]\n",a);
}
} s;
int main(){
s.a=10;
s.display();
return 0;
}
-------------------------------------------------------------------------
With GCC compiler its generating a compile error.
But with G++ its working fine.
-------------------------------------------------------------------------
#include<stdio.h>
struct mystruct{
int a ;
void display(void){
printf("Hello world...[%d]\n",a);
}
} s;
int main(){
s.a=10;
s.display();
return 0;
}
-------------------------------------------------------------------------
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers