Classes and Objects explanation with simple examples

The main motto of evolution of  C++ programming was  to add object orientation to the C programming language that was missing in traditional C programming language. Classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types.

• A class is

An abstract data type similar to a ‘C’ structure.

A representation of objects and the sets of operations that can be applied to such objects.

Class consists of Data members and methods.
Data Members : Primary purpose of a class is to held data/information. This is achieved with
attributes which is also know as data members.

Member Functions: The member functions determine the behavior of the class i.e. provide
definition for supporting various operations on data held in form of an

General syntax of class
Class class_name
Data Members;


When you define a class, you define a blueprint for a data type. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations


 C++ Objects:

A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types.

• Object is an instance of a class.
• An object has state, behavior and identity; the structure and behavior of similar objects are defined in their common class; the term instance and object are interchangeable.

General syntax for defining an object:

Just as we declare a variable of data type int as:

int x;

object will be created as:

Class_name object name ;

How to use Member Function?

A mentioned earlier, The member functions determine the behavior of the class.

Member function can be accessed as


If we want to pass any arguments in the function then

Object_name.function_name(list of arguments);
Access specifiers:

Access specifiers are used to identify access rights for the data and member functions of the class. There are three main types of access specifiers in C++ programming language:

  • private
  • public
  • protected
  • A private member within a class denotes that only members of the same class have accessibility. The private member is inaccessible from outside the class.
  • Public members are accessible from outside the class.
  • A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class.

When defining access specifiers, the programmer must use the keywords: private, public or protected when needed, followed by a semicolon and then define the data and member functions under it.


• Drawback of classes with all public data members:
– No protection against its members
– The Object can be misused by the programmer.
– Against the OO philosophy

Mohit Arora
Follow me