C++的函数模板

来源:互联网 发布:scratch软件 编辑:程序博客网 时间:2024/06/05 23:45
直接上代码,下面是写的关于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;}


0 0
原创粉丝点击