感谢内容提供者:金牛区吴迪软件开发工作室
接上一篇:C++程序设计【八】之 文件操作
第九章:函数模板与类模板
一、函数模板
1.函数模板的概念
2.函数模板的示例
#include<iostream>
using namespace std;
template<typename T>
T abs(T x) {
return x < 0 ? -x : x;
}
int main() {
int n = -5;
int m = 10;
double d = -.5;
float f = 3.2;
cout << n << "的绝对值是:" << abs(n) << endl;
cout << m << "的绝对值是:" << abs(m) << endl;
cout << d << "的绝对值是:" << abs(d) << endl;
cout << f << "的绝对值是:" << abs(f) << endl;
return 0;
}
#include<iostream>
using namespace std;
template<class T>
void Swap(T &x, T &y) {
T tmp = x;
x = y;
y = tmp;
}
class myDate {
public:
myDate();
myDate(int, int, int);
void printDate() const;
private:
int year, month, day;
};
myDate::myDate() {
year = 1970;
month = 1;
day = 1;
}
myDate::myDate(int y, int m, int d) {
year = y;
month = m;
day = d;
}
void myDate::printDate() const {
cout << year << "/" << month << "/" << day << endl;
return;
}
int main() {
int n = 1, m = 2;
// 编译器自动生成void Swap(int &, int &)函数
Swap(n, m);
cout << n << " " << m << endl;
double f = 1.2, g = 2.3;
// 编译器自动生成void Swap(double &, double &)函数
Swap(f, g);
myDate d1, d2(2000, 2, 2); // 创建俩个对象
// 编译器自动生成void Swap(myDate &, myDate &)函数
Swap(d1, d2);
d1.printDate();
d2.printDate();
return 0;
}
#include<iostream>
#include<string>
using namespace std;
template<typename T>
int myCompare(const T &left, const T &right) {
if (left < right) {
return -1;
} else if (right < left) {
return 1;
} else {
return 0;
}
}
template<class T>
void Swap(T &x, T &y) {
T tmp = x;
x = y;
y = tmp;
}
int main() {
string arraystring[10] = {
"shang", "xia", "zuo", "you", "qian", "hou", "dong", "xi", "nan", "bei"};
int j;
string temp;
for (int i = 1; i < 10; i++) {
j = i;
while (j > 0 && myCompare<string>(arraystring[j - 1], arraystring[j]) > 0) {
swap(arraystring[j], arraystring[j - 1]);
j--;
}
}
for (int i = 0; i < 10; i++) {
cout << arraystring[i] << ",";
}
return 0;
}
3.函数或函数模板调用语句的匹配顺序
二、类模板
1.类模板概念
2.类模板示例
#include<iostream>
using namespace std;
template<class T>
class TestClass {
public:
T buffer[10]; // T类的数据成员buffer数组大小固定为10
T getData(int j); // 获取T类buffer(数组)的第j个分量
};
template<class T>
T TestClass<T>::getData(int j) {
return *(buffer + j);
}
int main() {
TestClass<char> ClassInstA; // char取代T,从而实例化为一个具体的类
int i;
char cArr[6] = "abcde";
for (i = 0; i < 5; i++) {
ClassInstA.buffer[i] = cArr[i];
}
for (i = 0; i < 5; i++) {
char res = ClassInstA.getData(i);
cout << res << " ";
}
cout << endl;
TestClass<double> ClassInstF; // 实例化为另外一个具体的类
double fArr[6] = {
12.1, 23.2, 34.3, 45.4, 56.5, 67.6};
for (i = 0; i < 6; i++) {
ClassInstF.buffer[i] = fArr[i] - 10;
}
for (i = 0; i < 6; i++) {
double res = ClassInstF.getData(i);
cout << res << " ";
}
return 0;
}
3.类模板与继承
#include<iostream>
using namespace std;
template<class T> // 类模板,基类
class TBase {
T data;
public:
void print() {
cout << data << endl;
}
};
class Derived:public TBase<int> {
}; // 从模板继承,普通类
int main() {
Derived d; // 普通派生类的对象
d.print(); // 调用类模板中的成员函数
return 0;
}
#include<iostream>
using namespace std;
class TBase {
int k;
public:
void print() {
cout << "TBase::" << k << endl;
}
};
template<class T>
class TDerived:public TBase {
T data;
public:
void setData(T x) {
data = x;
}
void print() {
TBase::print();
cout << "TDerived::" << data << endl;
}
};
int main() {
TDerived<string>d;
d.setData("2019");
d.print();
return 0;
}
#include<iostream>
using namespace std;
template<class T>
class TBase {
public:
T data1;
void print() {
cout << "TBase::" << data1 << endl;
}
};
template<class T1, class T2>
class TDerived : public TBase<T1> {
public:
T2 data2;
void print() {
TBase<T1>::print();
cout << "TDerived::" << data2 << endl;
}
};
int main() {
TDerived<int, int> d; // 类模板实例化,并声明对象
d.data1 = 5;
d.data2 = 8;
d.print();
TDerived<string, string> d2;
d2.data1 = "happy";
d2.data2 = "new year";
d2.print();
TDerived<int, string> d1;
d1.data1 = 2021;
d1.data2 = "good luck";
d1.print();
return 0;
}
转载:https://blog.csdn.net/weixin_43606158/article/details/114260389
查看评论