Object-Oriented Programming (C++)- The Easy Way (Part-I)

Object-Oriented Programming (C++)- The Easy Way (Part-I)

We have been battling with learning Object-Oriented Programming from various resources, be it textbooks, online materials, videos, and so on. We somehow grasp the concept, but when it comes to the interview, we are stuck with one simple yet unanswerable question.

“How will I revise my OOP concept now? I definitely can’t go through all of my previous resources!”

With this idea, I intended to make a series of article that visits the entirety of the OOP Concepts in a brief yet point by point way. I picked C++ as my transporter language as it is known to huge numbers of us and supports the object-oriented approach as well. Do give it a read!

We talk about OOP, but never ask why we need it? Weren’t we good enough with the traditional procedural programming style?

Basically, OOP deals with clearing out the confusion and simplifying long, really long codes into many different groups! It makes the program development easier and manageable. Moreover, it helps you hide some parts of code from the others so that the only accessible thing remains the feature and all its implementation details get safely hidden. This bit isn’t really possible with a procedural style of programming.

Also, you can definitely think of a reason why do you need OOPS in your life?

Once we know the need, we are all set to know what it entails.

The basic, the atom of OOP — Object

An Object simply means a real-world entity. It may be a phone, tablet, pen, and so on. Object-Oriented Programming simply means designing a program or set of rules consisting of objects and classes. But wait, what’s a class?

A Class is nothing but a collection of objects. You can call it a logical entity. An object contains both data and functionality.

Let’s try to further understand these concepts with the help of a program


#include<iostream>
using namespace std;

class Country{
    public:
        string name;
        int populationCount;

    void storeCountryInformation(string s, int n){
        name = s;
        populationCount = n;
    }
};

int main(){
    Country c1;
    c1.storeCountryInformation("India", 1300000000);
    return 0;
}

In the above code, we created a class Country with a function to store its basic information such as name and population count. The fields name and populationCount are called Data Members of the class. Furthermore, the function storeCountryInformation is known as a Class Method.

Moreover, Country is a type and c1 is the reference variable that refers to the instance of class Country.

- Constructors & Destructors

A constructor is a unique method that is invoked at the time of object creation automatically. In other words, a constructor constructs objects of a class. Also, an object is created at runtime and is a runtime entity.

A destructor is a similar method with the opposite functioning. It destructs the objects within a class and is also called automatically. They are declared similar to a default constructor except it is preceded by tilde(~).

There can be multiple types of constructors like Default, Parameterized, Copy Constructor declared within a class, but only one destructor function can be declared per class.

Let’s understand all of these through a code.

#include<iostream>
using namespace std;

class Country{
    public:
        string name;
        int populationCount;

    //Constructor
    Country(){
        cout<<"Country constructor envoked"<<endl;
    }

    //Destructor
    ~Country();

    //Parameterized Counstructor
    Country(string s, int n){
        this->name = s; // or name = s
        populationCount = n; // or this->populationCount = n
    }

    void storeCountryInformation(string s, int n){
        name = s;
        populationCount = n;
    }

    void printCountryDetails(){
        cout<<"Country Name: "<<this->name<<endl;
        cout<<"Population Count: "<<this->populationCount<<endl;
    }
};

Country::~Country(){
    cout<<"Destructor envoked"<<endl;
}

int main(){
    Country c1;
    c1.storeCountryInformation("India", 1300000000);
    c1.printCountryDetails();
    Country c2 = Country("USA", 327200000);
    c2.printCountryDetails();
    return 0;
}
/*
Output:
Country constructor envoked
Country Name: India
Population Count: 1300000000
Country Name: USA
Population Count: 327200000
Destructor envoked
Destructor envoked
*/

Along with the use of Default Constructor and Parameterized Constructor, a special keyword, this has been used. It refers to nothing but the current instance of the class Country. Talking a little more about Destructors, they neither accept any arguments nor return anything and are called when the object goes out of scope. What does this mean?

An object could be out of scope due to any of the following reasons:

1) The function ends 2) The program ends 3) A block containing local variable ends 4) A delete operator is called

Also, did you see I defined the destructor outside the class! Yes, it’s possible through the Scope Resolution Operator (::). Feel free to use it whenever you want to keep your class complete and crisp at the same place ;-).

If we are so versed with the destructors now, can we answer why in the world do we need it at all? Didn’t C++ add one for me already? Well, C++ won’t betray you! If you don’t write your own destructor, the C++ compiler would add one by itself. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid a memory leak. Did you know destructors can also be virtual? If you don’t know virtual, sit back and continue reading, I would visit it soon :-). If you want to read about Virtual Destructors & Pure virtual destructors, I am leaving a link below. Thank me later ;-)

geeksforgeeks.org/virtual-destructor

geeksforgeeks.org/pure-virtual-destructor-c

About me? I am a tech lover! I love to read, implement and explain :)

Best.