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

C++ Constructors

Learn C++ constructors: default, parameterized, and copy constructors. Master member initializer lists for efficient initialization.


Tutorials › C++ › Constructors
Intermediate8 min readLesson 24 of 50

In This Lesson

  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor
  • Initializer List
  • Quiz

Default Constructor

A constructor is a special method that runs automatically when an object is created. It has the same name as the class and no return type.

#include <iostream>
using namespace std;

class Student {
  public:
    string name;
    int    grade;

    // Default constructor
    Student() {
        name  = "Unknown";
        grade = 0;
        cout << "Student created!" << endl;
    }
};

int main() {
    Student s;   // Constructor called automatically
    cout << s.name << ", Grade: " << s.grade << endl;
    return 0;
}

Output:

Student created!
Unknown, Grade: 0

Parameterized Constructor

class Student {
  public:
    string name;
    int    grade;

    Student(string n, int g) {
        name  = n;
        grade = g;
    }

    void display() {
        cout << name << " — Grade: " << grade << endl;
    }
};

int main() {
    Student s1("Alice", 95);
    Student s2("Bob",   82);
    s1.display();
    s2.display();
    return 0;
}

Output:

Alice — Grade: 95
Bob — Grade: 82

Member Initializer List

A more efficient way to initialize members — required for const and reference members:

class Circle {
  public:
    const double PI;
    double radius;

    Circle(double r) : PI(3.14159), radius(r) { }

    double area() { return PI * radius * radius; }
};

Circle c(5.0);
cout << c.area();   // 78.54

Exercise

Create a Product class with a parameterized constructor that sets name, price, and quantity. Add a method totalValue() that returns price × quantity.

Show Solution
#include <iostream>
using namespace std;

class Product {
  public:
    string name;
    double price;
    int    quantity;

    Product(string n, double p, int q) : name(n), price(p), quantity(q) { }

    double totalValue() { return price * quantity; }
};

int main() {
    Product p("Widget", 9.99, 50);
    cout << p.name << " total: $" << p.totalValue() << endl;
    return 0;
}

Quiz

  1. When is a constructor called?

    • A) When a method is invoked
    • B) Automatically when an object is created
    • C) When the object is destroyed
    • D) Only when explicitly called
    Answer

    B) Constructors run automatically when the object is instantiated.

  2. What is the return type of a constructor?

    • A) void
    • B) The class type
    • C) int
    • D) No return type
    Answer

    D) Constructors have no return type — not even void.

Summary

  • Constructors initialize objects when they are created.
  • The default constructor takes no arguments.
  • Parameterized constructors accept arguments to set initial values.
  • Member initializer lists are more efficient and required for const members.
← Previous: Objects Next: Access Specifiers →
c++constructorsoopintermediate
PreviousLesson 23: C++ ObjectsNextLesson 25: C++ Access Specifiers
Back to All Tutorials