0% found this document useful (0 votes)
6 views

Lecture 1 - Introduction to Object-Oriented Programming (OOP) In

The document provides an introduction to Object-Oriented Programming (OOP) in C++, highlighting key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It explains how these concepts enable developers to model real-world problems and create organized, reusable, and maintainable code. C++ is emphasized as a powerful language for implementing OOP principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 1 - Introduction to Object-Oriented Programming (OOP) In

The document provides an introduction to Object-Oriented Programming (OOP) in C++, highlighting key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It explains how these concepts enable developers to model real-world problems and create organized, reusable, and maintainable code. C++ is emphasized as a powerful language for implementing OOP principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Introduction to Object-Oriented Programming (OOP) in C++

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;
}
};

class Dog : public Animal {


public:
void sound() {
cout << "Dog barks" << 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;
}
};

class Dog : public Animal {


public:
void sound() override { // Overriding the base class method
cout << "Dog barks" << 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)
};

class Circle : public Shape {


public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};

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.

You might also like