C++ class Introduction
The most important feature of C++ is the class. it is significance is highlighted by the fact that stroustrup
initially gave the name C with classes to this new language. A class is an extension of the structure used in C language. It is new way of creating and implementing a user defined data type we shall discuss, in this tutorial, the concept of the class by first reviewing
the traditional structure found in C and then the ways in which classes can be designed implemented and applied
C STRUCTURE REVISITED
We know that one of the unique features of the C language is structures. They provide a method for packing together data of different types. A structure is a convenient tool for handle a group of
logically related data items. It is a user defined data types with a template that serves to define its data properties. once the structure type has been defined, we can create variables of that structure type using declarations that are similar to the built in type declaration.
for example consider the following declaration
student
{
name[20];
roll;
total_mark;
};
|
The keyword struct declares student as a new data type that can hold three fields (data members)of different
Data types. These fields are known as elements or structure members . The identifier student
which is referred to as structure tag or structure name , can be used to create variable of type
student
Example
struct student A; //C declaration
A is variable of type student and has three member variable as defined by the template.
Member variable can be accessed by using the dot or period operator as follows below
strcpy(A.name,"nitish");
A.roll=900;
a.total_mark=3444.3;
Finaltotal=a.total_mark+5;
|
structure can have arrays, pointers or structures as members
LIMITATIONS OF C STRUCTURES
The standard C does not allow the struct data type to be treated like build in type for example consider The following structure
Emp
{
x;
y;
};
Emp e1,e2,e3;
|
The complex number e1,e2 and e3 can easily be assigned values using the dot operator, but we cannot add two complex numbers or subtract one from the other for example
e3=e1+e2;
is illegal in C
Another Important limitation of C structure of C structures is that they do not permit data hiding. structure members can be directly accessed by the structure variable by any function anywhere in their scope.
in other words, the structure members are public members
EXTENSIONS TO STRUCTURES
C++ support all the features of structures as defined in C. but C++ has expanded its capabilities further to suit its OOPS philosophy. It attempts to bring the user defined data types as close as possible to the built in data types, and also provides a facility to hide the data which is one of the main concept of Object oriented programming. Inheritance, a mechanism by which one type can inherit characteristics from other types, in also supported by C++.
In C++ language, a structure can have both variable and functions as members. it can also declare some of its members as private so they cannot be accessed directly by the external functions
IN C++ language, The structure names are stand alone and can be used like any other type names.
In other words, The keyword struct can be omitted in the declaration of structurevariable. for example, we can declare the student variable A as
Student A;
Remember, this is an error in C
C++ language incorporates all these extensions in another user defined type known as class. There is very little syntactical difference between structure and classes in C++ and , therefore they can be used interchangeably with minor modifications. since class is a specially introduced data type in c++ language, most of the C++ programmers tend to use the structures for holding only data, and classes to hold both the data and functions. Therefore we will not discuss structures any further
Note: The only difference between a structure and a class in c++ is that, by default, the members of a class are private, while, by default, the members of a structure are public