Techstructive Blog

Techstructive Blog

Object-Oriented Programming in C++ :  Classes and Objects

Object-Oriented Programming in C++ : Classes and Objects

Understanding the OOP concept and some basics semantics in C++

Subscribe to my newsletter and never miss my upcoming articles

Introduction

We all know that C++ is famously known for Object-Oriented Porgramming, but what is Object-Oriented Programming? Well, this is the article, where we will explore the basics of Object-Oriented Programming, its building blocks i.e Classes and Objects, and get familiar with its basic semantics in C++. Let's get into it straight away!

What is Object-Oriented Programming?

Object-Oriented Programming(OOP) is a way to structure and design a program or an application. Object-Oriented Programming deals with creating classes and objects and it's related terminologies. OOP helps in following the principles like DRY(Don't Repeat Yourself) and KISS(Keep it Simple Stupid). Using OOP certain applications and problems can be solved in a simple and efficient way. It helps in better understanding and readability of the code.

Virus is a class and COIVD-19 is an object of the Virus class XD

In OOP we create a collection of the data and functionalities by organizing in a structure called Class. We then instantiate or create the actual data by creating an object of the particular class. Once we have created a class, we can simply create as many objects as we need to. Objects are basically the storage of the actual data in memory. Let's explore them in the next few sections.

NOTE: There are a lot of terminologies in OOP, just remember the basics of those terms and not the different names of those terms.

What are Classes

Classes are the structure or template of the data and its associated behavior. The data or the variables in the classes are called properties or attributes(also data members). We also have functions that define the behavior of the properties or the data present in the class and are called methods.

A Class in C++ should be defined outside of the main function. In C++, class is a reserved word and hence it is used to declare and define a class. It has a general structure like:

class Name
{
    // properties and methods
};

It's a convention to use the initial letter of a Class Name Uppercased. We can define class methods outside the class body as well, we will see it in the next few parts of the series.

Let's create a basic class in C++,

class Animal
{
    public:
        int legs;
        string name;
    void print()
    {
        cout<<name<<" has "<<legs<<" legs.\n";
    }
};

Here in the above code, we have a class called Animal and it has 2 properties called legs which is an int and name a string. We also have a method called print which displays the current object's name and the leg count. We will see what is the current object in the next section.

oopcpp1-class.png

We use those properties or the variables defined in the class in our application either by accessing them through the objects or in the methods of that class. The variables or properties in classes are not assigned to any memory address i.e. they are just kind of a blueprint or placeholder tags to match for the data.

Access Specifier

We have written public before the definition of the properties of the class, it is an access modifier. Don't worry we will see it in detail in the next few parts of the series, just for time being understand that we can change whether we want the main function(or any function globally) to access the class' properties or not.

We are saying public which means we can access the properties of this class anywhere outside the class. This is not a good practice but just for simplicity of understanding, we are using public. If you do not specify anything like public, it is by default private, which means the properties of the class are not accessed anywhere outside the class. Let keep it simple right now.

We also have other concepts like defining the methods outside/inside the class, header files, constructors, destructors, and many others related to a class definition, we will cover it in the next few parts.

What are Objects

Now, we have created a class but where is the data actually? It will be in objects, We actually assign the data or properties to a memory address by creating the objects of that particular class.

We can create objects of a particular class in C++ by writing the name of the class that this object will belong to and the name of the object,classname objectname;. This will create or assign memory to the properties of the class to the object.

After the object has been created, we can assign the value to the properties of the class in the object. We can access the properties of the class by referencing the name of the object with . and the name of the property or variable of the class, objectname.propertyname, we can assign the value simply by using the assignment operator = and give the properties the value, input from the user or however you like it.

#include<iostream>
using namespace std;

class Animal
{
    public:
        int legs;
        string name;
    void print()
    {
        cout<<name<<" has "<<legs<<" legs.\n";
    }
};

int main(){

    Animal dog;            // create an object of class Animal

    dog.name="Floyd";     // assign the class property to actual data in memory
    dog.legs=4;                 
    dog.print();          // call a method(function) associated to object's class

    return 0;
}

image.png

If Animal is the class, then the dog here is the object. As simple as you can think. We are technically instantiating an object when we say Animal dog. We assign the variables the memory as defined in the class. We give some value to the properties using the .(dot syntax), after the class name.

So dog.legs = 4; will assign the value of 4 in the memory address of the object's property(dog is the class and legs is the property).

Similarly, we can call the function associated with the class as we do for properties but we use () to indicate to calling the function.

oopcpp1-obj.png

So that is how we create objects in C++.

Why and When to use OOP

Object-Oriented programming makes quite complex problems and applications structure and scale quite easily and enhances the readability of the codebase. We can use OOP to create the applications for bigger and real-life applications, it allows us to add new features quite easily and thereby improving the maintaining ability.

The following might be the reasons to use OOP in an application:

  • If the application cannot be stated in a single sentence, you need to create the components into classes and hence use OOP.

  • It is quite easy to maintain OOP applications even if there are a lot of maintainers to the project.

  • If you see certain parts of the code repeating in many places, OOP is the way to go.

  • If you want your application to be secure in terms of data from the rest of the components, OOP is again a great option as you can use Encapsulation to the advantage.

Conclusion

So, we have studied the concept of Object-Oriented Programming and how to define classes and create objects in C++. There are a lot of concepts in OOP to grab up in the next few parts of the series, but this should build the foundation of OOP as a paradigm or a way of thinking when it comes to problem-solving and programming.

I hope you have got a good introduction to the basics of Object-Oriented Programming and its semantics in C++. Thank you for reading. In the next part, we will explore the Access Modifiers/Specifiers in the Classes. Until then Happy Coding :)

 
Share this