TeachMate
My ProgressJoin Session

Object-Oriented Programming

0/14 completed · ~1h 2m

All Topics
C++LearnObject-Oriented ProgrammingSyllabus
1 / 14

Introduction to OOP

intermediate
3 min read

What you'll learn

  • Model an entity as a class with data and behavior.
  • Create objects and call their methods.
  • Name the four pillars of OOP.

Object-Oriented Programming (OOP) organizes code around objects that combine data and behavior.

Problem

A college system stores a student's name, age, and marks and a teacher's name, age, and subject, then acts on them: enroll a student, record marks, teach a class. If the data lives in loose variables and the actions in loose functions, the two drift apart.

Goal: Keep each entity's data and the actions on it together in one named type, instead of loose variables and loose functions.

StudentTeacher
namename
ageage
markssubject

A student and a teacher both have data (name, age) and actions (enroll, teach). Keeping the data and the actions together, instead of in loose variables and functions, is what a class does. Once you see that, the syntax below is just how you write it.

Understand

Take something you use every day and list its data and the actions you perform on it. Then decide what a class for it would store and what its methods would do.

Before writing classes, have students trace a small program by hand — predict each variable's value line by line. Reading code carefully is the skill OOP builds on.

Four Pillars of OOP

  1. Encapsulation: bundling data and methods, hiding internal details
  2. Inheritance: creating new classes from existing ones
  3. Polymorphism: same interface, different implementations
  4. Abstraction: exposing only essential features

Why OOP?

  • Models real-world concepts naturally
  • Promotes code reuse
  • Makes large programs manageable
  • Enables polymorphic behavior

Key Concepts

  • Class: A blueprint (template) for creating objects
  • Object: An instance of a class
  • Member variables: Data stored in the object
  • Member functions: Operations the object can perform

Example: Car Analogy

  • Class: Car (the blueprint)
  • Objects: My car, your car, that car (instances)
  • Attributes: color, speed, fuel (member variables)
  • Behaviors: accelerate, brake, turn (member functions)

Before OOP

struct Student { string name; int age; };
void printStudent(Student s) { ... }

After OOP

class Student {
    string name;
    int age;
public:
    void print() { ... }
};

OOP is not always the answer. For simple programs, procedural code is fine. Use OOP when you need to model complex relationships and behaviors.

Key takeaway: OOP models real-world entities as objects with data and behavior — use it when your program needs to manage complex relationships between entities.

Try it: Pick three real-world objects (a student, a book, a bank account) and list the data and behaviors each would have as a class.

TeachMate - Interactive learning platform for bachelor students

HomeLearnJoin Session|2 Subjects