CS for Beginners (Session 05)

Shape Image One

OBJECT-ORIENTED
PROGRAMMING
IN C++

Continuing with Object-Oriented Programming in C++

This session expands upon the initial concepts of OOP covered in the previous class. We will delve into more advanced features of Object-Oriented Programming in C++, including inheritance, polymorphism, and abstract classes.

Objectives of This Session

Understand and implement inheritance in C++.

Explore the concept of polymorphism and how it enhances flexibility.

Introduction to abstract classes and interfaces.

1. Inheritance

Definition:

A mechanism in which one class acquires the properties (methods and fields) of another. With inheritance, we can create a new class based on an existing class.

Types:

 – Single Inheritance: Deriving a new class from one base class.

  – Multiple Inheritance: Deriving a new class from more than one base class.

– Syntax:

  class Base {

  public:

      void baseFunction() {

          cout << “Function of Base Class”;

      }

  };

  class Derived : public Base { };

  int main() {

      Derived obj;

      obj.baseFunction();  // Calling function of base class using derived class

  }

2. Polymorphism

Definition:

The ability of a message to be displayed in more than one form. In C++, it is achieved by method overriding and operator overloading.

Method Overriding:

  – Happens when a derived class has a definition for one of the member functions of the base class. That base function is redefined in the derived class.

Example:

 class Base {

    public:

        virtual void show() {  // Note the virtual keyword

            cout << “Base class show function called.”;

        }

    };

    class Derived : public Base {

    public:

        void show() {

            cout << “Derived class show function called.”;

        }

    };

    int main() {

        Base* basePtr;

        Derived d;

        basePtr = &d;

        basePtr->show();  // Points to Derived’s show()

    }

3. Abstract Classes and Interfaces

Abstract Classes:

 – Cannot be instantiated and are typically used as base classes.

 – Must have at least one pure virtual function.

Syntax:

 class Abstract {

  public:

      virtual void f() = 0;  // Pure virtual function

  };

  class Concrete : public Abstract {

  public:

      void f() {

          cout << “Function f implemented.”;

      }

  };

  int main() {

      Concrete c;

      c.f();  // Calls Concrete’s f()

  }

Interfaces:

  – An interface in C++ is a class with only pure virtual functions and no data members.

4. Practical Examples

Implementing an Interface:

class Shape {

  public:

      virtual void draw() = 0;  // Pure virtual function, making Shape an interface

  };

  class Circle : public Shape {

  public:

      void draw() {

          cout << “Drawing Circle.”;

      }

  };

  int main() {

      Circle c;

      c.draw();  // Calls Circle’s draw()

  }

5. Getting Started with Learning

Resources:

 – [Code With Harry for learning C++] (https://bit.ly/4b3XLUy)

 – [W3 Schools for learning C++] (https://www.w3schools.com/cpp/)

Conclusion

In this session, we have expanded your understanding of OOP in C++ by exploring inheritance, polymorphism, and abstract classes. These concepts are crucial for designing flexible and reusable code in larger software projects.

Optimized by Optimole
WhatsApp whatsapp
Messenger messenger
Instagram instagram
Call Us phone
chat