📚

Introduction to Object Oriented Programming

Aug 10, 2024

Lecture 42: Introduction to Object Oriented Programming

Instructor: Love Babbar


What is Object Oriented Programming (OOP)?

  • A programming technique that revolves around objects.

Key Concepts of OOP:

  1. Object: An entity with two main attributes:
    • State or property (e.g., name, health, level)
    • Behavior (e.g., attack, defend)

Real-Life Examples of Objects:

  • Camera, microphone, laptop, etc.
  • In gaming: a hero (e.g., Paul from Tekken) with properties such as health (70%) and level (A, B, C, D).

Benefits of OOP:

  • Readability: Easier to understand the code as it mirrors real-world entities.
  • Manageability: Simplifies program structure.
  • Extensibility: Facilitates code reuse and extension.

Classes and Objects

  • A class is a user-defined data type that defines the properties and behaviors of an object.
  • An object is an instance of a class.

Example of Defining a Class:

class Hero { char name[100]; int health; char level; };

Creating an Object:

Hero h1;

Accessing Object Properties:

  • Use the dot operator . to access properties:
    • h1.health
    • h1.level

Access Modifiers:

  • Public: Accessible from outside the class.
  • Private: Accessible only within the class (default).
  • Protected: Accessible in the class and by derived classes.

Main Functions in OOP:

  1. Getter/Setter: Used to access private data members.
    • Example of Getter:
    int getHealth() { return health; }
    • Example of Setter:
    void setHealth(int h) { health = h; }

Constructors and Destructors:

  • Constructor: A special function called during object creation. Can be default or parameterized.
  • Destructor: Called when an object goes out of scope. Used to free memory.
  • Syntax for Destructor:
~Hero() { // Cleanup code }

Dynamic Memory Allocation:

  • Creating objects using new for dynamic allocation.
  • Example:
Hero* h = new Hero();

Copy Constructor:

  • Used to create a copy of an object.
  • Should be passed by reference to avoid infinite loops.
  • Example:
Hero(const Hero &temp) { health = temp.health; level = temp.level; }

Shallow vs Deep Copy:

  • Shallow Copy: Copies memory addresses, might lead to shared states.
  • Deep Copy: Creates a new memory allocation and copies values.

Static Members in Classes:

  • Static members belong to the class rather than any object instance.
  • Can be accessed without creating an object:
cout << Hero::timeToComplete << endl;

Homework:

  • Research on the const keyword, initialization lists, and how to create const-type member functions.

Upcoming Topics:

  • Next class will cover the four pillars of OOP: inheritance, polymorphism, encapsulation, and abstraction.

Additional Resources:

  • Documentation and theory available on Code Studio.

Conclusion:

  • This lecture has provided foundational knowledge on OOP concepts, including classes, objects, access modifiers, constructors, destructors, and more.
  • Look out for the next lecture in the series.