GeekInterview.com
Series: Subject: Topic:
Question: 336 of 339

What is the difference between class and structure?

Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private.
Asked by: Interview Candidate | Asked on: Jul 5th, 2005

Editorial / Best Answer

Answered by: sumitv

View all answers by sumitv

Member Since Sep-2008 | Answered On : Sep 4th, 2008

Structure does support inheritance.

try out the following code.

#include<iostream>

using namespace std;

struct Base
{
    int A;
};

struct Derived:public Base
{
    int B;
    void display();
};

void Derived::display()
{
    cout<<endl<<"A = "<<A<<endl;
}

int main()
{
    Derived D;
    D.A = 111;
    D.display();
    getchar();
    return 0;
}

Try out private and protected inheritance as well. It works. :)

Regards,
Sumit

Showing Answers 1 - 19 of 19 Answers
girish

Answered On : Jul 5th, 2005

Structure member are public in default and class members are privare in default. 

Yes  1 User has rated as useful.
  
Login to rate this answer.
Sharath

Answered On : Mar 20th, 2006

1:By default,the members of structures are public while that for class is private2: strutures doesn't provide something like data hiding which is provided by the classes3: structures contains only data while class bind both data and member functions

  
Login to rate this answer.
sunil yadav

Answered On : Nov 17th, 2006

Structure dosen't support the polymorphism, inheritance and initilization.

In a Structure all the data types are public but in class they are private.

In a Structure we can't initilse the value to the variable but in class variable we assign the values.

Structuer is a collection of the different data type.

  
Login to rate this answer.
Gagan

Answered On : Feb 5th, 2007

There should be no confusions. The only difference between a structure and a class is that all members in a class are private by default whereas they are public in a structure.

Yes  3 Users have rated as useful.
  
Login to rate this answer.
Arun

Answered On : Jul 16th, 2007

Try inheritance, polymorphism, overloading, encapsulation, etc.all these are possible.........1. struct data is public by default.. but its private in a class2. class is successor of struct in heirarchy..3. struct is ovrloaded.. in C++ its not same with class..

  
Login to rate this answer.
sumitv

Answered On : Sep 4th, 2008

View all answers by sumitv

Structure does support inheritance.try out the following code.#includeusing namespace std;struct Base{    int A;};struct Derived:public Base{    int B;    void display();};void Derived::display(){    cout<<endl<<"A = "<<A<<endl;}int main(){    Derived D;    D.A = 111;    D.display();    getchar();    return 0;}Try out private and protected inheritance as well. It works. :)Regards,Sumit

Yes  2 Users have rated as useful.
  
Login to rate this answer.
susantaown

Answered On : Sep 13th, 2008

View all answers by susantaown

  #include<iostream.h>      #include<conio.h>      #include<stdlib.h>      struct a      {      protected:      int x;      public:      a(int y)      {      x=y;      }      void showdata()      {      cout<<"x="<<x;      }      };      void main()      {      a aa(2);      aa.showdata();      getch();      }structure support inheritance in cpp

Yes  1 User has rated as useful.
  
Login to rate this answer.
anubhava_b

Answered On : Oct 10th, 2008

View all answers by anubhava_b

Similar to difference in default access specifier, default inheritance too is different in Class Vs Structures.

  
Login to rate this answer.
Vins99

Answered On : Jan 30th, 2009

View all answers by Vins99

A structure and a Class in C++ are same in every aspect expect in structure by default data members and member functions are public and private in case of class.

Yes  1 User has rated as useful.
  
Login to rate this answer.
nivi.babuji

Answered On : Jun 19th, 2009

View all answers by nivi.babuji

Structure is the ancestor of the class and the difference lies in the fact that all the members inside the class are taken as private by default but in structure all the members are made as public by default.

  
Login to rate this answer.
shrikant99

Answered On : Aug 28th, 2009

View all answers by shrikant99

By default structure is private and class is public.

  
Login to rate this answer.
sreevatsava

Answered On : Sep 17th, 2009

View all answers by sreevatsava

You cannot use inline functions in a structure

Yes  1 User has rated as useful.
  
Login to rate this answer.
sachin_mundhra

Answered On : Jan 28th, 2010

View all answers by sachin_mundhra

In case of class by default members and methods are in private section and In case of structure they are in public section.

  
Login to rate this answer.
nilesh2080

Answered On : Mar 11th, 2011

View all answers by nilesh2080

Struct cannot be inherited.. thats the thing to be remembered.

  
Login to rate this answer.

Class stores both variables and methods.
Structure stores only members(variable) of different datatype.By default the access specifiers of class are private but where as in structure it is public.

  
Login to rate this answer.
yogish

Answered On : Aug 5th, 2011

structure support multiple inheritence also try this code

Code
  1. struct stud1
  2. {
  3.         int x;
  4.         float y;
  5.         stud1(int x,float y)
  6.         {
  7.                 this->x = x;
  8.                 this->y = y;
  9.         }
  10. };
  11. struct stud2:public stud1
  12. {
  13.         char c;
  14.         stud2(int x,float y,char c):stud1(x,y)
  15.         {
  16.                 this->c = c;
  17.         }
  18. };
  19. struct stud3:public stud2
  20. {
  21.         char d;
  22.         stud3(int x,float y,char c,char d):stud2(x,y,c)
  23.         {
  24.                 this->d = d;
  25.         }
  26. };
  27. int main()
  28. {
  29.        
  30.         stud3 struTest(1,2.5,'y','x');
  31.         cout<<struTest.x<<"
  32. "<<struTest.y<<"
  33. "<<struTest.c<<"
  34. "<<struTest.d<<endl;
  35.         getchar();
  36.         return 0;
  37. }
  38.  

  
Login to rate this answer.
prasad

Answered On : Dec 27th, 2012

Inline can be used in structures ...it is possible to work

  
Login to rate this answer.
MrH

Answered On : Jun 17th, 2014

A structures methods and data members are public by default.

  
Login to rate this answer.
robertgbjones

Answered On : Aug 5th, 2014

View all answers by robertgbjones

In current C++ the ONLY difference between class and struct is that struct members (data and method) and inheritance are public by default, and class members and inheritance are private by default.

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.