Lecture 1 - Introduction to Object-Oriented Programming (OOP) In
Lecture 1 - Introduction to Object-Oriented Programming (OOP) In
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to design
and implement software. C++ is one of the most widely used OOP languages. It allows developers to model
real-world problems using objects, making it easier to organize and structure complex programs.
Key Concepts of OOP in C++
1. Class and Object:
o Class: A class is a blueprint or template for creating objects. It defines the properties (attributes) and behaviors (methods) that the objects of that class
will have.
o Object: An object is an instance of a class. It is a specific realization of a class that holds actual values for attributes and can invoke methods defined by
the class.
class Car {
public:
string color;
string brand;
void drive() {
cout << "The car is driving" << endl;
}
};
int main() {
Car myCar; // Creating an object of class Car
myCar.color = "Red";
myCar.brand = "Toyota";
myCar.drive(); // Calling a method of the object
return 0;
}
2. Encapsulation: Encapsulation is the concept of bundling data (attributes) and methods (functions) that operate on the data into a single unit or class. It also
involves restricting access to some of the object’s components using access specifiers like public, private, and protected.
o Public: Members can be accessed from outside the class.
o Private: Members can only be accessed from within the class.
class Account {
private:
double balance; // Private attribute
public:
void deposit(double amount) {
balance += amount; // Public method to modify the private
attribute
}
double getBalance() {
return balance; // Public method to access the private attribute
}
};
3. Inheritance: Inheritance allows a new class to inherit properties and behaviors from an existing class. The new class is called the derived class, and the
existing class is the base class.
class Animal {
public:
void sound() {
cout << "Animal makes a sound" << endl;
}
};
int main() {
Dog myDog;
myDog.sound(); // Calls the method from the derived class
return 0;
}
4. Polymorphism: Polymorphism allows objects to behave in different ways based on their class. It can be achieved through function overloading and method
overriding.
o Method Overloading: Same method name but with different parameters.
o Method Overriding: A derived class provides its own implementation of a method that is already defined in the base class.
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};
int main() {
Animal* animal = new Dog();
animal->sound(); // Calls the overridden method in Dog
return 0;
}
4. Abstraction: Abstraction is the process of hiding the complex implementation details and showing only the essential features of the object. In C++, abstraction
is achieved through abstract classes and pure virtual functions.
class Shape {
public:
virtual void draw() = 0; // Pure virtual function (abstract method)
};
int main() {
Shape* shape = new Circle();
shape->draw(); // Calls the method in Circle
return 0;
}
Conclusion
C++ provides powerful support for Object-Oriented Programming, helping to model real-world problems
more effectively. By using classes, objects, inheritance, polymorphism, encapsulation, and abstraction,
developers can create organized, reusable, and maintainable code.