Slno |
Structure |
Class |
1. |
‘Struct’ keyword is used to create fresh/new structure. |
‘class’ keyword is used to create a fresh/new class. |
2. |
‘Structure variable’ is present to handle structure elements of the structure. |
‘Object’ is present to handle data members and member functions of the class. |
3. |
Members of structure (called structure elements) are public by default. |
Members of a class are private by default. |
4. |
Structure elements are only of public type. |
Class members may be public, protected and private or mix of all these, depends on requirements. |
5. |
No security of structure elements at all, present in a structure. |
Class members may be no secure(public), medium secure(protected) and highly secure(private) or all these, depends on requirements/situation. |
6. |
Structures are best suited for handling small data structure as well. Structures are good for small and isolated model objects |
Classes are used to model more complex behavior/data. classes are suitable for larger or complex objects. |
7. |
A structure can’t be of abstract type. |
A class may be of abstract type. |
8. |
A structure contains only data member/structure elements. |
A class contains both member data and member functions. |
9. |
In a structure, we can’t initialize the value to the structure elements directly. |
In a class, we can assign the values to the member variables directly. |
10. |
Structure does not show inheritance and hence can’t be inherited. |
Classes show inheritance property and can be inherited, as well. |
11. |
Structure show only the parametrized constructor specially . |
A class shows all types of constructor i.e. default constructor ,copy constructor , and parametrized constructor. |
12. |
A structure doesn’t support destructor services/facility. |
A class support destructor facility. |
13. |
The structure can’t contain a volatile fields. |
The class contain volatile fields. |
14. |
Structure uses sizeof operator. |
A class doesn’t use sizeof operator. |
15. |
Empty structure elements in a structure are not initialized automatically. |
Empty Fields/class members in a class are automatically initialized with 0/false/null values by default |
16. |
A struct is a value type i.e. when we create structure, the variables value are assigned & holds the struct’s actual data and when new struct is created and assigned a new variable then the new variable and the original variable contain two separate copies of the same data. Finally, changes made to one copy do not affect the other copy.Thus structure is pass-by-copy of value. |
Class is a reference type i.e. when an object of a class is created, the variable of object of that class is assigned & holds only a reference to that memory. Changes made through one variable are reflected in the other variable because they both refer to the same data and class is pass by reference type. |
17. |
Structures are stored/created as a stack in memory i.e. When we create a new structure, it will be allocated as stack in memory. |
When we create a new class it will be allocated as heap in memory.
|
18. |
Normally structure can be used for grouping of data. |
Class provides flexibility for linking data and also gives re-usability concept. |
19. |
In structure, there is no garbage collector mechanism hence there is no efficient memory management technique. |
Classes have an efficient garbage collector mechanism. |
20. |
A structure without elements occupies zero byte space in memory. |
The class without members (empty) occupies one byte space in memory. |
0 Comments