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:

Explanation:
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

Output:

32
Steve Rogers


Example: C++ multiple objects with methods

Output:

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.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More