Check out example codes for "constructor in c++". It will help you in understanding the concepts better.

Code Example 1

// Cpp program to illustrate the 
// concept of Constructors 
#include <iostream> 
using namespace std; 
class construct { 
    int a, b; 
    // Default Constructor 
        a = 10; 
        b = 20; 
int main() 
    // Default constructor called automatically 
    // when the object is created 
    construct c; 
    cout << "a: " << c.a << endl 
         << "b: " << c.b; 
    return 1; 

Code Example 2

#include <iostream>
class Entity {
	float x, y;
	void Print() {
		std::cout << "( " << x << " , " << y << " )" << std::endl;
	void Initialize() {
		x = 0.0f;
		y = 0.0f;
	}//This method initialize x and y to 0

	Entity() {
		//A constructor is a function that will get called everytime you intantiate a class
		//1) A constructor function looks like this name of function  must be == to classname 
		//2) This function must not return anything 
		x = 0.f; y = 0.0f;//constructor will initalize the x and y to 0 when we will create an instance of class
		//we can use constructor member initializer list to initialize our variables which is better than using constructor check that out
//2 using constructor with parameters 
class Entity2 {
	float X, Y;
	Entity2(float x, float y) {
		X = x;
		Y = y;
		std::cout << "this is x: " << x << " this is y: " << y << std::endl;
		//Using constructor with parameters will create an instance of object with these parameters and output x an y
class DefaultC {
	DefaultC() {
		//making default constructor private will not construct an instantiate an object
	int a;
	DefaultC() {
		//This is a default construtor that creates an object without prameters  and if make it private or delete it we ca'nt construct the object
		T// This is default constructor and gets called always when you create object with no parameters and deleting it or making it private you will not be able to construct an object
	DefaultC() = delete; //making default constructor private will not construct an instantiate an object
int main() {
	Entity e;
	e.Print();//output => because Printing the uninitialised memory (-1.07374e+08, -1.07374e+08)
	//std::cout << e.x << std::endl; // Error => uninitialised local variable e used

	//Instead we can call Initialize method that will initialize to set x and y to 0.0
	//this will work but is not the best way 
	Entity e1;
	e1.Initialize();// using this method to initialize x and y
	e1.Print();// output => (0,0)
	std::cout << e1.x << std::endl;// output => 0

	//But the above is no a good practice we can use a constructor instead
	//A constructor is a function that will get called everytime we instantiate an object
	Entity e2;
	e2.Print();//output => (0,0)
	std::cout << e2.x << std::endl;//output => 0
    // This time we don't have to call any method the constructor initialized our x and y var on inistantiating an object
	std::cout << "=================================================" << std::endl;
	Entity2 entity(2.2f,3.3f); // create entity with parameters and will output x and y
	//Entity2 entity2;  //error =>	no default constructor exists for class "Entity2"	 this is because we have not specified the default constructor we specified a constructor that will take parameters x and y and will construct an object on these parameters
	//Default constructor is expalined below
	DefaultC C;// error =>  Default C constructor is inaccessible because we deleted it or made it private
	//To avoid this just the delete line and the private constructor and  you will bee good


Code Example 3

class MyClass {     // The class
  // Access specifier
    MyClass() {     
  // Constructor
      cout << "Hello World!";

int main() {
  MyClass myObj;    // Create an object of MyClass (this will call 
  the constructor)
  return 0;

Code Example 4

public enum Day {


    int value;

    private Day(int value) {
        System.out.println("arg cons");
        this.value = value;

    private Day() {
        System.out.println("no arg cons");

    public static void main(String args[]) {



Code Example 5

class Other{
    public Other(String message){

class scratch{
    public static void main(String[] args) {
        Other method = new Other("Hey");
        //prints Hey to the console

Learn ReactJs, React Native from