How do you use classes and objects in C++ programming?

Introduction to classes

How do you use classes and objects in C++ programming? There are generally two categories of programming: one is procedural and the second one is object oriented programming (OOP). In procedural programming, the concept of procedures or functions is used. So, functions are the building blocks of procedural programming while objects are the building blocks for object oriented programming.

It has been studied that for binding the data of same type arrays are used. For binding the data of different types, the structures are used where the values to different data items are assigned using structure variables. Now if you have to bind the different types of data items as well as the functions, then Classes are available in C++.

How do you define classes and objects in C++?

The general syntax used for classes is as follows

class class_name

{

private:

collection of data items

public:

collection of member functions

};

The two keywords private and public are called the access specifiers. Their description will be given in more detail while discussing the idea of inheritance.  For the time being it can be said that the class data members are usually declared under the tag of private while the member functions are placed under the tag of public. The word private implies that these data members are available only within the class and can not be accessed outside the class. The member functions of the class can only have access to them. The member functions are usually declared as public. It means they cal also be accessed outside the class.

What are the objects?

The object has the same relation to a class that a data member has to its type.  Object is also called an instance  of a class. Objects are used for execution of constructors and calling member functions. The following example of a class shows the role of objects in OOP.

Example

class abc

{

private:

int  a,b.c;

public:

void getdata(int x,int y,int z)

a=x; 

b=y;

c=z;

}

void showdata()

{

cout<<“the values of class data members are:”<<a<<b<<c<<endl;

}

void main()

{

clrscr();

abc obj1;

obj1.setdata(12,13,14);

obj1.showdata();

getch();

}

In this above example obj1 is the object of class abc. It is used for assigning values to the data members a,b,c of class. Two functions setdata and showdata are used for assigning and displaying values of data members. They are defined inside the class to they can be said inline functions. It is also possible to define member functions somewhere outside  the class but then they will not be called inline functions. These functions are accessed using objects of class with dot(.) operator.

Objects can also represent variables of user defined data types. consider the following case where the area of a rectangle is to be calculated.

class rectangle

{

private:

int length;

int width;

public:

void setdata()

{

cout<<“enter length”<<endl;

cin>>length;

cout<<“enter width”<<endl;

cin>>width;

}

void calculatearea()

{

cout<<“area is:”<<length*width<<endl;

}

};

void main()

{

clrscr();

rectangle obj;

obj.setdata();

obj.calculatearea();

}

It can be noticed here that there are two ways to set valued to the member function. The first is through the program where a setdata function helps to assign values to variables. In the second case the values are taken from the user.

Constructors:

Constructors are special member functions and they are used for automatically assigning values to member functions. Constructors are executed whenever an object of the class is created. Another feature of constructor is they do not have any return type.

class counter

{

private:

int count;

public:

counter(): count(0)

{

}

void inc_count()

{

count++;

}
int getcount()

{

return count;

}

};

void main()

{
clrscr();

counter c1,c2;

cout<<“through object1″<<c1.getcount();

cout<<“through object2″<<c2.getcount();

c1.inc_count();

c2.inc_count();

c2.inc_count();

cout<<“display again count”<<c1.getcount();

cout<<“display again count”<<c2.getcount();

getch();

}

in the above example the variable count has been assigned a value ‘0’ through the constructor. If there are multiple variables or data members of a class then they can be assigned values directly as:

someclass(): value1(2), value2(56), value3(23) {}

Practice Questions

1.Write a program to show that for each object constructor is called separately.

Solution:

class testrun

{

public:

testrun()

{

count<<“constructor is called for each object individually”<<endl;

}

};

void main()

{

clrscr();

testrun obj1, obj2,obj3;

getch():

In the above program it can be noticed that constructor is called for these three objects separately.

Write a program to declare default arguments in a constructor. Obtain power of the number.

class power

{

private:

int pow;

int num;

int ans;

public:

power(int n=9,p=4);

void show()

{

cout<<“number raise to power is”<<ans<<endl;

}

};

power::power(int n, int p)

{

num=n;

pow=p;

ans=pow(n,p);

}

void main()

{

clrscr();

class power p1,p2(6);

p1.show();

p2.show();

getch();

}

also read here

2 thoughts on “How do you use classes and objects in C++ programming?”

Leave a Reply

Your email address will not be published. Required fields are marked *