Saltar al contenido

constructor predeterminado en c ++ con ejemplos

Nuestro team de especialistas pasados algunos días de investigación y recopilación de de información, hemos dado con la respuesta, esperamos que te sea útil para tu trabajo.

Ejemplo: constructor predeterminado en c ++

#includeclassEntitypublic:float x, y;voidPrint()
		std::cout <<"( "<< x <<" , "<< y <<" )"<< std::endl;voidInitialize()
		x =0.0f;
		y =0.0f;//This method initialize x and y to 0Entity()//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 classEntity2public: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;;classDefaultCprivate:/*
	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;intmain()
	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();

Más adelante puedes encontrar las crónicas de otros sys admins, tú igualmente eres capaz mostrar el tuyo si lo crees conveniente.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *