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.

Editorial / Best Answer

sumitv  

  • Member Since Sep-2008 | 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 - 65 of 65 Answers

girish

  • Jul 5th, 2005
 

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

Sharath

  • Mar 20th, 2006
 

1:By default,the members of structures are public while that for class is private

2: strutures doesn't provide something like data hiding which is provided by the classes

3: structures contains only data while class bind both data and member functions

  Was this answer useful?  Yes

sunil yadav

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

  Was this answer useful?  Yes

Gagan

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

Arun

  • 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 class
2. class is successor of struct in heirarchy..
3. struct is ovrloaded.. in C++ its not same with class..

  Was this answer useful?  Yes

sumitv

  • 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

susantaown

  • Sep 13th, 2008
 

  #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

Vins99

  • Jan 30th, 2009
 

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.

nivi.babuji

  • Jun 19th, 2009
 

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.

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

yogish

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

  Was this answer useful?  Yes

prasad

  • Dec 27th, 2012
 

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

  Was this answer useful?  Yes

MrH

  • Jun 17th, 2014
 

A structures methods and data members are public by default.

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

srishty

  • Jun 13th, 2015
 

Structure are value type so they get allocated on stack while class is reference type so they get allocated on heap

  Was this answer useful?  Yes

Ashish

  • Mar 24th, 2016
 

You are absolutely wrong my friend. Only difference between class and struct is the members in a class are private by default whereas in struct its public. You can have private member data and member functions is a struct as well. Modern C++, starting from C++ 11 is an entirely different animal.

  Was this answer useful?  Yes

Ashish

  • Mar 24th, 2016
 

Beware, whatever is supported by a class is also supported by struct whether its inheritance, polymorphism, encapsulation, data hiding etc. etc. as of C++ 11. Only difference is, for a class default access specifier is private whereas for struct its public.

  Was this answer useful?  Yes

Sajeed Ullah

  • Dec 2nd, 2016
 

All data members of the class is by default private,
Whereas all data members of structure is by default public.

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions