Classes

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.

REMARK

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.

Declaration

class class_name
        {
            visibility mode:
            class data
            member functions
        }

 

Class data 

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.

 

Member functions

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.

 

Syntax for defining function outside the class
return-type class_name::function_name (argument list)
{
body of function
}

 

Visibility modes

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. 

Private/Protected

If any member is private it cannot be accessed outside the calls. Only member functions can access it.

Public

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.

 

NOTE

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.

 

Class objects

Declaring class objects follows the same syntax and scope rules as structure objects

 

EXAMPLE 1 

#include<iostream>
#include<iomanip>
#include<cstdlib>
using namespace std;

class STUDENT
        {

            int  age;
            char name[20];
        public:
            int rollno;
            int input();
            int output();


        };

int STUDENT::input()
{
    cout<<endl;
    cout<< "Enter Roll number :: ";
    cin>>rollno;
    cout<<"Enter name :: ";
    fflush(stdin);
    gets(name);
    cout<<"ENter age :: ";
    cin>>age;


return 1;
}
int STUDENT::output()
{
    cout<<endl<<"Roll number\t::\t"<<rollno;
    cout<<endl<<"Name\t\t::\t"<<name;
    cout<<endl<<"Age\t\t::\t"<<age;

return 1;
}


int main()
{
STUDENT obj[5];
int rollno,i;
cout<<"Enter details for the 5 students\n";
for( i=0; i<5;i++)
    {

    cout<<"Student "<<i+1<<endl;
    obj[i].input();
    }

cout<<endl<<"enter roll no for student whose details are to be found  :";
cin>>rollno;

for(i=0;i<3;i++)
{
if(obj[i].rollno==rollno)
{obj[i].output();break;}
}

if(i==5)
cout<<endl<<endl<<rollno<<"  Not Found";

}
 

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

 

Constructor

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

 

Destructor

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

 

Friend functions

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.


Friend classes

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.