classes in c++

Classes In C++ -:  A class is a way to bind the data and its associated function together. It allows the data to be hidden. If necessary from external use. When defining a class we are creating a new abstract data type that we can be treated like any other built in data type. Normally class has two types.

  1. Class Declaration -:The class declaration describe the type and the scope of its member
  2. Class Function Definition -:The class function definition describe how the class function are implemented.

Class Declaration -:The class declaration describe the type and the scope of its member The example of class declaration is here

Class classname 
          variable declaration ;
          function declaration ;
          variable declaration ;
          function declaration ;


The class declaration is similar to a  declaration.The keyword class specifies that what follows is an abstract data of type class name.The body of a class is enclosed within braces and terminated by a semicolon. The class body contain the declaration of variable and function. These function and variable are collectively called class member.

They are usually grouped under two specify category these are the

  • Private class -This accessed within the class.
  • Public class-:It accessed outside the class.


The variable declared inside the class are known as data member and the function are known as member function. Only the member function can have access to the private class data member and private function

Example of simple class has been given here…

class item

          int number;          //variable declaration
          float cost;          //private by default


          void getdata( int x, float y);        //function declaration 
           void putdata (void);                 // using prototyping


Creating Object -:  Once a class has been declared  we can create a variable of that type by using the name like any other built variable type as like

item x;   // memory of x is created


Accessing Class member- :  After defining the class the second steps comes how to access class member in c++  programming language  The private data of a class can be accessed only through the member function of that class. The Main() function contain statement that access number and cost directly. Syntax of accessing class member as like here

object name, function name(actual arguments);


Defining Member Function -: Member function are defined in two types

  1.  Outside the class definition
  2. Inside the class definition

Both the function has major difference for processing the defining the header function .

Outside the class definition -: Member function that are declared inside a class have to be defined separately outside the class The definition are very much like the normal function.They should have a function header and a function body. Since c++ does not support the old version of function definition.

An important difference between a member function and a normal function is that a member function incorporates a membership identify label in the header. This label tells the compiler which class the function belong to for like this

return type class name:: function name (arguments declaration )
   function body


Leave a Comment