Object-Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects, which are instances of classes. Classes define the structure and behavior of objects, allowing us to create reusable and modular code.
Key Concepts in OOP
Encapsulation
Encapsulation is the concept of bundling data and the operations that can be performed on that data within a single unit called a class. It allows us to hide the implementation details and provide a clean interface for interacting with objects.
In C++, a class is defined using the class
keyword. For example:
1class Person {
2private:
3 string name;
4 int age;
5
6public:
7 void setName(string n) {
8 name = n;
9 }
10
11 string getName() {
12 return name;
13 }
14
15 void setAge(int a) {
16 age = a;
17 }
18
19 int getAge() {
20 return age;
21 }
22};
In the above code, we define a class Person
with private
data members name
and age
, and public
member functions setName
, getName
, setAge
, and getAge
.
Inheritance
Inheritance allows us to create new classes (derived classes) based on existing classes (base classes) and inherit their properties and behavior. It is used to establish a hierarchical relationship between classes.
In C++, inheritance is achieved using the class
keyword with a colon followed by the access specifier and the base class name. For example:
1class Student : public Person {
2private:
3 string major;
4
5public:
6 void setMajor(string m) {
7 major = m;
8 }
9
10 string getMajor() {
11 return major;
12 }
13};
In the above code, we define a derived class Student
that inherits from the base class Person
. The public
member functions of the base class are accessible in the derived class.
Polymorphism
Polymorphism allows objects of different types to be treated as objects of a common type, enabling code reuse and extensibility. It can be achieved through function overloading and function overriding.
In C++, function overloading is the ability to define multiple functions with the same name but different parameter lists. For example:
1void print(int num) {
2 cout << "Integer: " << num << endl;
3}
4
5void print(float num) {
6 cout << "Float: " << num << endl;
7}
In the above code, we define two functions print
with the same name but different parameter types. The appropriate function is called based on the argument passed.
Function overriding, on the other hand, is the ability to define a function in the derived class that has the same name, return type, and parameters as a function in the base class. For example:
1class Shape {
2public:
3 virtual void draw() {
4 cout << "Drawing a Shape" << endl;
5 }
6};
7
8class Circle : public Shape {
9public:
10 void draw() override {
11 cout << "Drawing a Circle" << endl;
12 }
13};
In the above code, we define a base class Shape
with a virtual function draw
, and a derived class Circle
that overrides the draw
function to provide its own implementation.
Benefits of OOP
- Modularity: OOP promotes modular code, allowing for easy maintenance and reusability.
- Code Organization: Objects encapsulate related data and behavior, making code organization more intuitive.
- Portability: OOP facilitates code portability and reuse through inheritance and polymorphism.
- Abstraction: Classes and objects provide an abstraction layer, hiding complex implementation details.