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?
class can have member functions while structure cannot.
class data members are public by default while that of structure are private.
Pointer to structure or classes cannot be declared.
class data members are private by default while that of structure are public by default.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
23 comments Page 1 of 3.

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;
}
-------------------------------------------------------------------------

Shruti said:   7 years ago
In C++, structures can also contain member functons, just like in Class.

Here's an example:

#include<iostream.h>
struct s{
int a;
void f(){ cout<<"hello";}
}ob;
int main()
{
cout << "Welcome!\n" ;
ob.f();
return 0;
}

Output:
Welcome!
hello

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++.

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++.

Vaishali Gurram said:   1 decade ago
Structure members are public by default and hence we can access it anywhere in the program through structure variable. Whereas class members are private to provide security to data members and 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...
}

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.

Princee sharma said:   5 years ago
There is a difference in structures in c++ and c.

Structures in c can't have member function but c++ can have and so this being question in reference to c++ first option is wrong.

Sumit said:   10 years ago
No, structure can't have a member functions.

The fundamental difference between C and C++. C++ supports classes (and in C++ a struct is a special case of a class), and C does not.

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.


Post your comments here:

Your comments will be displayed after verification.