Classes and their members

Let’s take a short tour of some of the essential features of classes and related concepts in C++.

A class has data variables and member functions. These may be public or private. Public members are declared under the public section of the class definition, while private members are declared under the private section. Publicly declared members are visible outside of the class, while private members are not accessible from outside the class. This essentially means that only members of a class can access private members of that class. However, functions that are not part of the class can access its public members.

A class definition might look as follows:

class EXAMPLE_CLASS

{

private:

//private data variables and member functions.

public:

//public data variables and member functions.

};


Instantiation of the class i.e. creation of an object of class EXAMPLE_CLASS is done as follows:

EXAMPLE_CLASS example_obj;


A constructor is a member function which is called automatically when an object of the class is created. We generally use it to provide initial values to the data member variables of the object being created. It has the same name as the name of the class, and is declared under the public section of the class definition. Further, a constructor does not have any return type, not even void. A constructor may or may not have arguments passed to it. A constructor which can be called without passing any arguments to it is called the default constructor of the class. The programmer can define more than one constructor for a class – these constructors are distinguished by the compiler on the basis of the number and type of the parameters passed on to it. This is described as the constructor being overloaded. Let us look at an example:

class MY_CLASS

{

private:

int x;

public:

// the default constructor initializes x with value 10.

MY_CLASS( ) { x = 10;}

// the parametrized constructor also initializes x.

MY_CLASS ( int y) { x = y; }

};

Consider two instantiations of MY_CLASS.

MY_CLASS obj1;

MY_CLASS obj2(35);

After the above instantiations, obj1 has value 10 in its private member x, while obj2 has value 35 for its variable x.

The member functions of a class may be classified as modification member functions and constant member functions. A modification member function, as its name suggests, is allowed to modify any data variables that are part of the class. And again, as its name suggests, a constant member function is not allowed to modify the data variables that are part of the class.

Let’s look at an example:

class MY_CLASS

{

private:

int x, y;

public:

MY_CLASS(int value) { x = y = value;}

//change_x is a modification member function

void change_x (int my_value) {x = my_value;}

//print_xy is a constant member function

void print_xy ( ) const { cout << x << y; }

};

As we can see from the example, the function change_x modifies the class member x. Hence it is a modification member function.

Consider a function such as print_xy. As is clear from the name, this function prints the value of the class members x and y. So, by the name itself, and even before writing the (one-line) code for the function, we know that this function will not be modifying any member variables. Thus, we can declare this function to be a constant function by putting the keyword const after the parameter list of the function. This means that, if by chance, we err in writing the code and accidentally write a statement, as part of the the function’s implementation, that tries to modify any member variable, the compiler would indicate an error. (This is similar to what happens if you try to modify the value of a const variable, such as a const int.)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: