C++ Classes and Objects

In this tutorial, you will learn about core feature of C++ called ‘C++ classes and objects‘ for which C++ was called ‘C with class’.

C++ Class: Introduction
Declaration and defining class in C++
Access Specifiers
C++ Object: Introduction
C++ Object: Syntax
C++ Class and Object: Example

c++ classes and objects

C++ programming classes and objects


Similar to the structure in C programming, c++ class is also user-defined data types which allow data binding of different types and has its own data members and member functions.

Member functions define the operations on data members. And to access these data members and functions we need to create an instance of the class called ‘object’.

To put it simply a class is a blueprint of a data that defines the characteristics and behavior of its data members and member functions.

Example of c++ classes and objects

A class of Vehicle can have different data members like cars, buses, bikes, and others. It can define characteristics of vehicles like gear, tyres,  seats, colors, and others and behavior like driving.

All in all, it can also be said that a class is a way to bind the data and its associated functions together in a single unit. This process is called Encapsulation.

Declaring and defining class in C++


In C++ a class is defined by using the keyword class followed by the class name.

class class_name
{
   private:
     variable declaration;
     function declaration;
   public:
     variable declaration;
     function declaration;
 };

After the declaration of the class name is the body of class which is enclosed within the curly braces.

The body of class contains the declaration of variables and functions which are collectively called class members.

This binding of member data and member functions within a single unit is called encapsulation.

[adsense1]

For example:

class student
{
   private:
     int roll_number;
   public:
     char name[10];
     int func1()
     {
         roll_number = 2;
         return roll_number;
     }
 };

As seen in above example roll_number and name are data members and func1() is the member function of class name student.

The class members are grouped into sections private, protected and public. These keywords are called access specifiers which define the accessibility or visibility level of class members.

By default the class members are private. So if the visibility labels are missing then by default all the class members are private.

Private, Public and Protected Access Specifiers


The class members that have been declared as private can be accessed only from within the class. This facilitates data hiding which is also one of the key features of object-oriented programming.

The class members that has been declared as protected are accessible within the class and also in the derived class. Public members can be accessed from outside of class as well.

Click here to know in details about access specifiers and how they are used for data hiding

C++ Objects


When we define a class we define just a blueprint or we just map data members and member functions.

A class doesn’t occupy any physical space in computer memory until an object of that class type is defined.

The declaration of class develops a template but data members cannot be manipulated unless the object of its type is created.

An object is an instance of the class. Creating an object is like defining a variable of class type.

Syntax to declare object

class_name object_name;

Basically, an object is variable of datatype class name which holds every data and functions defined inside that class.

For example (from above example)

student x;

This object x will hold all the class members within class student.

How to access data members and member functions?

In above example, class student holds data member roll_number and member function func1.

These class members can be accessed through object in following way.

x.name; //accessing public data member
x.func1(); //accessing member functions

The class members can be accessed by using dot (.) operator. The dot (.) operator is called member access operator.

Example elaborating concept of object, class and class members

//C++ program to demonstrate the use of object and class
#include <iostream>
using namespace std;
class student
{
   private:
     int roll_number;
   public:
     char name[10];
     int func1()
     {
         roll_number = 2;
         return roll_number;
     }
};
int main()
{
    student s1; //creating object s1 of class type student
    cout << s1.func1();
    return 0;
}

Output

cplusplus class and object example

In this program object s1 of class type student is created, which then calls the member function func1().

Note: The private data member roll_number can only be used within the class.