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++ Objects
⚡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 23 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 23 of 50CamboFreelanceJune 19, 2026

C++ Objects

Learn how to create and use C++ objects. Understand the relationship between classes and objects, and the this pointer.


Tutorials › C++ › Objects
Intermediate7 min readLesson 23 of 50

In This Lesson

  • Creating Objects
  • Multiple Objects
  • Calling Methods
  • Quiz

Creating Objects

An object is an instance of a class. You can create many objects from the same class.

#include <iostream>
using namespace std;

class Rectangle {
  public:
    double width;
    double height;

    double area()      { return width * height; }
    double perimeter() { return 2 * (width + height); }
};

int main() {
    Rectangle r1, r2;

    r1.width = 5.0; r1.height = 3.0;
    r2.width = 8.5; r2.height = 4.2;

    cout << "R1 Area: "      << r1.area()      << endl;
    cout << "R1 Perimeter: " << r1.perimeter() << endl;
    cout << "R2 Area: "      << r2.area()      << endl;
    return 0;
}

Output:

R1 Area: 15
R1 Perimeter: 16
R2 Area: 35.7

The this Pointer

Inside a method, this is a pointer to the current object:

class Box {
  public:
    int size;
    void setSize(int size) {
        this->size = size;   // 'this->size' is the member; 'size' is the parameter
    }
};

Exercise

Create a BankAccount class with owner (string), balance (double). Add methods deposit(double), withdraw(double), and printBalance(). Test with two accounts.

Show Solution
#include <iostream>
using namespace std;

class BankAccount {
  public:
    string owner;
    double balance;
    void deposit(double amount)  { balance += amount; }
    void withdraw(double amount) { if (amount <= balance) balance -= amount; }
    void printBalance()          { cout << owner << ": $" << balance << endl; }
};

int main() {
    BankAccount acc;
    acc.owner = "Alice"; acc.balance = 1000.0;
    acc.deposit(500);
    acc.withdraw(200);
    acc.printBalance();   // Alice: $1300
    return 0;
}

Quiz

  1. What is the relationship between a class and an object?

    • A) They are the same thing
    • B) A class is an instance of an object
    • C) An object is an instance of a class
    • D) Objects contain classes
    Answer

    C) A class is the blueprint; an object is a concrete instance of that blueprint.

Summary

  • Objects are instances of classes created using the class name as a type.
  • Each object has its own copy of member variables.
  • this is a pointer to the current object inside a method.
← Previous: Classes Next: Constructors →
c++objectsoopintermediate
PreviousLesson 22: C++ ClassesNextLesson 24: C++ Constructors
Back to All Tutorials