Classes - Part -I
The most important feature of C++ is the "Class". Its significance can be highlighted from the fact the Stroustrup initially gave it the name "C with classes". A class can be seen as an extension of the idea of structure which is used extensively in C. It is a new way of creating and implementing a user defined data type. Today we will discuss the concept of class by reviewing the traditional structures found in C and then the ways in which classes can be designed, implemented and applied.Revisiting Structure from C
One of the unique features of C language is structures. They provide a method for packing together data of different types. A structure is a convenient tool for handling a group of logically related data items. It is a user defined data type with a template that serves to define its data properties. Once the structure type has been defined, we can create variables of that type using declarations that are similar to the built in type declarations, For example, in the following declaration :
struct student
{
char name[20];
int roll_number;
float total_marks;
};
The keyword Struct declares student as a new data type that can hold three fields of different data types. These fields are known as structure members or elements. The identifier student, which is referred to as a structure name or structure tag, can be used to create variables of type student.
Structures can have arrays, pointers or structures as members.
Limitations of C Structure
The C language does not allow the struct data type to be treated like a built in data type. For example consider the following structure
struct complex
{
float x;
float y;
}'
struct complex c1, c2, c3;
The complex numbers c1, c2 and c3 can be easily assigned values using the dot operand but we cannot add two complex numbers or subtract one from the other. For example :-
c1 = c2 + c3 ; is illegal in C.
Another important limitation of C structure is that they do not permit data hiding. Structure members can be directly accessed by the structure variables by any function anywhere in their scope. In other words, the structure members are public members.
Structures Extended in C++
C++ supports all the features of structures as present in C. But as is expected C++ has expanded the capabilities of structures further to suit its Object Oriented Programming philosophy. It brings the user defined types as close as possible to the built in data types, and also provide a facility to hide the data which is one of the main principles of OOP. Inheritance, a mechanism by which one data type can inherit characteristics from other data types is also supported by C++.
Some of the important features of Structures in C++
- A structure can have both variables and functions as data members.
- Some of the data members can be declared as private which makes it impossible for external functions to access the data.
- The keyword struct is not compulsory.
C++ incorporates all these changes in structures as a new user defined type known as Class. There is very little syntactical difference between structures and classes.
Generally in C++ we use structure for functions only holding data and use classes when we need to hold both data and functions.
Note :- The only major difference between a structure and a class in C++ is that by default the members of a class are private, while in structure it is public by default.
We will learn more about classes in the next post. Keep visiting!