CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C++ DETAILS
Question :
What is the difference between structures and classes in C++?

Category C++ Interview Questions
Rating (5.0) By 1 users
Added on 7/19/2006
Views 2972
Rate it!
Answers:

Difference between structures and clas...

Ans:

Structs are value-type variables and are thus saved on the stack, additional overhead but faster retrieval. Another difference is that structs cannot inherit.




we can give function definition and function declaration in class but this is not possible in structure



 Posted by: sajag rachhora    

Contact sajag rachhora  Contact sajag rachhora

structure is used to store the data which is having sub parts .
where as class is a collection of member variables and member functions
it is not possible to call the function in structure .where asit is possible in class.



 Posted by: sreelatha    

Contact sreelatha  Contact sreelatha

Willing to add some more points
2)By default the members variables in Structure z public whereas in Class it private
3)Class contains Member Functions, its not so in struct



 Posted by: parvati    

Contact parvati  Contact parvati

structures can be used in both c and c++ while class is an OOP Concept, so it can be used in C++ only.
A class has a constructor and a destructor while a structure doesnt have any.
A class can be inherited while a structure cannot be inherited.
By default all the member variables in a structure are public while in a class they are private. The member variables can be public,protected, and private in a class but in a structure its always public.



 Posted by: sandeep chowdary.A    

Contact sandeep chowdary.A  Contact sandeep chowdary.A

1. it is possible to declare member functions in a struct also, you can call them...

the only difference is that: By default the members variables in Structure public whereas in Class it private

here is the proof:

struct hari
{
int nNumber;

void SetNumber(int nNewValue);
int GetNumber();


};

void hari::SetNumber(int nNewValue)
{
nNumber =nNewValue;
}

int hari::GetNumber()
{
return nNumber;
}
main()
{
struct hari h;
h.SetNumber(10);
int i= h.GetNumber();
}



 Posted by: haribabu    

Contact haribabu  Contact haribabu

Yes hari is Right.
Only diff. is that:

By default structure variable are public whereas class variable are private.



 Posted by: VIKAS GUPTA    

Contact VIKAS GUPTA  Contact VIKAS GUPTA

And also its possible to inherit STRUCture in C++



 Posted by: Anandha    

Contact Anandha  Contact Anandha

1) Structure in .cpp file can have a Constructor and Destructor; where as Structure in .c can not have Constructor and Destructor.

2) Structures (both .c and .cpp) can not have a direct member functions but can have function pointers as a member ( which points to address of static/global function ).



 Posted by: Mahesh    

Contact Mahesh  Contact Mahesh


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is an object?


View Answer
What is the difference between class and structure?


View Answer
We can overload assignment operator as a normal function.But we can not overload assignment operator as friend function why?



View Answer
What are virtual functions?


View Answer
What is the Basic nature of "cin" and "cout" and what concept or principle we are using on those two?

View Answer
What is a scope resolution operator?


View Answer
What do you mean by inheritance?


View Answer
What is the difference between an object and a class?


View Answer
What is encapsulation?


View Answer
what is defference between constructor and destructor


View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C++ Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/6146/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.97