Introduction of Object-Oriented Programming

Introduction of Object-Oriented Programming

Introduction

Object-oriented programming is nothing but it's just an approach to mapping your problem with real-world or life objects. It is all about creating objects that have some attributes and methods (block of code that is reusable).

Let's know some more about OOP😊.

This approach helps us to work on Dry (don’t repeat itself) code. This means we can use our one-time written code multiple times in multiple places without writing it again and again, we just need to call its instance.

It helps to keep the structure of your program clean❤️.

It makes the program short(in terms of the number of lines), efficient, fast, and easier.

Famous languages which enable us to use object-oriented programming (OOP)

Famous languages which enable us to use object-oriented programming (OOP) are following

  • Java

  • Ruby

  • Python

  • Typescript

  • C++

  • C#

Class and Objects

This is a core concept for learning about OOP.

OOP enables us to associate everything with classes and real-life objects which have attributes and methods

CLASSES

Class is a user-defined data type. We can also say that it works like a prototype or template.

Every language has its own syntax for creating a class but in this blog, we will only discuss the syntax for c++, the concept is the same in every language, only the syntax is different.

For creating a class we use the class keyword in c++

Syntax:-

class HelloClass{
    private:   // access specifier (don't worry we will discuss the latter in this blog)
        int a;
};

OBJECTS

The object is also known as an instance of a class. The question is here why it is called an instance of the class. So the answer is, when we create objects of a class, each object possesses characteristics and is defined within the class. Since an object indirectly represents a class, it is referred to as an "instance of a class"

Let's first take a look at object declaration syntax💕

Code:-

#include<iostream>
using namespace std;
class HelloClass{       // The class
  public:             // Access specifier
    int no;        // Attribute (int variable)

};

int main() {
 HelloClass Obj;  // Create an object of MyClass

  // Access attributes and set values
  myObj.no= 15; 


  // Print attribute values
  cout << myObj.myNum << "\n";

  return 0;
}

Here we created the object (obj) of the class to access the attribute of HelloClass.

For accessing any attribute and method from the class we use the dot operator(.) like used in our code (myObj.no\= 15; )

Constructor

Constructor is also known as a Special method. It is automatically called when an object of the class is created

Syntax:-

To create a constructor method or function we use the Same name as the class

Like void HelloCLass() is the constructor of the upper declared class (HelloClass)

CODE:-

#include<iostream>
using namespace std;
class HelloClass {     // The class
  public:           // Access specifier
    HelloClass() {     // Constructor
      cout << "Hello World!";
    }
};

int main() {
  HelloClass myObj;    // Create an object of HelloClass (this will call the constructor)
  return 0;
}

Access Specifiers:-

Access specifiers are used to defining how the attribute and method can be accessed from the class

There are three main access specifiers that are widely used in all famous oop supported languages

Public, Private, and Protected.

public - members are accessible from outside the class

private - members cannot be accessed (or viewed) from outside the class

protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes

CODE:-

#include<iostream>
using namespace std;
classHelloClass {
  public:    // Public access specifier
    int x;   // Public attribute
  private:   // Private access specifier
    int y;   // Private attribute
};

int main() {
 HelloClass obj;
  obj.x = 25;  // Allowed (public)
  obj.y = 50;  // Not allowed (private)
  return 0;
}

Four Pillers of OOP

There are four pillars of oop.👍

  1. Abstraction

  2. Polymorphism

  3. Encapsulation

  4. Inheritance

We will take a detailed overview of these pillars in a separate blog.

Thanks❤️

if you have any questions you can ask me on my social media handles

Linkedin 😊

tweeter ❤️