Classes & Objects In C++


 

Structure of C++ Program

Include files: These are nothing but the files that are already processed and the function in it are used in the program.

Class declaration: The most important feature of C++ is the "class". A class is an extension of the idea of structure used in C. It is a new way of creating and implementing user-defined data type.

The structure used in C were having some limitation and therefore to overcome it bjarne introduce the concept of class.

A class is a way to bind data and its associated function together a class specification has two parts:

  1. Class declaration
  2. Class function definitions.

The class declaration describe the type and score of its members. The class function definition describe how the class function are implemented.

Syntax:

class<class_name>{

private:

variable declaration;

function declaration;

public:

variable declaration;

function declaration;

}

Variable declared in a class are known as members variable and function are known as a member functions.

Example:

class item{

int number;//member variables

float cost;

public:

void getdata(int a,float b);

void putdata();//member function

};

Creating objects

In the above example the declaration of item class does not define any object of item but only specifies what they will contain. Once a class is declared , we can create variable of that type by using the class name.

Eg.

Item x;//memory for x is created

Here x is a variable of type item.in C++ class variable are known as objects. Therefore ,x is called as object of type item. We can declare more than one object for a class as follows:-

Item x,y,z;

Object can be cretaed when a class declaration is done as follows:-

Class Item{

….

….

}x,y,z;

Accessing class members:

Private data of a class can accessed only through the member function of that class, to access them we need to use the object of that class type as follows:-

Object_nmae.function_name(actual arguments);

Eg.

x.getdata(100,75.5);

Even we can access the member variables if they are declared as public.

Scope resolution operator[::] 

When a class declared, the member function are defined inside the class. But the program is not readable to make it readable we can define the member function outside the class definition to define it outside the class a scope resolution[::] operator is used. Use of this operator help us to access the member function outside the class.

Syntax:

Return type class-name::function-name(){

Body of the function;

}

Eg.

Class Emp{

public:

void get();

};

void Emp::get(){

//function definition

}

Example:

#include<iostream.h>

int m=10;

void main(){

int m=20;

{

int k=m;

cout<<"we are in inner block";

cout<<"k="<<k;

cout<<"m="<<m;

cout<<"::m="<<m;

}

cout<<"we are in outer block";

cout<<"m="<<m;

cout<<"::m="<<m;

}

In the above program, the variable m is declared at three places, namely outside, inside the main() function, inside the main(),and inside the inner block, Here ::m will always refer to the global m.in the inner block, ::m refer the value of 10 and not 20.

Use of scope resolution operator is done in classes to identify the class to which a member function belongs.

Encapsulation

It is a process of hiding data of a class from outer class our function.it is also known as data hiding .This is achieved by using accesses specifiers :- private , public and protected.

Private

It is an access specifier which helps to hide the data member and member function of a class from the outside classes and function. By default the class has a private mode of data declaration. To protect the data from outside class we have to use the private keyword

Public

It is an access specifier which help us to make the data member and member function of a class available for the outside classes and function. To make the data accessible globally we have to use public keyword.

Nesting of member function

We had seen that only an object of that class using dot operator could call a member function. However , there is an exception to this. 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 function.

Array of objects

We know that array can be of any data including struct. Similarly we can also create an array of variable that are of the type class. Such variable are called array of objects.

Eg.

class Empolyee{

char name[10];

float age;

public:

void get();

void show();

};

The identifier employee is user defined data type and can be used to create objects that relate to different category of the employee.

Eg.

Employee manager[3];//array of manager

Employee clerk[5];//array of clerk

Employee worker[75];//array of worker

The array manager contain three objects, namely manager[0],manager[1],manager[2], of type employee class. Similarly for clerk array contain 15 objects and worker contain 75 objects.

Array objects behave same as any other array, so we can use the usual array accessing method to access individual method.

E.g:

manager[0].get();

or

manager[i].get();

an array object is stored inside the memory in the same way as a multidimensional array. the space is allocated for data items(member variable) of the object are created.

Member function are stored separately and will be used by all the objects.

Array within a class

Array can be used as member variable in class.

E.g:

class arr{

int a[10];

public:

void get();

void show();

};

The array variable a[] declared as private member of the class arr can be used in the member function, like any other variable.