Back Button
Back to Chapter
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.
Collapse_Icon
Table of Contents

Classes

We all know that Charles Darwin classified all living creatures into animal kingdom and plant kingdom.

His first created a class which had properties such as green pigmentation, ability to create its own food, etc.  All the creatures which showed these features were classified as plantae or plant kingdom.

He created another class with properties such as locomotion, dependence on other creatures for food, etc. He put these creatures into the animal kingdom.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ffcaf9b4-6c09-4d19-83ef-dbbaa874a6ac/Untitled_Diagram_(53).png

Humans tend to classify things that have similar properties together, therefore it becomes necessary for us to include a method by which we can classify our C++ program into groups of objects with similar properties.

In C++ we use classes to define a set of properties for objects using data members and member functions, all objects belonging to this class will possess these properties.

Keyword class is used to declare a class.

Syntax:

class className{ int a;//class members; };//semicolon is necessary

In C++ classes are used in object-oriented programming paradigm, as discussed earlier in object-oriented programming emphasis is on the objects that developers want to manipulate rather than the logic required to manipulate them.

In the OOP concepts, we learned 5 concepts that are essential for OO programming, now we will see how these concepts are implemented using classes and objects.

Encapsulation

We already know what encapsulation is now let us look into an example to make this concept even more clear.

#include using namespace std; class EncapsulationEx { private: // we declare a as private to hide it from outside int a; public: // set() function to set the value of a void set(int a1) { a = a1; } // get() function to return the value of a int get() { return a; } }; // main function int main() { EncapsulationEx e;//declaration of object e.set(20); cout<
Output: 20

In the above program, the variable a is made private so that this variable can be accessed and manipulated only by using the methods get() and set() that are within the class. Therefore we can say that the variable a and the methods set() as well as get() have bound together this binding is known as encapsulation.

While dealing with classes we makes use of set() to set the value of a data member in a class and get() to a access the value of this variable. You can use other function names to but it is conventional to use "set" and "get".

Data Abstraction

In OOPs, Abstraction is the method of getting information. The information needed will be taken in such a simple way that only the required components are extracted, and the ones that are considered less significant are unnoticed.

Consider the example given below to add two numbers and display the sum.

#include using namespace std; class Summation { private: // private variables int x, y, sum; public: void sum(int a, int b) { x= a; y = b; sum = a + b; cout<<"Sum of the two number is : "<
Output: Sum of the two number is : 9

Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions define the attributes and behavior of the objects in a Class.

In short a class a user defined data-type which has data members and member functions.

In this example, we can see that abstraction is achieved by using class. The class 'Summation' holds the private members a, b, and c, which are only accessible by the member functions of that class. Hence we make use of the public member function "sum" in order to access these variables

There are two ways in which we can define member functions of a class.

The first way is as shown in the example above inside the class.

the second method is to define a member function outside the class definition using the scope resolution :: operator along with class name and function name.

#include using namespace std; class num{ private: int a; public: void get() { return a; } } void num::set(int x)//defining a member function outside the class { a=x; } main() { int k=10; num c; c.set(k); cout<<"number:"<
Output: number:10
Note:  All the member functions defined inside the class definition are by default inline, but you can also make any non-class function inline by using keyword inline with them. Inline functions are which are copied everywhere during compilation, so the overhead of function calls is reduced.

Objects

An object is as the name suggests an object much like a real-world object which has shape size and occupies space, the object in C++ also has fixed size and has space in the memory of the computer.

It is important to understand that only an object has memory whereas the class which we create consumes no memory at all. Suppose you wish to make a car, the first thing you would do is make a blueprint a class basically is a blueprint or design based on which we create objects and use them to write our code.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/240c6321-b7f6-4dad-99b0-0637f408d82f/Untitled_Diagram_(55).png

Declaring an object.

className objectName;

Accessing a member of the class

objectName.memberName;

Suppose we are writing a code for a game where we have a protagonist and multiple enemies.

We would create two classes "class hero" and "class enemy", let us see what this would look like.

#include using namespace std; class hero{ private: int health; int attack; int defence; public: void set(int h,int a,int d) { health=h; attack=a; defence=d; } void display() { cout<<"health:"<
Output: HERO health:100 attack:90 defence:95 ENEMY health:100 attack:60 defence:50 destruction power:100