Object oriented programming

Object oriented programming

Object-Oriented Programming (OOP) is a popular programming paradigm that focuses on the use of objects and their interactions to design and implement computer programs. C++ is a powerful language that fully supports OOP concepts. In this blog, we will introduce the basics of OOP in C++.

The four main principles of OOP are:

  1. Encapsulation

  2. Inheritance

  3. Polymorphism

  4. Abstraction

Encapsulation is the practice of keeping the implementation details of an object hidden from the outside world. In C++, we achieve encapsulation by using classes. A class is a blueprint for creating objects that contains data members and member functions. Data members are the variables that hold the state of the object, and member functions are the operations that can be performed on the object. The access to these data members and member functions can be restricted using access specifiers such as private, protected, and public.

Here is an example of a simple class in C++:

csharpCopy codeclass Car {
private:
    string make;
    string model;
    int year;
public:
    void setMake(string make) {
        this->make = make;
    }
    string getMake() {
        return make;
    }
    void setModel(string model) {
        this->model = model;
    }
    string getModel() {
        return model;
    }
    void setYear(int year) {
        this->year = year;
    }
    int getYear() {
        return year;
    }
};

In this example, we have defined a class called 'Car' that has three data members - make, model, and year - and six member functions - setMake, getMake, setModel, getModel, setYear, and getYear. The data members are declared as private, which means they can only be accessed by member functions of the same class.

Inheritance is the mechanism by which a new class can be derived from an existing class. The new class is called the derived class, and the existing class is called the base class. In C++, we use the ':' operator to indicate inheritance. The derived class inherits all the data members and member functions of the base class.

Here is an example of a derived class in C++:

arduinoCopy codeclass SportsCar : public Car {
public:
    void driveFast() {
        cout << "Driving " << getMake() << " " << getModel() << " fast!" << endl;
    }
};

In this example, we have defined a derived class called 'SportsCar' that inherits from the base class 'Car'. The 'SportsCar' class has one new member function called 'driveFast'. This function can access the data members of the base class using the inherited member functions.

Polymorphism is the ability of objects of different classes to be treated as if they are objects of the same class. Polymorphism is achieved in C++ through virtual functions. A virtual function is a member function of a class that can be overridden in a derived class. The 'virtual' keyword is used to declare a function as virtual.

Here is an example of a virtual function in C++:

csharpCopy codeclass Vehicle {
public:
    virtual void start() {
        cout << "Starting the vehicle..." << endl;
    }
};

class Car : public Vehicle {
public:
    void start() {
        cout << "Starting the car..." << endl;
    }
};

class Truck : public Vehicle {
public:
    void start() {
        cout << "Starting the truck..." << endl;
    }
};

In this example, we have defined a base class called 'Vehicle' with a virtual function called 'start'. We have also defined two derived classes - 'Car' and 'Truck' - that override the