Lesson 9

Classes and Objects

Classes and objects are the foundation of object-oriented programming in C++. They help organize data and behavior together.

Example

Example
#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
    string name;
    int age;

    void display()
    {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

int main()
{
    Student s1;
    s1.name = "Alice";
    s1.age = 20;
    s1.display();

    return 0;
}
Book Tip:

This short lesson is part of a larger learning path. The full book expands each topic with more explanation, exercises, and projects.