C++ OOP Introduction

Object-oriented programming, often shortened to OOP, is a way to organize code around objects. An object can store data and also provide functions that work with that data.

C++ supports OOP through features such as classes, objects, access control, constructors, inheritance, and polymorphism. This lesson introduces the idea before the next lessons look at the syntax in more detail.

Why OOP Is Useful

As programs grow, keeping all data and functions separate can become hard to manage. OOP helps by grouping related information and behavior into one unit.

For example, a program about students might need each student to have a name, an age, and a way to print a short profile. With OOP, those pieces can belong together in a Student type instead of being scattered across unrelated variables and functions.

Classes and Objects

A class is a blueprint for a type of object. It describes what data the object can store and what functions it can use.

An object is a real value created from a class. If a class is the blueprint, an object is one actual item built from that blueprint.

  • class Student describes what a student object has and can do.
  • Student first; creates one object from that class.
  • Different objects from the same class can store different values.

A First OOP Example

This program defines a simple Student class. The class stores data in member variables and uses a member function to print that data.

#include <iostream>
#include <string>

class Student {
public:
    std::string name;
    int age;

    void printProfile() {
        std::cout << name << " is " << age << " years old." << std::endl;
    }
};

int main(void) {
    Student first;
    first.name = "Maya";
    first.age = 20;

    Student second;
    second.name = "Leo";
    second.age = 22;

    first.printProfile();
    second.printProfile();

    return 0;
}

Output:

Maya is 20 years old.
Leo is 22 years old.

How The Example Works

The line class Student starts the class definition. Inside the class, name and age are member variables. Each Student object gets its own copy of those variables.

The keyword public means the following members can be accessed from outside the class. That is why main can assign values with first.name and first.age.

The function printProfile is a member function, also called a method. It belongs to the class and can use the object’s member variables directly.

The dot operator, ., accesses a member of an object. In first.printProfile();, the program calls the method for the object named first. In second.printProfile();, the same method runs again, but it uses the data stored in second.

Main OOP Ideas

C++ OOP is built around a few important ideas. You do not need to master them all at once, but the names will appear often.

  • Encapsulation means keeping data and the functions that use it together.
  • Abstraction means using an object through clear operations without needing to know every internal detail.
  • Inheritance lets one class build on another class.
  • Polymorphism lets related objects respond to the same operation in different ways.

When To Use OOP

OOP is helpful when your program has things with state and behavior, such as bank accounts, game characters, employees, products, or shapes. It is less useful for very small programs where a few variables and functions are enough.

Takeaway: OOP in C++ lets you create your own types by grouping data and related functions into classes, then using objects made from those classes.