Object Oriented Programming in C++ (original) (raw)

Object-Oriented Programming (OOP) is a programming paradigm that organizes programs using classes and objects. It helps developers create modular, reusable, and maintainable applications by modeling real-world entities in code.

Need for Object-Oriented Programming

Before Object-Oriented Programming (OOP), most programs followed a procedural approach where the focus was mainly on functions and step-by-step instructions. As programs became larger, managing and reusing code became difficult.

OOP in C++ was introduced to solve this problem by organizing programs into classes and objects, making applications easier to understand, reuse, and maintain.

Basic Concepts of Object-Oriented Programming

Object-Oriented Programming in C++ is built on concepts like classes, objects, inheritance, encapsulation, abstraction, and polymorphism that help in designing modular and reusable programs.

object_oriented_programming

Object-Oriented Programming

Class

A class is a user-defined blueprint used to create objects. It defines the data members and member functions that objects of the class will have. Using classes, multiple objects with similar properties and behavior can be created without rewriting code repeatedly.

**Components of a Class

Class_Object_example

C++ `

#include using namespace std;

class Student { public: string name; int age;

void display() {
    cout << name << endl;
}

};

int main() { // Create object Student s1;

// Assign values
s1.name = "Geeksforgeeks";

// Call function
s1.display();

return 0;

}

`

**Explanation: In this example, Student is a class and s1 is an object of that class. The object accesses the data member name and calls the member function display().

Object

An Object is a basic unit of Object-Oriented Programming that represents real-life entities. A typical C++ program creates many objects, which interact with each other by invoking methods. The objects are what perform your code, they are the part of your code visible to the user. An object mainly consists of:

#include #include using namespace std;

class Employee {

// Instance variables 

private: string name; float salary;

public: // Constructor Employee(string name, float salary) { this->name = name; this->salary = salary; }

// getters method
string getName() { return name; }
float getSalary() { return salary; }

// setters method
void setName(string name) { this->name = name; }
void setSalary(float salary) { this->salary = salary; }

// Instance method
void displayDetails() {
    cout << "Employee: " << name << endl;
    cout << "Salary: " << salary << endl;
}

};

int main() { Employee emp("Geek", 10000.0f); emp.displayDetails(); return 0; }

`

Output

Employee: Geek Salary: 10000

**Explanation: Here, emp is an object of the Employee class. The constructor initializes object data, and the member function displays employee details.

Four Pillars of OOP in C++:

Abstraction

Abstraction in C++ is the process of hiding the implementation details and only showing the essential details or features to the user. It allows to focus on what an object does rather than how it does it. In C++ abstraction is achieved using abstract classes (classes that have at least one pure virtual function).

C++ `

#include using namespace std;

class Shape {

public: // Pure virtual function virtual void draw() = 0; };

class Circle : public Shape {

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

int main() {

Circle c;

c.draw();

return 0;

}

`

**Explanation

Encapsulation

Encapsulation is the process of bundling data and methods into a single unit (class) and restricting direct access to some of its components. It acts as a protective shield for the data.

Encapsulation

Encapsulation in C++

C++ `

#include using namespace std;

class Student { private: int marks; public: void setMarks(int m) { marks = m;

    } 
    int getMarks() { 
        return marks; 
        
    } 

};

int main() { Student s; s.setMarks(90); cout << s.getMarks(); return 0;

}

`

**Explanation: The data member marks is private and can only be accessed using public member functions.

Inheritance

Inheritance is a mechanism in C++ where a class (derived) acquires the properties and behaviors of another class (base), forming an "is-a" relationship.

inheritance-660x454

Inheritance

C++ `

#include using namespace std; class Animal { public: void sound() { cout << "Animal makes sound";

    } 

}; class Dog : public Animal {

}; int main() { Dog d; d.sound(); return 0;

}

`

**Explanation: The Dog class inherits the sound() function from the Animal class.

Polymorphism

The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity can take many forms. In C++, polymorphism allows the same method or object to behave differently based on the context, specially on the project's actual runtime class.

1

Polymorphism in C++

C++ `

#include using namespace std; class Shape { public: virtual void draw() { cout << "Drawing Shape";

    } 

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

    } 

}; int main() { Shape* s; Circle c; s = &c; s->draw(); return 0;

}

`

**Explanation: The same function draw() behaves differently depending on the object type.

Advantages of OOP over Procedural Programming

Object-Oriented Programming provides several advantages compared to procedural programming.

Situations Where OOP May Not Be Suitable

Object-Oriented Programming may not be ideal in the following situations: