Both a structure and a class provides a way to create a customized data type which can be used further to create instances. C++ expands the role of structure to create a class. The Structure and class, are almost similar in all respect except the significant one difference that, structure by default have all its member as “public”, and class by default have all its member “private”.
Furthermore, polymorphism and inheritance are not supported by the structure, but classes support polymorphism and inheritance. Let’s study the difference between structure and class by using the comparison chart.
Content: Structure Vs Class
Comparison Chart
Basis for Comparison | Structure | Class |
---|---|---|
Basic | If access specifier is not declared, by default all member are 'public'. | If access specifier is not declared, by default all members are 'private'. |
Declaration | struct structure_name{ type struct_element 1; type struct_element 2; type struct_element 3; . }; | class class_name{ data member; member function; }; |
Instance | Instance of 'structure' is called 'structure variable'. | Instance of a 'class' is called 'object'. |
Polymorphism and inheritance | Not supported | Supports polymorphism and a class can also be inherited. |
Nature | Value type | Reference type |
Memory is allocated on | Stack | Heap |
Null values | Not possible | Can have null values |
Requires constructor and destructor | No | Yes |
Definition of Structure
A structure is a collection of variables of dissimilar data types, all referenced by one name. A structure declaration forms a template that is used to create an instance of the structure.
Syntax
The structure is declared as follow.
struct sname { type struct_element1; type struct_element2; type struct_element3; . . . } variable1, variable2, . . .;
The keyword ‘struct’ defines to the compiler that a structure is being declared. The ‘sname’ represents the name given to the structure. The structure declaration is always terminated by a semicolon, as it is considered as a statement.
You can declare the instances of the structure before its termination as done in above code (variable1, variable2) or you can declare the instance of structure in main( ), by writing the instance name preceded with the name of the structure.
Example
main(){ sname S1, S2; }
Here S1 and S2 are the instances of the structure. The instances of the structure are called “structure variable”.
How to access a structure?
The element declared inside the body of structure can be accessed through the structure variables through the use of dot (.) operator.
//example S1.struct_element1;
- The array of structure can also be created; for this, you first need to declare a structure and then, declare an array of that type.
//sample struct sname sarray[10];
Above statement creates an array with the name ‘sarray’ containing ten variable, and each variable is organized as defined in ‘sname’.
- You can pass a structure member to a function, and you can pass an entire structure to a function.
- Like, integer pointer, array pointer, a structure pointer can also be declared by placing ‘*’ at the front of structure variables name.
//sample struct sname *S1;
Note
The ‘structure’ elements are stored in a contiguous memory location.
Definition of Class
Class in OOP defines a new type which contains data members and member function, that are used to access the data members of the class. The instances of classes are called “objects” each of which has the same organization as a class. The class is a logical abstraction, whereas an object has a physical existence.
Syntax
The class is syntactically similar to the structure. The class can be declared as follow.
class class_name { private data members and member functions. access_specifier type data_member; type mem_funct(parameter list){ . . } } object list;
Here, the class is a keyword which declares to the compiler that a class has been declared. The main feature of OOP is data hiding which is accomplished by providing three access specifiers that are “public”, “private”, “protected”. If you do not specify any access specifier in the class while declaring data members or member functions, by default all are considered private.
The public access specifier allows, functions or data to be accessed by other parts of your program. The private members of the class can only be accessed by a member of that class only. The protected access specifier is applied during inheritance. Once you declare the access specifier, it can not be changed throughout the program.
How to access an object?
The objects are nothing but the instance of the classes. The members of the class are accessed by the object of the same class using a dot (.) operator.
//object.mem_funct(arguments);
- The object can also be passed as an argument to a function.
- The pointer to an object can also be created.
Key Differences Between Structure and Class
- The main difference between structures and classes is that by default, all member of the structure are public. In contrast, by default, all the members of the class are private.
- A structure is considered as the value type whereas, a class is a reference type.
- At the time of instantiating a structure, the memory is allocated on a stack. As against, in class, the memory is alotted on the heap.
- No structure member can have a null value. Conversely, the variables of a class can have null values.
- In order to initialize the member of a class, the constructors and destructors are used. On the contrary, the structure can initialize its members automatically.
- In variables in the structure can not be initialized during the declaration while in case of class, this can be done.
Similarities
- In C++, both the structure and class are syntactically equivalent.
- Both structure and class can declare some of their members private.
- The name of a structure or class can be used as a stand-alone type.
Conclusion
The structure in C had some of the limitations as the structure do not permit data hiding, ‘struct’ datatype could not be treated like built-in types; structures do not support inheritance. Structure in C++ overcame these limitations.
In C++, classes are the expanded version of the structure. The programmer finds it easy to use the class to hold data and function both and structure only to hold data.
Jignesh says
Nice differences
shayan says
Nice Differences