Chapter 3 :Introduction to classes and constructors



A class is a way to bind the data and its associated functions together. It allows the data(and functions) to be hidden, if necessary, from external use. When defining a class, we are creating a new abstract data type can be treated like any other built-in data type. Generally, a class specifications has two parts: 1. class declaration 2. class function definitions

class class_name
{

private:


variable declarations;
function declarations;

public :


variable declarations;
function declarations;
};

Member functions can be defined in two places :
Outside - the class definition.
inside - the class definition In this example getdata is outside function and putdata is inside function.
The class body contains the declaration of variables and functions. These functions and variables are collectively called members. They grouped under two sections, namely, private and public.
The members that have been declared as private can be accessed only form within the class. On the other hand,public members can be accessed from outside the class also. The data-hiding (using private declaration) is the key feature of object-oriented programming. By default , the members of class are private.
Constructors :
A constructor is a 'special' member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it construct the values of data member of the class. Characteristics of constructor :

They should be declared in the public section.

They invoked automatically when the objects are created.

They do not have return types ,not even void and therefore, they cannot return values.

They cannot be inherited, though a derived class can call the base class constructor.

Like other C++ function, they can have default arguments.

Constructors cannot be virtual.

We cannot refer to their addresses.

An object with a constructor(or destructor) cannot be used as a member of a union.

They make implicit calls to the operators new and delete when memory

In practice it may be necessary to initialize the various data elements of different objects with different values when they are created. C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.
C++ permits us to use no arguments and two arguments constructors in the same class. sharing the same name by two or more functions is referred to as function overloading. Similarly, when more than one constructor function is defined in a class, we say that the constructor overloaded.
A copy constructor takes a reference to an object of the same class as itself as an argument.
Dynamic Constructors :
Allocation of memory to objects at the time of their construction is known as dynamic construction of objects.
A destructor, as the name implies, is used to destroy the objects that have been created by a constructor. Like constructor ,the destructor is a member function whose name is the same as the class name but is preceded by tilde(~). A destructor never takes any argument nor does it return any value. It will be invoked implicitly by the compiler upon exit from the program(or block or function as the case may be to clean up storage that is no longer accessible. It is a good practice to declare destructors in a program since it releases memory space for future use.