Introduction to Object-Oriented Programming (OOP) in C++
Object-Oriented
Programming (OOP) is a programming paradigm that uses objects and classes to
organize software design. It allows for modelling real-world entities and
relationships in a program. C++ is an object-oriented programming language that
provides features to implement OOP concepts effectively.
Key Concepts of OOP
Class and Object
o Class: A blueprint
for creating objects. It defines properties and behaviours of objects.
class Car {
public:
string brand;
string model;
int year;
};
o Object: An instance of
a class.
Car car1;
car1.brand = "Mahindra";
car1.model = "THAR";
car1.year = 2024;
Encapsulation
o Encapsulation is
the bundling of data and methods that operate on that data within a single
unit, or class. It restricts direct access to some of an object's components.
class Car {
private:
string brand;
string model;
int year;
public:
void setBrand(string b) { brand = b; }
string getBrand() { return brand; }
};
Inheritance
o Inheritance
allows a class to inherit properties and behaviors from another class.
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Beep beep!"
<< endl;
}
};
class Car : public Vehicle {
public:
string model = "Mustang";
};
Polymorphism
o Polymorphism
allows methods to do different things based on the object it is acting upon.
There are two types: compile-time (method overloading) and runtime (method
overriding).
class Animal {
public:
virtual void makeSound() {
cout << "Some sound"
<< endl;
}
};
class Dog : public Animal {
public:
void makeSound() override {
cout << "Bark" <<
endl;
}
};
Abstraction
o Abstraction
hides complex implementation details and shows only the essential features of
the object.
class AbstractCar {
public:
virtual void startEngine() = 0;
// Pure virtual function
};
class MyCar : public AbstractCar {
public:
void startEngine() override {
cout << "Engine
started" << endl;
}
};
Example: Implementing OOP Concepts in C++
#include <iostream>
using namespace std;
// Base class
class Vehicle {
public:
string brand;
void honk() {
cout << "Beep beep!"
<< endl;
}
};
// Derived class
class Car : public Vehicle {
public:
string model;
int year;
Car(string b, string m, int y) {
brand = b;
model = m;
year = y;
}
void display() {
cout << "Brand: "
<< brand << ", Model: " << model << ",
Year: " << year << endl;
}
};
int main() {
Car car1("Toyota", "Corolla", 2020);
car1.display();
car1.honk();
return 0;
}
Need of OOP
- Real-world Modelling: OOP
allows for creating models that closely represent real-world entities,
making it easier to design and understand the system.
- Reusability: Classes
and objects can be reused across programs, reducing redundancy.
- Scalability: OOP
principles support the growth and complexity of software projects.
- Maintenance: OOP makes
it easier to manage and maintain existing code, promoting the use of
modular code.
- Abstraction: Hides the
complex implementation details and exposes only necessary parts.
- Encapsulation: Keeps the
data safe from outside interference and misuse.
- Inheritance:
Facilitates code reuse by allowing new classes to inherit properties and
behaviours from existing classes.
- Polymorphism: Allows
for using a single interface to represent different data types.
Advantages of OOP
- Modularity: Code is
organized into objects, making it modular and easier to manage.
- Code Reusability:
Inheritance and polymorphism promote reusability.
- Maintainability:
Encapsulation ensures that internal implementation details are hidden,
making the code more maintainable.
- Extensibility: New
features can be added with minimal changes to the existing codebase.
- Productivity:
Reusability and modularity can significantly enhance development
productivity.
- Security: Data
hiding and abstraction protect the integrity of the data.
- Flexibility:
Polymorphism and dynamic binding provide flexibility in code usage.
How We Can Say C++ is OOP
C++ supports all
the fundamental concepts of OOP, which include:
- Classes and Objects: C++ uses
classes as blueprints for creating objects.
- Encapsulation: Using
access specifiers to restrict access to the data members of a class.
- Inheritance: C++
supports inheritance, allowing classes to inherit properties from other
- Polymorphism: C++
supports function overloading and overriding, allowing polymorphism.
- Abstraction: Abstract classes and interfaces can be created using pure virtual functions.
Origin of OOP Concepts
The concept of
Object-Oriented Programming originated in the 1960s with the development of the
programming language Simula. Simula was designed for simulation purposes and
introduced the concepts of classes and objects.
In the 1970s,
Alan Kay and his team further developed these ideas while working on the
Smalltalk programming language at Xerox PARC. They are credited with coining
the term "object-oriented programming."
Bjarne
Stroustrup, the creator of C++, was inspired by Simula and extended the C
programming language to support object-oriented programming, resulting in the
creation of C++ in the early 1980s.
Comments
Post a Comment