In this tutorial we will learn about Classes and its use in C++. Classes form one of the major features that powers C++.
- It is mechanism of packaging data of different types alongwith their functions.
- Classes is a derived data type (like structures), it contains members of other data types and also associated functions.
Classes are in many respects similar to structures, but they have the added capability to also contain functions to operate upon the data members.
By definition structures are used for grouping data members(simple data variables) whereas classes are used for grouping data members as well as functions associated with these variables, but most compilers allow the use of class and structure almost interchangebly with minor variations in their implementation.
However we will restrict our use of structures only for data members and that of classes for data and functions.
Class data basically contains data variables which may simple int , char, float or other derived data type such as arrays, objects(or structures or classes) also
The declaration will be identical to that used in declaring any variable.
As already pointed out classes may contain functions.
Again the functions used in classes are linke any other regular function.
We can inlcude the function definition inside the class also but usually it is better to keep[ the function definitions outside the class.
In such situations we make use of the scope resolution operator '::' to tell the compiler that the functions definition is for a member function of the class.
There are three visibility modes namely: public , private , protected.
These modes determine whether the class members (data and functions) are accessible outside the class or only within the class member functions.
If any member is private it cannot be accessed outside the calls. Only member functions can access it.
Any public member can be accessed from outside the class by its object.
→ the use of these modes will be more clear from the examples that we discuss.
1. If no mode is specified , it is defaulted to private
2. Visibility modes also determine which members are inheritable. The concept of inheritance will be discussed later on. Till then its sufficient to assume that private and protected modes are equivalent.
Declaring class objects follows the same syntax and scope rules as structure objects
In the above example
- rollno is declared as a public member of the class so obj[i].rollno is accessible from non-member functions( main in this case)
- name and age are private members so we have to access them through member functions
- again output() and input() are public member functions , so they are accessible from outside.
- in the member function , the members of the the object are accessed without the use of the ‘.’ Operator i.e. in the member functions the data members can be treated as local variables.
- member function can access other variables/function in the program and also declare variables if required
Class provided a way of initializing objects automatically- through the use of constructors.
- a constructor is basically a function
- constructor name is same as that of the class it belongs to
- a constructor has no return type i.e. it cannot return any value
A destructor is used to free the memory used by any class object when we no longer that object
#we have separate tutorial on this topic and hence more details are being omitted
Sometimes it is required that some external function (i.e. which is not member of a particular class) can access some private /protected member of that class. Such a function is called friend function.
A friend function is no different from any other function except for the fact that it can access the private members of the class it is a friend to.
A friend function is declared and used as any other function, the only difference being that its declaration also comes in the class declaration preceded by the word friend.
all the functions of a class can be made friend to another class by making the former a friend class of the latter.
→ friend functions do tend to make the code messy and are as such sparingly used.
Kindly post your doubts on our discussion forum.