C++ Classes and Objects

Derya Cortuk
4 min readMar 23, 2020

--

C++ Class

A class in C++ is a user defined data type or data structure, which contains data members and member functions. A class is a blueprint of data that defines the characteristics, which can be accessed and used by creating an instance of that class called “object”.

Member functions identify the operations on data members that are the data variables. In other words, member functions are the functions used to manipulate these variables and together these data members and define the properties and behavior of the objects in a class.

When a class is defined, no memory is allocated.

A class is defined using keyword class followed by the class name; and class body, enclosed by a pair of curly braces.

Access Modifiers in C++

Access Modifiers or Access Specifiers in a class are used to set accessibility of the class members. There are 3 types of access modifiers: public,private and protected. By default access to members of a C++ class is private.

1.Public: All the class members declared under public will be accessible from anywhere in the program using the direct member access operator(.) with the object of that class.

2.Private: Private members of a class can be accessed only by the functions inside the class, which facilitates data hiding. Namely, the private members are not accessible outside the class; they can be accessed only through methods of the class.

3.Protected: Protected access modifiers are accessible from other members of the same class, are similar to that of private access modifier, but they can be accessed by any subclass(derived class) of that class.

C++ Objects

A class provides a blueprint or a template. When a class is defined, a class does not occupy any physical space in computer memory until an object of that class type is defined. If we use the data and access functions defined in the class, we need to create objects.

Accessing data members and member functions: The data members and member functions of class can be accessed using the dot(‘.’)operator with the object.

Member Functions in Classes

Member functions are the functions, which have their declaration inside the class definition or outside the class definition. If we define a member function outside the class definition, we have to use the scope resolution :: operator along with class name and function name.

Constructors

A constructor has same name as the class and may be defined inside or outside the class definition , which is a special member function of a class that is called by the compiler every time an object of that class is instantiated.

There are 3 types of constructors:

  1. Default constructors
  2. Parameterized constructors
  3. Copy constructors

Destructors

A destructor is a special member function of a class which is called by the compiler when the scope of the object ends.A destructor has exact same name as the class prefixed with a tilde( ~) and it can neither return a value nor can it take any parameters.

--

--