Initialize an object in C++
This post will discuss how to initialize an object in C++.
1. Using default constructor
We can use the default constructor to construct an object of a class, with all its fields initialized with default values. For example, the following code implicitly invokes the default constructor of the Point class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> class Point { public: int x, y; }; int main() { Point p; std::cout << "(" << p.x << ", " << p.y << ")\n"; return 0; } |
Output:
(0, 0)
The following code explicitly invokes the default constructor of the class. Note that this is not an assignment, and the operator= is not called here.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> class Point { public: int x, y; }; int main() { Point p = Point(); std::cout << "(" << p.x << ", " << p.y << ")\n"; return 0; } |
Output:
(0, 0)
2. Using user-defined constructor
Another option is to provide the user-defined constructor and invoke it using the value initialization assignment syntax. This is the preferred option when one or more fields of the class need to be initialized with some custom value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> class Point { public: int x, y; // constructor Point() {} Point(int x, int y): x(x), y(y) {} }; int main() { Point p = Point(1, 1); std::cout << "(" << p.x << ", " << p.y << ")\n"; return 0; } |
Output:
(1, 1)
The above code is equivalent to the following:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> class Point { public: int x, y; // constructor Point() {} Point(int x, int y): x(x), y(y) {} }; int main() { Point p(1, 1); std::cout << "(" << p.x << ", " << p.y << ")\n"; return 0; } |
Output:
(1, 1)
3. Using member initialization
The following syntax calls the corresponding constructor if available. If no constructor is available, it simply performs member initialization.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> class Point { public: int x, y; }; int main() { Point p {1, 1}; std::cout << "(" << p.x << ", " << p.y << ")\n"; return 0; } |
Output:
(1, 1)
4. Using Pointers
To dynamically allocate memory for an object and initialize it with the supplied or the default values, use the new operator. You can do something like using pointers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> class Point { public: int x, y; // constructor Point() {} Point(int x, int y): x(x), y(y) {} }; int main() { Point* p = new Point(1, 1); std::cout << "(" << p->x << ", " << p->y << ")\n"; return 0; } |
Output:
(1, 1)
In modern C++, switch to smart pointers from the C++ Standard Library to ensure that programs are free of memory leaks and are exception-safe. The following example shows how a std::make_unique pointer could be used to initialize an object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <memory> class Point { public: int x, y; // constructor Point() {} Point(int x, int y): x(x), y(y) {} }; int main() { std::unique_ptr<Point> p = std::make_unique<Point>(1, 1); std::cout << "(" << p->x << ", " << p->y << ")\n"; return 0; } |
Output:
(1, 1)
That’s all about initializing an object in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)