您的位置:首页 > 编程语言 > C语言/C++

C++的函数模板

2016-11-11 23:37 232 查看
直接上代码,下面是写的关于C++的一个很经典的函数模板方面的问题
#include<iostream>
using namespace std;

template <class T>

T max(T a, T b, T c)
{
if (b > a)
a = b;
if (c > a)
a = c;
return a;
}
int main()
{
int i1 = 185, i2 = 76, i3 = 567;
double d1 = 56.84, d2 = 57.21, d3 = 45.33;
long g1 = 67854, g2 = -912564, g3 = 673456;

cout << "i_max = " << max(i1, i2, i3) << endl;
cout << "i_max = " << max(d1, d2, d3) << endl;
cout << "i_max = " << max(g1, g2, g3) << endl;
getchar();
getchar();
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;

class Student
{
private:
int num;
char name[20];
char sex;
public:
void set_data(int n, char *p, char a);
void display();

};

void Student::set_data(int n, char *p, char a)
{
num = n;
strcpy_s(name, strlen(p) + 1,p);
sex = a;
}

void Student::display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
int main()
{
Student stud1, stud2;
stud1.set_data(1, "he", 'f');
stud2.set_data(2, "she", 'm');
stud1.display();
stud2.display();
system("pause");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: