C++ Tutorial – Class Basics

C++ programming supports Object-Oriented Programming (OOP). The OOP concepts are implemented using classes in C++. Before we start learning about C++ class basics, understand the following OOP concepts given below.

Data Abstraction

C++ classes are abstract data-types (ADTs). The classes use data abstraction which is collection of data and methods in OOP. The term abstraction means showing only the essential features without sharing the implementation details. The internal details of a class is hidden to other classes.

Data Hiding or Information Hiding

C++ hiding details of a class and only allows member functions to access the data for protection. No other class or function can access the information from outside the class.

This is achieved using visibility labels – private, public and protected.

Data Encapsulation

The data and functions of a class is neatly encapsulated into a package so that information is hidden (data hiding) and no one can access the information outside of class.


The term “inheritance” means object of one class can inherit properties and methods of another class object.

The class whose properties and method are inherited is called the Base or Parent class.

The class which inherits the properties and methods is called the derived or child class. Inheritance creates a hierarchy of classes. The methods of child classes are reused many times because the derived class use them, otherwise, these methods should be written for each child class separately.

Code re-usability is the advantage of using inherited classes.


Sometimes, a function of class takes many form but retain the same name. The process of polymorphism is known as function overloading in C++.

For example,

void fly ();
int fly (int);
float fly (int, int, float);

Message Passing

The message passing enables communication of information among objects. A request to run a method of an object is the message to that object.

You need three components for message passing in a c++ class.

  1. An object
  2. A method of the object (message)
  3. A parameter (information to be passed)

For example

Obj.fly (eagle);


“Obj” is the object and “fly ()” is a message. The argument “eagle” is an information that is passed in the message.

Class Declaration Example

A c++ class is encapsulation of data and its methods to prevent access from outside the class.

To declare a class

  • Use keyword class
  • The variables and methods inside a class are called class members. A variable inside class is called a data member and a method is called a member function.
  • The visibility of member is controlled by visibility labelsprivate, public and protected.

For example

class car {
char model;
char type;
float price;
void speed();


Diagram - Class Members in C++
Diagram – Class Members in C++

Visibility Labels

The visibility labels prevent the access of class members from outside the class. There are three types of visibility labels defined inside a c++ class.

  1. Public
  2. Private
  3. Protected
Diagram - Class Visibility Labels in C++
Diagram – Class Visibility Labels in C++


  1. The public members can be accessed by any function outside the class. The public member functions are also called the interface functions because they interact with other classes.
  2. The member functions can be defined inline (inside the class) or outside the class.


The main characteristics of private members are following.

  1. Private members cannot be accessed outside the class. Only member functions from same class can access private class members.
  2. When no visibility is defined then all class members are private and cannot be accessed outside the class.
  3. There is a special function called friend functions that can access private members.


The protected label have some characteristics of private label.

  1. The protected members can be accessed by member functions and friend functions. Also, access by member functions of derived classes and friend functions of derived classes.
  2. Cannot be accessed outside of class.


Balagurusamy. 2008. Object Oriented Programming With C++. Tata McGraw-Hill Education.

Ravichandran. 2011. Programming with C++. Tata McGraw-Hill Education.