OOP (Object Oriented Programming) organises a program around objects that hold data (attributes) and behaviour (methods) together.
Class is the blueprint; object is a real instance created from it. Example: class Car → objects myCar, yourCar.
Encapsulation, Abstraction, Inheritance, Polymorphism
class Car {
public:
string brand; // attribute
int speed;
void drive() { // method
cout << brand << " is driving at " << speed << " km/h\n";
}
};
int main() {
Car c1; // object
c1.brand = "Toyota";
c1.speed = 80;
c1.drive();
}
Q & A
-
Does a class take memory?No. Memory is allocated only when an object is created.
-
Advantages of OOP?Reusability (inheritance), security (encapsulation), easy maintenance, real-world modelling and flexibility (polymorphism).