Skip to main content

C++ Lec-2 Introduction to Object-Oriented Programming (OOP) in C++

 


Introduction to Object-Oriented Programming (OOP) in C++


Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize software design. It allows for modelling real-world entities and relationships in a program. C++ is an object-oriented programming language that provides features to implement OOP concepts effectively.

Key Concepts of OOP

 

Class and Object

o   Class: A blueprint for creating objects. It defines properties and behaviours of objects.

 

class Car {

public:

    string brand;

    string model;

    int year;

};

 

o   Object: An instance of a class.

 

Car car1;

car1.brand = "Mahindra";

car1.model = "THAR";

car1.year = 2024;

 

Encapsulation

o   Encapsulation is the bundling of data and methods that operate on that data within a single unit, or class. It restricts direct access to some of an object's components.

 

class Car {

private:

    string brand;

    string model;

    int year;

public:

    void setBrand(string b) { brand = b; }

    string getBrand() { return brand; }

};

 

Inheritance

o   Inheritance allows a class to inherit properties and behaviors from another class.

 

 

class Vehicle {

public:

    string brand = "Ford";

    void honk() {

        cout << "Beep beep!" << endl;

    }

};

 

class Car : public Vehicle {

public:

    string model = "Mustang";

};

 

 

Polymorphism

o   Polymorphism allows methods to do different things based on the object it is acting upon. There are two types: compile-time (method overloading) and runtime (method overriding).

 

class Animal {

public:

    virtual void makeSound() {

        cout << "Some sound" << endl;

    }

};

 

class Dog : public Animal {

public:

    void makeSound() override {

        cout << "Bark" << endl;

    }

};

 

 

 

 

 

Abstraction

o   Abstraction hides complex implementation details and shows only the essential features of the object.

 

 

class AbstractCar {

public:

    virtual void startEngine() = 0;  // Pure virtual function

};

 

class MyCar : public AbstractCar {

public:

    void startEngine() override {

        cout << "Engine started" << endl;

    }

};

 

 


Example: Implementing OOP Concepts in C++

 

#include <iostream>

using namespace std;

 

// Base class

class Vehicle {

public:

    string brand;

    void honk() {

        cout << "Beep beep!" << endl;

    }

};

 

// Derived class

class Car : public Vehicle {

public:

    string model;

    int year;

 

    Car(string b, string m, int y) {

        brand = b;

        model = m;

        year = y;

    }

 

    void display() {

        cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << endl;

    }

};

 

int main() {

    Car car1("Toyota", "Corolla", 2020);

    car1.display();

    car1.honk();

 

    return 0;

}

Need of OOP

  1. Real-world Modelling: OOP allows for creating models that closely represent real-world entities, making it easier to design and understand the system.
  2. Reusability: Classes and objects can be reused across programs, reducing redundancy.
  3. Scalability: OOP principles support the growth and complexity of software projects.
  4. Maintenance: OOP makes it easier to manage and maintain existing code, promoting the use of modular code.
  5. Abstraction: Hides the complex implementation details and exposes only necessary parts.
  6. Encapsulation: Keeps the data safe from outside interference and misuse.
  7. Inheritance: Facilitates code reuse by allowing new classes to inherit properties and behaviours from existing classes.
  8. Polymorphism: Allows for using a single interface to represent different data types.

Advantages of OOP

  1. Modularity: Code is organized into objects, making it modular and easier to manage.
  2. Code Reusability: Inheritance and polymorphism promote reusability.
  3. Maintainability: Encapsulation ensures that internal implementation details are hidden, making the code more maintainable.
  4. Extensibility: New features can be added with minimal changes to the existing codebase.
  5. Productivity: Reusability and modularity can significantly enhance development productivity.
  6. Security: Data hiding and abstraction protect the integrity of the data.
  7. Flexibility: Polymorphism and dynamic binding provide flexibility in code usage.

How We Can Say C++ is OOP

C++ supports all the fundamental concepts of OOP, which include:

  1. Classes and Objects: C++ uses classes as blueprints for creating objects.
  2. Encapsulation: Using access specifiers to restrict access to the data members of a class.
  3. Inheritance: C++ supports inheritance, allowing classes to inherit properties from other
  4. Polymorphism: C++ supports function overloading and overriding, allowing polymorphism.
  5. Abstraction: Abstract classes and interfaces can be created using pure virtual functions.

Origin of OOP Concepts

The concept of Object-Oriented Programming originated in the 1960s with the development of the programming language Simula. Simula was designed for simulation purposes and introduced the concepts of classes and objects.

In the 1970s, Alan Kay and his team further developed these ideas while working on the Smalltalk programming language at Xerox PARC. They are credited with coining the term "object-oriented programming."

Bjarne Stroustrup, the creator of C++, was inspired by Simula and extended the C programming language to support object-oriented programming, resulting in the creation of C++ in the early 1980s.



Comments

Popular posts from this blog

C++ Lec-1 (Introduction to Programming Languages)

  Introduction to Programming Languages ________________________________________________________________________ P rogramming languages are formal languages comprising a set of instructions that produce various kinds of output. They are used in computer programming to implement algorithms and control the behavior of machines. Language Generation Programming languages have evolved through several generations: First Generation (1GL) : Machine language, the most basic level of programming languages, consisting of binary code. Second Generation (2GL) : Assembly language, a low-level language with a strong correspondence to the machine language instructions. Third Generation (3GL) : High-level languages like C, C++, Java, Python, which are more abstract and easier for humans to understand. Fourth Generation (4GL) : Languages closer to human language, often used for database querying and report generation (e.g., ...

DATA TYPE in C/C++

  BUILT IN OF PRIMARY DATA TYPE     int (Integer) float(Floating-point) double (Double Floating-point) char (Character) wchar_t (Wide Character) bool (Boolean) void (Empty) Modified Data Types List    signed int - (used for integers) unsigned - int (can only store positive integers) short (used - for small integers (range -32768 to 32767)) unsigned short - (used for small positive integers (range 0 to 65,535)) Long - (used for large integers) unsigned long - (used for large positive integers ) long long - (used for very large integers) unsigned long long - (used for very large positive integers) long double - (used for large floating-point numbers) signed char - (used for characters (guaranteed range -127 to 127)) unsigned char - (used for characters (range 0 to 255)) Derived Type Data Type    Function Array Pointers References User-Defined Data Type   Class Structure Union Enumeration Typedef defined DataType