C++ Object-Oriented Programming

Introduction

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to design and organize software. C++ supports OOP, allowing developers to create modular, reusable, and maintainable code. The key concepts of OOP include classes, objects, inheritance, polymorphism, encapsulation, and abstraction.

Classes and Objects

Defining a Class

A class is a blueprint for creating objects. It defines properties (data members) and behaviors (member functions or methods) that the objects created from the class will have.

Syntax for Defining a Class

class ClassName {
public:
    // Data members (attributes)
    dataType member1;
    dataType member2;

    // Member functions (methods)
    returnType functionName(parameters) {
        // Function body
    }
};

Example: Defining and Using a Class

#include <iostream>
using namespace std;

// Define a class named 'Person'
class Person {
public:
    // Data members
    string name;
    int age;

    // Member function to display person details
    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    // Create an object of the Person class
    Person person1;

    // Initialize object members
    person1.name = "John Doe";
    person1.age = 30;

    // Call the member function
    person1.display();

    return 0;
}

Output

Name: John Doe, Age: 30

Explanation

  • class Person defines a class named Person with data members name and age, and a member function display().
  • Person person1; creates an object person1 of the Person class.
  • The object members are initialized and the display() function is called to print the person’s details.

Constructors and Destructors

Constructors

Constructors are special member functions that are automatically called when an object is created. They are used to initialize objects.

Example: Constructor

#include <iostream>
using namespace std;

class Person {
public:
    string name;
    int age;

    // Constructor
    Person(string n, int a) {
        name = n;
        age = a;
    }

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    // Create an object using the constructor
    Person person1("John Doe", 30);

    // Call the member function
    person1.display();

    return 0;
}

Output

Name: John Doe, Age: 30

Explanation

  • Person(string n, int a) is a constructor that initializes the name and age members.
  • Person person1("John Doe", 30); creates an object person1 using the constructor.

Destructors

Destructors are special member functions that are automatically called when an object is destroyed. They are used to perform cleanup tasks.

Example: Destructor

#include <iostream>
using namespace std;

class Person {
public:
    string name;
    int age;

    // Constructor
    Person(string n, int a) {
        name = n;
        age = a;
    }

    // Destructor
    ~Person() {
        cout << "Destructor called for " << name << endl;
    }

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    // Create an object using the constructor
    Person person1("John Doe", 30);

    // Call the member function
    person1.display();

    return 0;
}

Output

Name: John Doe, Age: 30
Destructor called for John Doe

Explanation

  • ~Person() is a destructor that prints a message when called.
  • The destructor is automatically called when person1 goes out of scope.

Inheritance

Inheritance allows a class (derived class) to inherit properties and behaviors from another class (base class).

Example: Inheritance

#include <iostream>
using namespace std;

// Base class
class Person {
public:
    string name;
    int age;

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

// Derived class
class Student : public Person {
public:
    int studentID;

    void displayStudent() {
        display(); // Call base class function
        cout << "Student ID: " << studentID << endl;
    }
};

int main() {
    // Create an object of the derived class
    Student student1;
    student1.name = "John Doe";
    student1.age = 20;
    student1.studentID = 12345;

    // Call the derived class function
    student1.displayStudent();

    return 0;
}

Output

Name: John Doe, Age: 20
Student ID: 12345

Explanation

  • class Student : public Person defines a derived class Student that inherits from the base class Person.
  • The displayStudent() function calls the display() function from the base class.

Polymorphism

Polymorphism allows functions to be used in multiple forms. It can be achieved through function overloading and overriding.

Example: Function Overloading

#include <iostream>
using namespace std;

class Calculator {
public:
    // Overloaded functions
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
};

int main() {
    Calculator calc;

    cout << "Sum of integers: " << calc.add(5, 10) << endl;
    cout << "Sum of doubles: " << calc.add(5.5, 10.5) << endl;

    return 0;
}

Output

Sum of integers: 15
Sum of doubles: 16

Explanation

  • The Calculator class has two overloaded add functions with different parameter types.

Example: Function Overriding

#include <iostream>
using namespace std;

// Base class
class Animal {
public:
    virtual void makeSound() {
        cout << "Some generic animal sound" << endl;
    }
};

// Derived class
class Dog : public Animal {
public:
    void makeSound() override {
        cout << "Bark" << endl;
    }
};

int main() {
    Animal* animalPtr;
    Dog dog;

    animalPtr = &dog;
    animalPtr->makeSound(); // Calls the overridden function in Dog class

    return 0;
}

Output

Bark

Explanation

  • The makeSound function is overridden in the Dog class.
  • animalPtr->makeSound(); calls the overridden function in the Dog class.

Encapsulation

Encapsulation is the bundling of data and methods that operate on that data within a single unit, usually a class. It restricts direct access to some of the object’s components, which is a means of preventing unintended interference and misuse.

Example: Encapsulation

#include <iostream>
using namespace std;

class Person {
private:
    string name;
    int age;

public:
    void setName(string n) {
        name = n;
    }

    void setAge(int a) {
        age = a;
    }

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Person person1;
    person1.setName("John Doe");
    person1.setAge(30);
    person1.display();

    return 0;
}

Output

Name: John Doe, Age: 30

Explanation

  • The name and age members are private and can only be accessed through public member functions.

Abstraction

Abstraction involves hiding the complex implementation details and showing only the essential features of an object.

Example: Abstraction

#include <iostream>
using namespace std;

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

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

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

int main() {
    Circle circle;
    Rectangle rectangle;

    circle.draw();
    rectangle.draw();

    return 0;
}

Output

Drawing Circle
Drawing Rectangle

Explanation

  • Shape is an abstract class with a pure virtual function draw().
  • Circle and Rectangle classes inherit from Shape and implement the draw() function.

Conclusion

Object-Oriented Programming (OOP) in C++ allows for designing and organizing software using objects and classes. This chapter covered the key concepts of OOP, including classes and objects, constructors and destructors, inheritance, polymorphism, encapsulation, and abstraction. Understanding these concepts is crucial for writing modular, reusable, and maintainable code in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top