What is Class?


A class is a user defined data type that allows us to bind data and its associated functions together as a single unit. Thus class provides the facility of data encapsulation.
Class provides the facility of data hiding using the concept of visibility mode such as public, private and protected.
Once a class is defined we can create an object of the class to access variables and functions defined inside the class.

Syntax:
class Class_Name
{
Private:
Data-Type Variable_Name;
Function declaration or Function Definition;
Public:
Data-Type Variable_Name;
Function declaration or Function Definition;
};

     

Class can be created using the class keyword. The class definition starts with curly bracket and ends with curly bracket followed by semicolon.
We can declare variables as well as functions inside the curly bracket as shown in the syntax. The variables defined inside class are known as data member and the function declared inside the class are known as member function.
In order to provide data hiding facility class provides the concept of visibility mode such as private, public or protected. If you don’t specify any visibility mode for the member of the class then by default all the members of the class are considered as private.
The data member and member function declared as a public can be accessed directly using the object of the class. But the data member and member function declared as private can not be accessed directly using the object of the class.

Example:
class test
{
int a, b;
public:
void input ();
{
cout<<"Enter Value of a and b";
cin>>a>>b;
}
void output ()
{
cout<<"A="<<a<<endl<<"B="<<b;
}
};

Download Projects


Download Programs