C++ Classes and Objects Tutorial:
C++ Classes and Objects?
The most important feature of C++ is the “class”. Its significance is highlighted by the fact that Stroustrup initially gave the name “C with Class” to his new languages. A class is an extension of the idea of structure use in C. it is a new way of creating and implementing a user-defined data types.
C STURCTURE REVISED
Structure provides a method for packing together data of different types. A structure is a convenient tool for handling for a group of a 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 the type using declarations that are similar to the built-in type declarations.
Example
struct student
{
char name[20];
int roll_number;
float total_marks;
};
The keywords struct declares student as anew 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 structure name or structure tag, can be used to create variables of type student.
Limitations of C Structures:
The standard C does not allow the struct data type to be treated like built-in types.
For example:
struct complex
{
float x;
float y;
};
struct complex c1, c2, c3;
the complex numbers c1, c2 and c3 can easily be assigns values using the dot operator, but we cannot add two complex numbers or subtract one form the other.
For Example:
c3 = c1 + c2; //Is illegal in C.
Another important limitation of C structures 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.
Extension to Structures
In C++, a structure can have both variables and functions are members. It can also declare some of its members as ‘private’ so that they cannot be accessed directly by the external functions.