Oop concept πŸ₯°

πŸ˜πŸ˜‡ Object-Oriented Programming (OOP) is a programming method that uses objects and classes to design programs.

It helps to make programs easy to understand, reusable, and secure.

In OOP, everything is treated as an object.

πŸ”Ή Basic Concepts of OOP

1. Class

A class is a blueprint or template used to create objects.

πŸ‘‰ Example:

Think of a Car as a class.

Car has:

Color

Speed

Model

These are called properties (variables) and functions (methods).

Code: C++

class Car {

public:

    string color;

    int speed;


    void drive() {

        cout << "Car is driving";

    }

};


2. Object

An object is an instance of a class.

πŸ‘‰ Real-life Example:

If Car is a class, then BMW, Audi are objects.

C++  code

Car c1;

c1.color = "Red";

c1.speed = 100;

c1.drive();


Four Main Principles of OOP

3. Encapsulation

Encapsulation means binding data and functions together in a single unit (class).

It also protects data from unauthorized access using private access.

C++ code

class Student {

private:

    int marks;


public:

    void setMarks(int m) {

        marks = m;

    }


    int getMarks() {

        return marks;

    }

};


4. Abstraction

Abstraction means showing only necessary details and hiding internal details.

πŸ‘‰ Example: When you drive a bike, you don’t know how the engine works internally.

C++ code

class ATM {

public:

    void withdrawMoney() {

        cout << "Money Withdrawn";

    }

};


5. Inheritance

Inheritance allows one class to use properties and methods of another class.

πŸ‘‰ Example:

Child class inherits from Parent class.

C++  code

class Animal {

public:

    void eat() {

        cout << "Eating";

    }

};


class Dog : public Animal {

public:

    void bark() {

        cout << "Barking";

    }

};


  • Code Reusability
  • Less duplication

6. Polymorphism

Polymorphism means many forms.

It allows the same function to behave differently.

➤ Function Overloading

Same function name with different parameters.

C++ code

int add(int a, int b) {

    return a + b;

}


int add(int a, int b, int c) {

    return a + b + c;

}

➤ Function Overriding

Child class changes the behavior of parent class function.

C++ code

class Parent {

public:

    void show() {

        cout << "Parent class";

    }

};


class Child : public Parent {

public:

    void show() {

        cout << "Child class";

    }

};
πŸ”Ή Advantages of OOP

  • Reusability
  • Easy Maintenance
  • Data Security
  • Modularity
  •  Real-world modeling

πŸ”Ή Disadvantages of OOP

  • Complex for beginners
  •  Requires more memory
  • Takes more time to design

Comments

Popular posts from this blog

History of c programming language

Evolution of programming languages

How to Create a Bloger account