Class in C++ | Object in C++

Here is the following concept of class and object in C++,

class in C++

as already mentioned in the OOPs page. class is a, collection of data members and function members. These members of the class are declared in some access specifier or mode in the class.

Members of a class are made accessible using the access specifier, so that a class can be implemented.

Thus by defining access mode in a class, the syntex of a class would be as follows –


class class_name
      data member;
      member function;
     data member;
     member function;

Here, data member representing the data type (int, char, float) while member function is representing the function. It has a class-name an identifier i.e. any appropriate name (except reserved keyword).

before starting programming with class, we will discuss about these access specifier or visibility-mode.


Type of access mode/access specifier in C++

  • private
  • public
  • protected


private mode/private member

When a member of a class is declared in private mode, it can only be accessed by other members of the same class. i.e.They cannot be accessed from outside the class. All members of a class are by default private i.e. private keyword is an optional.

If no mode is defined in a class, then all the members of the class will be private. means 


public mode/public member

In this mode, the declared data members and function members can be accessed outside the class from its object (class-variable).Because all members of a class are default private. Therefore, we use public keywords to make members public.

apart from two access modes, a third access mode is also found in the class which is used to inherit the property and behavior of a class in another class. This is called protected mode.


protected mode/protected member

This mode of declaration of members is the same as private, that is, the members declaring in both modes can not be accessed from the class object.

Protected members can be accessed by inheriting another class (feature of inheritance) while private-members can access only members of the same class.

we use the protected keyword to protect members of a class.

We will read about protected mode in inheritance. Here we will discuss private and public members.

Thus a class cannot implement a class without defining access mode, i.e. using or not treating the class as both are equal.


Defining of a class members in C++

The private member of a class is defined within the class itself, whereas the public member can be defined in two ways –

  • inside class (inline definition)
  • outside class (outline definition)


public member definition inside the class (inline definition)

In this, without giving the function-declaration of members, direct given its definition inside the class.


class class_name

   return_type function_name()
     body of function;


Public member definition outside the class (outline definition)

In this, the declaration of function-members is inside the class while their definition is defined outside the class by scope resolution operator.


class class_name
   return_type function_name();

class_name :: return_type function_name()
     body of function;

Here, :: is a scope resolution operator. Which we use for outside definition of public members of a class.

In the next program, we have defined the definition of function-member using both methods(inside-definition and outside-definition). Both of these examples are given in the below accessing class member using their object.

Declaration of an object in C++

We already mentioned object in OOPS in C++ Page, Here we learn to declaring an object,

Just as the member of the structure is accessed from the variable of the structure, the member of the class is accessed from the class variable or object as well.

An object is an identifier of any appropriate name (except reserved keyword). A class can have more than one object but with a different name.


class_name object;


class-name object1, object2,.....n;


class class-name
} object1, object2, …..n;


class Student
     int roll; char name[10];

Here, x and y are object of student class.

accessing class member using their object

only public members of the class can be accessed. Hence-

using first object x,


using second object y,


You can understand this from the program given below-

In the program, we have done two public function-member declarations. The first public-member get_record (); definition of is given inside the class while the second public-member show_record (); definition is given outside the class (by scope resolution operator).


class student
     // public member by default
         int roll_no;
         char name[20];

      public:       // public member declaration
          void show_record(void);          // function declaration
          void get_record()             // function defination inside class (inline definition)
               cout<<"Enter roll no: ";
               cout<<"Enter Name   : ";

         void student::show_record()    // function defination outside class (outline definition)
               cout<<"\nRoll no: "<<roll_no;
               cout<<"\nName   : "<<name;

 student x,y;     // object declaration

 void main()    // main function
        x.get_record();   //first object's function call
        y.get_record();   //second object's first function call

        x.show_record();  //first object's second function call
        y.show_record();  //second object's second function call
Enter roll no: 11
Enter Name : Rahul sherma

Roll no: 11
Name : Rahul sherma

as you can see, we have stored the record of two students from this program.

As mentioned earlier, the concept of class in C++ is the  updated version of the structure concept of C. But How?,

Difference between class and structure in C++

All members of the structure are declared publicly by default, whereas members of a class by default are private.

All member of structure is accessed by structure-variable whereas only public member in class can be accessed by class-variable.

Class OOPs are the feature while the structure is taken from the process oriented programming (POPs).

Similarities between class and structure in C++

Like structure, class name is also an identifier ie any unique name.

both have members, data type and function type.

both do not reserve space in memory until their variable or object declare.

just as a member of a structure is accessed by a structure-variable, the class member (public member) is accessed from the class-variable or object.

members of both class and structure are declared within a bracket and terminated with bracket semicolon.

more about class and object,

previous-Object Oriented Programming in C++

next- Friend function in C++