C++: Exemple d’une fonction générique qui peut afficher tout type des données

Author:


Download

	#include 
	using namespace std;

template	class Afficher
	{
	public:
	void operator()(const T& t)
	{
		cout << t << " ";
	}

	};

	int main()
	{
		Afficher objAfficher;

		/* Affichage des entiers */
		for (int i = 0; i < 5; ++i) objAfficher(i);
        cout<<"n";
		Afficher objAfficher2;
		/* Affichage des Doubles */
		for (double x = 0; x < 5; ++x) objAfficher2(x);
        cout<<"n";
		Afficher objAfficher3;
		/* Affichage des caractères */
		for (double j = 0; j < 5; ++j)
		objAfficher3(j+'a');

		return 0;
	}

Leave a Reply

Your email address will not be published. Required fields are marked *