C++ Classes and Object

What is a Class?

A class is a user-defined data type. It is a logical abstraction and a template that defines the form of an object. A class specifies both code and data. It is not until an object of that class has been created that a physical representation of that class exists in memory. When you define a class, you declare the data that it contains and the code that operates on that data. Data is contained in instance variables defined by the class known as data members, and code is contained in functions known as member functions. The code and data that constitute a class are called members of the class.

Keywords public, protected and private is access specifiers. Note that storage is not allocated until an instance of the class is created.

Access specifiers/Visibility modes

There are three type of Access specifiers:

  • Public: When deriving a class from a public base class. Then the public members of the base. class become public members of the derived class.
  • Private: When deriving from a protected base class. Then both public member and protected members of the base class will become protected in the derived class.
  • Protected: When deriving from a private base class.Then both public member and protected members of the base class will become Private in the derived class.

What is Object?

Object is an instance of a class, which hold the data of the variable declared in the class. Each object contains data, and code to manipulate data. Objects can interact without having to know details of each other’s data or code. It is sufficient to know the type of message accepted, and the type of response returned by the objects. Objects take up space in the memory and have an associated address like a record in Pascal, or a structure in c.

Structure of C++ program with class and object

In this example, obj is the class object of the demo is created.
In the above class demo, x and y are data members and details() and display() are member functions.

Data Members and Member functions in Classes

Data Members

An object may contain values which are stored internally and are unique to that object. In order to do this, each value needs an appropriate declaration as a data member in the class. A data member may be of any type, including classes already defined, pointers to objects of any type, or even references to objects of any type. Data members may be private or public but are usually held private so that values may only be changed at the discretion of the class function members.

Members functions

A member function can be defined in 2 types.

  • Inside the class: In this method, the function is defined within the class body and are treated as inline by default.
  • Outside the class: In this way function prototype is declared within the class body and function is defined outside the class with the help of Scope Resolution operator (::).


A constructor is a special member function with the same name as its class name and is used to initialize the data members of the class. A constructor is invoked automatically when an object of a class is created. A constructor does not return any value not even void. A constructor must be defined in the public section.

Type of Constructor

  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor
  • Default Argument Constructor
  • Dynamic constructor

Default Constructor

A default constructor is a constructor that either has no parameters or if it has parameters, all the parameters have default values.

Parameterized Constructor

A constructor where the parameter is passed. The argument of the constructor can be of any type except that of the class to which it belongs.

Copy Constructors

A constructor that accepts a reference to an instance of its own class as an argument is called Copy Constructor.