20 Apr 2017

Difference Between Structure and Class


structure and classBoth 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. Both structure and class, are similar in all respect except one difference that, structure by default have all its member as “public”, and class by default have all its member “private”. Let’s study the difference between structure and class by using comparison chart.

Content: Structure Vs Class

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Similarities
  5. Conclusion

Comparison Chart

BASIS FOR COMPARISONSTRUCTURECLASS
BasicIf access specifier is not declared, by default all member are 'public'.If access specifier is not declared, by default all members are 'private'.
Declarationstruct structure_name{
type struct_element 1;
type struct_element 2;
type struct_element 3;
.
.
.
};
class class_name{
data member;
member function;
};
InstanceInstance of 'structure' is called 'structure variable'.Instance of a 'class' is called 'object'.

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.  The structure is declared as follow.
  1. struct sname{
  2. type struct_element1;
  3. type struct_element2;
  4. type struct_element3;
  5. .
  6. .
  7. .
  8. } 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.
  1. // example.
  2. main(){
  3. sname S1, S2;
  4. }
Here S1 and S2 are the instances of the structure. The instances of the structure are called “structure variable”. The element declared inside the body of structure can be accessed through the structure variables through the use of dot (.) operator.
  1. //example
  2. 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.
  1. //sample
  2. 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.
  1. //sample
  2. 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 use 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. The class is syntactically similar to the structure. The class can be declared as follow.
  1. class class_name{
  2. private data members and member functions.
  3. access_specifier
  4. type data_member;
  5. type mem_funct(parameter list){ . . }
  6. } 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.
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.
  1. //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

  1. The main difference between structures and classes is that by default all member of the structure are public whereas, by default all the members of the class are private.

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.
  • Both structure and a class support the mechanism of inheritance.

Conclusion:

The structure in C had some of the limitation 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.

No comments:

Post a Comment

commnet here