Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Classes
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 22 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++IntermediateLesson 22 of 50CamboFreelanceJune 19, 2026

C++ Classes

Introduction to C++ classes — the blueprint of OOP. Learn to define classes, add attributes and methods, and create objects.


Tutorials › C++ › Classes
Intermediate8 min readLesson 22 of 50

In This Lesson

  • Defining a Class
  • Members & Methods
  • Access Specifiers
  • Quiz

What is a Class?

A class is a user-defined blueprint that bundles data (attributes) and behavior (methods) into one unit. Classes are the foundation of Object-Oriented Programming (OOP).

class ClassName {
  public:          // access specifier
    dataType attribute;
    returnType method() { }
};

Defining and Using a Class

#include <iostream>
using namespace std;

class Car {
  public:
    string brand;
    string model;
    int    year;

    void honk() {
        cout << brand << " goes Beep!" << endl;
    }
};

int main() {
    Car myCar;
    myCar.brand = "Toyota";
    myCar.model = "Corolla";
    myCar.year  = 2022;

    cout << myCar.brand << " " << myCar.model << " (" << myCar.year << ")" << endl;
    myCar.honk();
    return 0;
}

Output:

Toyota Corolla (2022)
Toyota goes Beep!
Note: The class definition ends with a semicolon ; — this is a common mistake to forget!

Exercise

Create a Person class with attributes name (string) and age (int), and a method introduce() that prints "Hi, I'm [name] and I'm [age] years old." Create two Person objects and call their method.

Show Solution
#include <iostream>
using namespace std;

class Person {
  public:
    string name;
    int    age;
    void introduce() {
        cout << "Hi, I'm " << name << " and I'm " << age << " years old." << endl;
    }
};

int main() {
    Person p1; p1.name = "Alice"; p1.age = 28; p1.introduce();
    Person p2; p2.name = "Bob";   p2.age = 34; p2.introduce();
    return 0;
}

Quiz

  1. What does a class definition end with?

    • A) A closing brace only
    • B) A semicolon after the closing brace
    • C) A period
    • D) The keyword end
    Answer

    B) A semicolon — };

  2. What term describes the functions defined inside a class?

    • A) Properties
    • B) Attributes
    • C) Methods
    • D) Modules
    Answer

    C) Methods

Summary

  • A class is a blueprint combining data and behavior.
  • Class members are accessed using the dot operator ..
  • Always end a class definition with };.
← Previous: Pointers Next: C++ Objects →
c++classesoopintermediate
PreviousLesson 21: C++ PointersNextLesson 23: C++ Objects
Back to All Tutorials