C++ 模板类

ClassTemplate.h


#pragma once

template <typename T1,typename T2>
class myClass
{
private :
	T1 x;
	T2 y;

public:
	myClass(T1 a,T2 b);   //构造函数
	void show();
};

template<typename T1,typename T2>
myClass<T1,T2>::myClass(T1 a,T2 b):x(a),y(b){}

template<typename T1,typename T2>
void myClass<T1,T2>::show()
{
	cout<<"x= "<<x<<"y= "<<y<<endl;
}



main.cpp

#include<iostream>
#include<Windows.h>
#include<string>
#include"ClassTemplate.h"
using namespace std;

int main()
{
	myClass<int,int> class1(1,2);
	class1.show();

	myClass<float,int> class2(0.12345,3);
	class1.show();

	myClass<char,string> class3('a',"helloworld");
	class3.show();

	system("pause");
	return 0;
}





你可能感兴趣的:(C++ 模板类)