How to Define the Constructor Outside the Class in C++? Last Updated : 23 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report A constructor is a special type of member function whose task is to initialize the objects of its class. It has no return type so can't use the return keyword and it is implicitly invoked when the object is created. Constructor is also used to solve the problem of initialization. It is called after the object is created.Same name as of class. Constructor Defined Outside the Class The constructor can be defined outside the class but it has to be declared inside the class. Here, you will use the scope resolution operator. The syntax for constructor definition outside class: class class_name { public: class_name();}; // Constructor definition outside Classclass_name::class_name() {} Example: C++ // C++ program to define // constructor outside the // class #include <iostream> using namespace std; class GeeksForGeeks { public: int x, y; // Constructor declaration GeeksForGeeks(int, int); // Function to print values void show_x_y() { cout << x << " " << y << endl; } }; // Constructor definition GeeksForGeeks::GeeksForGeeks(int a, int b) { x = a; y = b; cout << "Constructor called" << endl; } // Driver code int main() { GeeksForGeeks obj(2, 3); obj.show_x_y(); return 0; } OutputConstructor called 2 3 Time complexity: O(1)Auxiliary Space: O(1) Reasons to Define the Constructor Outside the Class The following are some of the reasons why it is preferable to define constructors outside the class: No Compile-Time Dependency: One can put the class definition in the header file and the constructor definition in an implementation file that will be compiled.Readability and Cleaner Code: The main reason is to define the constructor outside the class is for readability. Since one can separate declarations into the header files and the implementations into the source files. Example: GeeksForGeeks.h C++ // Header file // Save this code with .h extension // For example- GeeksForGeeks.h #include <bits/stdc++.h> using namespace std; class ClassG { public: int a, b; // Constructor declaration ClassG(); // Function to print values void show() { cout << a << " " << b; } }; Time complexity: O(1)Auxiliary Space: O(1) File: geek.cpp C++ // Adding GeeksForGeeks.h File #include"GeeksForGeeks.h" #include<bits/stdc++.h> using namespace std; // Constructor definition ClassG::ClassG() { a = 45; b = 23; } // Driver code int main() { // This will call the // constructor ClassG obj; // Function call obj.show(); } Output: 45 23 Time complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Passing a Vector to Constructor in C++ S sameerhakeentc2019 Follow Improve Article Tags : C++ cpp-constructor Practice Tags : CPP Similar Reads How to create a List with Constructor in C++ STL Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, list has slow traversal, but once a position has been found, insertion and deletion are quick. Normally, when we say a List, we talk about doubly linked list. For implementing a singly linked list, we u 2 min read When Should We Write Our Own Copy Constructor in C++? A copy constructor is a member function that initializes an object using another object of the same class. (See this article for reference).When should we write our own copy constructor?C++ compiler provides a default copy constructor (and assignment operator) with class. When we don't provide an im 2 min read Calling a Super Class Constructor in Python Classes are like creating a blueprint for an object. If we want to build a building then we must have the blueprint for that, like how many rooms will be there, its dimensions and many more, so here the actual building is an object and blueprint of the building is a class. A Class is a user-defined 4 min read Constructor in Multiple Inheritance in C++ Constructor is a class member function with the same name as the class. The main job of the constructor is to allocate memory for class objects. Constructor is automatically called when the object is created. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can derive fro 2 min read Passing a Vector to Constructor in C++ Just like any other data, we can also pass a vector to a constructor of the desired class. We can pass it by value or by reference. What we do with it depends on our requirement. The following are some of the common cases:Copy Data to Member VectorIf we want to store the copy of the vector in the cl 3 min read When is a Copy Constructor Called in C++? A copy constructor is a member function that initializes an object using another object of the same class. The Copy constructor is called mainly when a new object is created from an existing object, as a copy of the existing object. In C++, a Copy Constructor may be called for the following cases: 1 2 min read Like