The most important feature of C++ is class. It is a new of representing a user defined data type.Before introducing to the concept of class, we revise the concept of structures.Structure is a user defined data types which is used to pack together different types of data types.Once structure is created, we can declare its variables.The limitation of structures is that they do not permit data hiding. Structure members can be accessed by any function in the scope.
In C++, a class can have both data members and member functions.It also permits data hiding by making them private.

NOTE: In structures, by default the members are public and in class,by default members are private.


A class can be specified in two parts:

  1. Class Declaration
  2. Class Function Defination

1.The general form of class declaration is:
class class_name { private: variable declarations; function declarations; public: variable declarations; function declarations; };
1. The keyword class specifies that it is a class. The keywords private and public are the visbility modes.
2. The class members that are declared in private section can be accessed only within the class.
3.The public members can be accessed outside the class also.
Consider a simple class example:
class book { int number; //variable declaration float cost; public: void getdata(int a,float b); //function declaration void putdata(); };

Creating Objects

Once a class has been declared, we can create objects of that class type.For eg. book x;. The necessary memory space is allocated to the object at this stage.

Accessing Class Members

Private data of the class cannot be accesses directly ouside the class. They can be accessed using member functions.The syntax of calling a member function is:
object_name.function_name(actual arguements);
For eg. x.getdata(3,20);. Similarly we can access data members using dot(.) operator.Public data members can be accessed directly whereas, private are accessed through member functions.


Member function can be defined in two places:

1.Ouside the class: Outside the class, member functions are defined like normal functions, but there is a label that tells the compiler to which class it belongs to. The general form of member function defination is:
return-type class-name::function-name(arguement declaration)
 	function body
2. Inside the class: Function can also be defined inside the class. When a function is defined inside the class, it is treated like as an inline function.Therefore, all the restrictions of inline function are also applicable here.For eg:
class book
 	int number; float cost;
 	public:  void getdata(int a,float b)
 				number=a; cost=b;
 			void putdata()
 				cout<< number<<"\n"<< cost;


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 functions.


We have stated that memory space is allocated for an object when it is declared and not when a class is specified.The statement is partially correct. The member functions are created and placed in the memory space only once when they are defined as a part of class specification.All the objects belonging to that class use the same member functions.Only space for member variables is allocated separately for each object.


A data member of a class can be declared as static.It has the following properties:

Static members are commonly used to maintain values common to the entire class.The type and scope of the static variable is defined outside the class.This is necessary as they are stored separately.


A member function declared as static has the following properties:


Like any other datatype objects can also be passed as function arguements.This can be done in two ways: 1) passing object by value, 2) passing object by reference.When the object is passed by call by value, the changes made to data member in the function are not reflected in the objects. In call by reference, changes are reflected in actual objects.An object can also be passed as an arguement to a non-member function.