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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More