C++ Classes and objects
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.
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:
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.
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.
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.
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.
Declaring an object.
Accessing a member of the class
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.