Difference between Structure and Classes


Difference between Structure and Classes

  • The major difference between class and structure is that the declaration of structure
    starts with the keyword 'struct' whereas on the other hand, a class starts with the
    keyword 'class'.
  • In class the data member and member are private by default whereas in structure they
    are public by default.
  • Data hiding is supported in classes but not in structure.
  • Structure deals with variables only whereas objects deal with real-world objects.

If we explicitly specify the access type of each member, then a structure will behave exactly
as a class.

The different forms of structure and class are as follows :

Structure :

struct struct-name
{
data-type member1;
data-type member2;
data-type membern;
};
struct struct-name v1,v2...vn;

Here member1,member2...are data members.

Class :

class class-name
{
private :

data members;
member functions;

public :

data members;
member functions;

protected :

data members;
member functions;

};
class class-name obj1,obj2...objn;

In structure v1,v2...vn is the variables list while in class ob1,ob2..obn is the object list.

Also note that the structure members can be accessed directly by the structure variables by
function anywhere in their scope(local or global), whereas class members can't.

The reason is that the class members are private by default whereas the structure members
are public default.




     Prev                                                   NEXT

0 comments:

Post a Comment