CS for Beginners (Session 04)

Shape Image One

OBJECT-ORIENTED
PROGRAMMING
IN C++

Introduction to Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). C++ is one of the pioneering languages that introduced OOP to system and application development.

Objectives of This Session

Understand the basic concepts of OOP in C++: Classes, Objects, and Methods.

Learn how to define classes and instantiate objects in C++.

Explore the principles of encapsulation and constructors.

1. Basic Concepts of OOP

Classes and Objects:

  – Class: A blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).

  – Object: An instance of a class.

– Example: Defining a class and creating an object.

class Car {

  public:

      string brand;

      string model;

      int year;

  };

 

  int main() {

      Car myCar;  // Create an object of Car

      myCar.brand = “Toyota”;

      myCar.model = “Corolla”;

      myCar.year = 2021;

 

      cout << “Car: ” << myCar.brand << ” ” << myCar.model << ” ” << myCar.year;

      return 0;

  }

2. Methods in Classes

Definition and Usage:

Methods in C++ are functions defined inside a class. They are used to perform operations on attributes.

Example:

Adding a method to the `Car` class.

 

  class Car {

  public:

      string brand;

      string model;

      int year;

      void displayInfo() {

          cout << “Car: ” << brand << ” ” << model << ” ” << year;

      }

  };

  int main() {

      Car myCar;

      myCar.brand = “Toyota”;

      myCar.model = “Corolla”;

      myCar.year = 2021;

      myCar.displayInfo();  // Call the method

      return 0;

  }

3. Encapsulation

Definition:

The idea of wrapping data and the methods that work with the data into a single unit. This prevents external access to some of the object’s components, which can protect the object’s internal state.

Using Access Modifiers:

– Public: Members are accessible from outside the class.

  – Private: Members cannot be accessed (or viewed) from outside the class.

– Example: Implementing encapsulation in the `Car` class.

  class Car {

  private:

      int year;

  public:

      string brand;

      string model;

      void setYear(int y) {

          if (y > 1990) {  // Simple validation

              year = y;

          }

      }

      int getYear() {

          return year;

      }

      void displayInfo() {

          cout << “Car: ” << brand << ” ” << model << ” – ” << getYear();

      }

  };

  int main() {

      Car myCar;

      myCar.brand = “Toyota”;

      myCar.model = “Corolla”;

      myCar.setYear(2021);  // Set the year using a method

      myCar.displayInfo();

      return 0;

  }

4. Constructors

Purpose:

Special method automatically called when an object of a class is created. Used to initialize objects.

Example:

Constructor in the `Car` class.

  class Car {

  public:

      string brand;

      string model;

      int year;

      // Constructor

      Car(string b, string m, int y) {

          brand = b;

          model = m;

          year = y;

      }

      void displayInfo() {

          cout << “Car: ” << brand << ” ” << model << ” ” << year;

      }

  };

  int main() {

      Car myCar(“Toyota”, “Corolla”, 2021);

      myCar.displayInfo();

      return 0;

  }

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

The introduction to Object-Oriented Programming in C++ provides a fundamental understanding of how to structure code in a more logical, reusable, and clear manner. This session covers the creation and use of classes, objects, methods, encapsulation, and constructors, which are essential components of OOP.

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