A class is way to bind the data and its associated functions together. It allows data functions to be hidden, if necessary from external use.
A class specification has two parts:
The general format of a class declaration is:
The keyword class is followed by the name of the class. The body of the class is enclosed between braces and terminated by semi-colon. The class body contains
the declaration of variables and functions. These are collectively called members. The variables declared inside the class are called data members. The functions are known as member functions. The keywords public, private and protected are called visibility modes.
The data member and member functions present in private and protected mode can be accessed by the member function in public mode. The private and protected modes are exactly the same except that protected can be inherited but private mode cannot be inherited.
The use of keyword private is optional. By default, the members of a class are private. If the labels are missing, members are private by default. Such a class is completely hidden from outside world and does not serve any function.
cin >> name >>rn >> marks;
cout << name << rn << marks;
An object is an instance of a class and it can be created by using class name.
Object can be created when a class is defined by placing their names immediately after the closing brace.
} x, y, z;
The above definition would create the object x, y and z of type student.
Through object, data member and member function present in public can be accessed. The general format is:
The dot operator is called the class member access operator.
Member function can be defined in two ways:
Inside the Class
When a member function is defined inside a class, it is considered to be inline by default. If a member function is very small then it should be defined inside the class.
Outside the Class
When a function has larger code then it should be defined outside the class declaration. The prototype of such functions, however, must be provided in the class definition. The operator ‘::’ known as scope resolution operator is used to associate member functions to their corresponding class.
A member function can be called by using its name inside another member function of the same class. This is known as nesting of member functions.
The member function are created and placed in the memory space only once when they are defined in class specification. All the objects belonging to that class use the same member functions. Space for member variable is allocated separately for each object because member variable holds different value for different objects.
A constructor is a special member function that initializes the objects of its class. It is special because its name is the same as the class name. It is invoked automatically whenever an object is created. It is called constructor because it constructs the values of data members of the class. It does not have any return type, not even void.
A constructor is declared and defined as follows:
int total ;
rn = 0; total = 0;
Invokes the constructor, student() and assign rn = 0 and total = 0.
A constructor that accepts no parameter is called default constructor. If no such constructor is defined, then the compiler supplies a default constructor. In that case, it is called nothing-to-do constructor.
The constructors that can take arguments are called parameterized constructors. When the object is created, you must supply arguments to the constructor function.
A copy constructor takes a reference to an object of the same class as itself as an argument. The process of initialization through a copy constructor is known as copy initialization.
It is used to destroy the objects that have been created by a constructor. The destructor is a member function whose name is the same as the class name but is preceded by a tilde. For example the destructor of the class student can be defined as
It never takes any argument nor does it return any value. It will be invoked by the compiler upon exit from the program (or function or block) to clean the storage. It is a good practice to declare destructor in a program because it releases memory space for future use.