C++ Classes and Objects

Class

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:

  1. Class declaration
  2. Class function definitions

Class Declaration

The general format of a class declaration is:

class class-name
{
private :
 Variable declarations;
 Function declarations;
public:
 Variable declarations;
 Function declarations;
protected :
 Variable declarations;
 Function declarations;
};

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.

Example

#include <iostream.h>
class student
{
private:
char name[80];
int rn;
float marks;
private:
void getdata();
void putdata();
};
void student::getdata()
{
cin >> name >>rn >> marks;
}
void student::putdata()
{
cout << name << rn << marks;
}
void main()
{
student st;
st.getdata();
st.putdata();
}

Creating Objects

An object is an instance of a class and it can be created by using class name.

student st;

Object can be created when a class is defined by placing their names immediately after the closing brace.

class student
{
} x, y, z;

The above definition would create the object x, y and z of type student.

Accessing Class Member

Through object, data member and member function present in public can be accessed. The general format is:

Object_name.data_member;
Object_name.member_function;

The dot operator is called the class member access operator.

Member Function Definition

Member function can be defined in two ways:

  1. Inside the class
  2. Outside the class

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.

Nesting of Member Functions

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.

Memory Allocation for Objects

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.

Constructor

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:

class student
{
int rn;
int total ;
public:
student()
{
rn = 0; total = 0;
}
};

The declaration

student st;

Invokes the constructor, student() and assign rn = 0 and total = 0.

Default Constructor

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.

Parameterized Constructors

The constructors that can take arguments are called parameterized constructors. When the object is created, you must supply arguments to the constructor function.

Copy Constructor

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.

Destructor

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

- student();

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.