what is ecplicit constructor in c++ code example
Example: constructor in c++
#include <iostream>
class Entity {
public:
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 {
public:
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 {
private:
/*
DefaultC() {
//making default constructor private will not construct an instantiate an object
};
*/
public:
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
std::cin.get();
}