C++ Programming - OOPS Concepts - Discussion

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

21. 

Which of the following is correct about class and structure?

[A]. class can have member functions while structure cannot.
[B]. class data members are public by default while that of structure are private.
[C]. Pointer to structure or classes cannot be declared.
[D]. class data members are private by default while that of structure are public by default.

Answer: Option D

Explanation:

No answer description available for this question.

Shivam said: (May 6, 2012)  
Even the option A is correct. We don't have member functions declared in class.

Harsh said: (Aug 19, 2012)  
I agree with shivam. Because only class have the membr function structure don't have the member function.

Samba said: (Sep 12, 2012)  
In C++ we can do
>
> struct Box{
> int x;
> int y;
> void function(){cout<<x<<" "<<y<<endl;};
> }

Nitin Awchat said: (Sep 17, 2012)  
Both ans A and B are correct

Tushar Bhimarao Dongare said: (Sep 27, 2012)  
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.

Thas said: (Dec 20, 2012)  
So structure can hold member functions?

Kumar said: (Dec 24, 2012)  
Yes @Thas, structures can hold functions. Here is example,

struct SomeStruct {
int x, y, z;

void someFunction();
};

void SomeStruct::someFunction() {
// Code for someFunction...
}

Shrinath said: (Jul 21, 2013)  
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: (Jul 26, 2013)  
In C++ structure can contain member variable.

Ravi_Mayank said: (Sep 18, 2013)  
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;
}
-------------------------------------------------------------------------

Ashutosh Ghimire said: (Oct 13, 2013)  
@Ravi Mayank.

You are mixing up C and C++. C is not an OOP.

Vaishali Gurram said: (Jan 30, 2014)  
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.

Vivek Singh said: (Jun 3, 2014)  
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++.

Vikas Pareek said: (Oct 8, 2014)  
Option A is correct as well because this is the difference between Classes and structures that structures cannot have member functions while classes can.

Raghava said: (Jan 3, 2015)  
Can structures in C, have member functions?

Sumit said: (Jun 5, 2015)  
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.

Bhavin Panchal said: (Jun 19, 2016)  
Option A also true.

Priya said: (Jul 15, 2016)  
Anybody can explain option A, in detailed form?

Badal Maity said: (Aug 24, 2016)  
The option A is correct.

Manpreet said: (Dec 29, 2016)  
I think A is also the correct answer.

Shruti said: (Sep 2, 2017)  
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

Princee Sharma said: (Oct 4, 2019)  
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.

Rahul Shirole said: (Aug 17, 2021)  
Structures in c++ can have member functions like class.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.