Classes and Objects in C++

05 min read

Classes:

Object-oriented programming is centered around objects, which bind together both the data and the functions that operate on the data. This helps in data protection ie., the data belonging to any particular object cannot even be accidentally altered from outside that object. For performing this data encapsulation, we make use of classes which are abstract data types (ie., the programmer can decide what values are acceptable for these data types and what operations can be performed on these data types). Thus a class is a user-defined data type consisting both variables (for holding the data) and functions (for accessing those data).

The variables inside a class are called as data members and functions are called as members functions.

Syntax for class declaration

class className
{
Access_specifier :
Data Member1;
Data Member2;
.......
Access_specifier :
Member Function1( );
Member Function2( );
........
};

 

Within this class, the variable and functions which we wish to include can be specified. For this example, class Rectangle, we may wish to have the 2 variables 'length' and 'breadth'. So, declare them inside the class. Also, assume we wish to have the following 2 functions inside the class.

void getdata( )  //to read the length and breadth values

void area( )     // to compute the area of the rectangle and display it

Now, we have planned to have 2 variables and 2 functions within the class. 

class Rectangle
{
int length;
int breadth;
void getdata( );
void area( );
};

 

Objects:

So far, we have described what this class 'Rectangle' looks like. This class declaration does not allocate memory for the class. It is only when we create objects for this class, the memory will get allocated for these objects.

Variables which we declare for primitive data types like int, float, etc holds only data whereas variables declared for the class type should hold both data and functions. That's why they are called 'objects' instead of variables. Declaring an object for the class is called class instantiation. 

Syntax for object declaration:

className  objectName;     //Ex: Rectangle r;

Accessing the class members

  • The dot operator is used to access the members of the class.
  • Accessing the data members of a class

         objectName  . MemberName

  • Accessing the member functions of the class.

         objectName . FunctionName (Actual Arguments)

 

The members of the class Rectangle can be accessed only by using this object name 'r'

Example:   r.getdata( ) or r.area( ) or r.length;      

Assume the main( ) function has to get length and breadth values of this class as inputs and display the area of the rectangle

class Rectangle
{
int length;   
int breadth;                              
public:
void getdata( );
void area( );
};

int main( )
{
Rectangle r;        // object 'r' is created for the class. Memory is allocated for 'length' & 'breadth' of this 'r' object
}

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error