OOPs (Object Oriented Programming)

OOPs (Object Oriented Programming)

ยท

3 min read

Just as the name sounds when developer begins to learn Object Oriented Programming their mind get feeling of Oops ๐Ÿ˜ฌ. Yep, Oops is hard for absolute beginner to grasp. However, Once you get familiar with Object Oriented Programming you will fall in love with OOPs, as it makes the code more readable and scalable.

What is OOPs?

OOPs is Object Oriented Programming which is a programming paradigm to write code. Almost every modern programming language support Object Oriented Programming as it makes easier for developer to make scalable, readable code while working with a team in Collaboration. Object Oriented Programming revolves arround the concept of Object and Class.

  • Class: Class is like a blueprint or structure for declaring Objects. We can define class and we can use these class to generate Objects.
// Class declaration
class MyClass {
public:
    // Member variables
    int myInteger;
    // Member functions
    void myFunction();
};

here, we declare two variables myInteger , and a function myFunction(). Now we will create an object and we can use these variables and functions.

  • Object: Object are instance of the class. By means that Now we can use the blueprint(class) and use it in our program. We can access data members and member function of the class. However, we have to learn about access specifiers.
#include <iostream>
using namespace std;

// Class declaration
class MyClass {
public:
    // Member variables
    int myInteger;

    // Member function
    void myFunction() {
        cout << "Integer: " << myInteger << endl;
    }
};

int main() {
    // Creating an object of MyClass
    MyClass obj;

    // Accessing member variables and functions using the object
    obj.myInteger = 10;
    obj.myFunction();

    return 0;
}

To initiate the Object we have to write the name of class and then name on the object as above example. Now, with the dot operator we can access data members and member function.


More concepts about Oops

  • Access Specifiers: Access Specifiers are used in class to define the visibility of the data members and member function inside a class, Which means how can or where we can access the data members and member function.

    There are basically 3 type of access Specifiers

  1. Public: Public access Specifiers defines that all the data members and member functions can be used anywhere inside the program. Such as in above example we defined the Public function and variable which we use with dot operator with Object.

  2. Private: Private access Specifiers defines that data members and member function only can be used inside the class.

#include <iostream>

class MyClass {
private:
    int privateVar;

public:
    void setPrivateVar(int val) {
        privateVar = val;
    }

    int getPrivateVar() {
        return privateVar;
    }
};

int main() {
    MyClass obj;
    obj.setPrivateVar(10);
    std::cout << "Private variable value: " << obj.getPrivateVar() << std::endl;

    // Attempting to access privateVar directly will result in a compilation error
    // std::cout << "Private variable value: " << obj.privateVar << std::endl;

    return 0;
}

If we try to access the private data members and member function. It will produce error.

  1. Protected: Protected Access Specifiers defines that data members and member function only can be used by the child class which inherited from parent class. We will discuss later about inheritance as it is one of the four pillars of Oops.

There are four pillars of OOPs

  1. Encapsulation

  2. Inheritance

  3. Polymorphism

  4. Abstraction

We will discuss about these in next Blog. Stay tuned. ๐Ÿ‘‹

ย