C++ Classes and Objects3 min read

C++ is an Object-Oriented Programming Language and classes and objects are the fundamental components of OOP’s. Everything in C++ is linked with classes and objects. The main purpose of C++ is to introduce the OOPs concept in the C programming language which itself is a powerful language.

Objects are real-life entity and classes are the blueprint for creating an object. For example: consider a class for a car, it has color, some weight, size, etc, these are its attributes and drive, gear, brake, etc are its behaviour.

These attributes and behaviour in a program are variables and functions in a class, also called the class members.

C++ Class

A class is a user-defined blueprint or prototype for creating Objects. It is a logical entity. We can say that classes are categories, and objects are items within each of those categories.

It contains fields, methods, constructors, etc. These members of a class can be accessed and used by creating an instance of that class.

Defining Class:

The definition of the class starts with the keyword class followed by a class name. The body of the class is enclosed within the curly braces and ends with a semicolon. All the members of the class are declared within the body of the class as shown below.

Syntax for class:

The class keyword is used to create a class followed by a class name ‘Rectangle‘. the public is one of the access specifiers that are used with the class members to determine their scope that is their visibility outside of the class. There are two public variables length and breadth (attributes). And at the end of braces, a semicolon is provided to end the class.

C++ Objects

Objects are real-world entities that have states and behavior. Example. bike, Lamp, pen, table, etc. An object is an instance of a class. It can be physical and logical.

All the class members are accessed by creating an object of that class. Objects are created at runtime so is called runtime entity.

  • State: It is represented by attributes of an object that is the data value.
  • Behaviour: It represents the functionality that is the behaviour of an object. Example withdraw, deposit, etc.

Here the type is of Rectangle type rect is a reference variable that refers to the instance of a Rectangle class.

Let us understand class and objects in C++ through an example.

Example of C++ Class and Object


Steve Rogers

Example: C++ multiple objects with methods


BMW 1999
Ford 1969

We created two objects (obj1, obj2) and call the methods present in the Car class with two separate objects by passing different values.


Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …