Coupon Accepted Successfully!



Structures in C

In C, structures are used to define custom or user-defined data type. Structure is a combination of same or different data types. Only variables are declared inside a structure. The initialization of member variables inside the structure is not permitted. Objects can directly access the data members of the structures.

Functions are not permitted as members in structure. Outside functions are also able to access the data members of the structure through the object. Thus, there is no security to data members declared inside the structure in C as shown in Figure.

In C++, structure also combines function with data members. C++ introduces new keyword class. A class in C++ is similar to structure. Using class or structure, a programmer can merge one or more dissimilar data types and a new custom data type can be created. A class is nothing but grouping of variables of different data types with functions. Each variable of a class is called as member variable. Functions are called as member functions or methods.

In C++, it is possible to access data members directly by objects, which is not possible in C. It is always the programmer’s choice to allow or disallow the direct access of data members. The mechanism of restricting access of data outside the class is called as data hiding or encapsulation. In such a case, only the member functions can access the data. If class is used in place of struct, it restricts the access of data members as shown in Figure.

class in C++

Test Your Skills Now!
Take a Quiz now
Reviewer Name