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 2 of 3.
Vikas Pareek said:
1 decade ago
Option A is correct as well because this is the difference between Classes and structures that structures cannot have member functions while classes can.
Vivek Singh said:
1 decade ago
The members of a struct are public by default, while in class, they default to private.
But the more technical:
Structure is an open container (i.e. Publicly open for everyone) that violate Encapsulation. That's why the Class is invented in c++.
But the more technical:
Structure is an open container (i.e. Publicly open for everyone) that violate Encapsulation. That's why the Class is invented in c++.
Shivam said:
1 decade ago
Even the option A is correct. We don't have member functions declared in class.
Ashutosh Ghimire said:
1 decade ago
@Ravi Mayank.
You are mixing up C and C++. C is not an OOP.
You are mixing up C and C++. C is not an OOP.
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;
}
-------------------------------------------------------------------------
Chinmoy said:
1 decade ago
In C++ structure can contain member variable.
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++.
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...
}
Thas said:
1 decade ago
So structure can hold member functions?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers