A class is a group of objects that have common properties and common behavior or
functions.
For example consider an object- you. You have certain properties like name, age,
date of birth, gender etc. and perform certain operations like move, listen, speak,
sit etc. Similarly your friends Gurpreet, Rohit, Saurav, Amit etc. also possess
certain properties like name, age, date of birth, gender etc. and perform certain
operations like move, listen, speak, sit etc. All these people represent different
objects having same properties and same functions. So these all objects can be grouped
together to form a class called ‘Person’.
Thus a class is a collection of similar type of objects and an object is an instance
of class. The properties of the objects are represented as data members and the
functions as member functions.
The members of class can be any of the following 3 types:
(a) Private- Accessible only within the class and by the friend functions of the
class. They can not be accessed by the object of the class.
(b) Public- Accessible anywhere, inside or outside the class. They can be accessed
outside the class using the object of the class.
(c) Protected- Accessible within the class, by the derived class (in case of inheritance)
and by the friend functions of the class. They can not be accessed using the object
of the class. Its main use is in the case of inheritance only.
In C++ a class can be defined as under;
Syntax:
class class_name
{
private:
data members;
member functions;
protected:
data members;
member functions;
public:
data members;
member functions;
};
Example:
class person
{
private:
char name[30];
int age;
char gender;
public:
void input_data( );
void output_data( );
};
__________
__________
person p1, p2; //Object creation
Here person is a class that has two objects p1 and p2 that share common properties
of name, age and gender and common functions input_data( ) and output_data( ).