Module 2

Object-Oriented Programming in C++

25 mins read

Lesson 1: Fundamentals of OOP

1.1 Classes, Constructors & Encapsulation

Using C++ classes, constructors, and member methods to encapsulate objects.

C++
#include <iostream>
#include <string>

class Car {
private:
    std::string brand;
    int year;
    
public:
    Car(std::string b, int y) : brand(b), year(y) {}
    
    void displayDetails() {
        std::cout << "Brand: " << brand << ", Year: " << year << std::endl;
    }
};

int main() {
    Car car1("Toyota", 2020);
    car1.displayDetails();
    return 0;
}