Instagram
youtube
Facebook
Twitter

Classes and Objects

Object-Oriented Programming ( OOP )

Object-Oriented Programming (OOP) is a programming paradigm that focuses on organizing code into objects, which are instances of classes. Each object contains data (attributes) and functions (methods) that operate on that data. C++ is an object-oriented programming language that supports the four abecedarian principles of OOP: encapsulation, inheritance, polymorphism, and abstraction. Then there is a brief overview of each principle in the context of C++.

  • Class Definition: A class is a blueprint for creating objects. It defines the properties(attributes) and behaviors (methods and functions) that the objects of the class will have.
class Car {

private:

    string brand;

    string model;

    int year;

public:

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

        brand = b;

        model = m;

        year = y;

    }

    void displayInfo() {

        cout << year << " " << brand << " " << model << endl;

    }

};

 

  • Object Creation:Objects are instances of classes.They represent real-world entities and encapsulate data and behavior associated with the class.
Car car1("Toyota", "Camry", 2023);

Car car2("Honda", "Civic", 2022);

 

  • Accessing Class Members: You can access the members(attributes and methods) of a class using the dot notation(‘.’).
car1.displayInfo(); 

car2.displayInfo();