目录
T1:
题目要求:
Use an int template nontype parameter numberOfElements and a type parameterelementType to help create a template for the Array class. This template will enableArray objects to be instantiated with a specified number of elements of a specifiedelement type at compile time.
代码实现:
#include<iostream>
#include<string>
using namespace std;
template<typename T ,int size>//定义一个能显示不同大小的类模板
class Stack
{
T array[size];
int top;
public:
Stack() { top = -1; }
bool push(const T& x);
bool pop(T& x);
bool isEmpty() const { return top == -1; }
bool isFull() const
{
return top == size - 1;
}
void print();
};
template <class T, int size>
bool Stack <T, size>::push(const T& x) {
if (!isFull()) {
array[++top] = x;
return true;
}
return false;
}
template <class T, int size>
bool Stack <T, size>::pop(T& x)
{
if (!isEmpty()) {
x = array[top--];
return true;
}
return false;
}
template<typename T, int size>
void Stack <T, size>::print()
{
for (int i = 0; i <= top; i++)
cout << array[i]<<" ";
cout << endl;
}
int main()
{
cout << "Enter 5 integer values:" << endl;
Stack<int, 5>str1;
for (int i = 0; i < 5; i++)
{
int x;
cin >> x;
str1.push(x);
}
cout << "The values in the intArray are:" << endl;
str1.print();
cout << "Enter 7 one-word string values:" << endl;
Stack<string, 7>str2;
for (int i = 0; i < 7; i++)
{
string x;
cin >> x;
str2.push(x);
}
cout << "The values in the stringArray are:" << endl;
str2.print();
return 0;
}
T2:模板中的运算符重载
题目要求:
Write a simple function template for predict function isEqualTo that compares itstwo arguments of the same type with the equality operator (==) and returns true ifthey are equal and false if they are not equal.Use this function template in a program that calls isEqualTo only with a variety ofbuilt-in types.
Now write a separate version of the program that calls isEqualTo with auser-defined class type Complex, but does not overload the equality operator. Whathappens when you attempt to run this program?Now overload the equality operator (with the operator function) operator ==. Nowwhat happens when you attempt to run this program?
代码实现:
1.0:
#include<iostream>
using namespace std;
template<typename T>
bool isEqualTo(T& x, T& y)
{
if (x == y) return true;
else return false;
}
//函数模板
int main()
{
//原始版本
int a;
int b;
cout << "Enter two integer values: ";
cin >> a >> b;
cout << a << " and " << b << " are "<< (isEqualTo(a, b) ? "equal" : "not equal") << '\n';
char c;
char d;
cout << "\nEnter two character values: ";
cin >> c >> d;
cout << c << " and " << d << " are "<< (isEqualTo(c, d) ? "equal" : "not equal") << '\n';
double e;
double f;
cout << "\nEnter two double values: ";
cin >> e >> f;
cout << e << " and " << f << " are "<< (isEqualTo(e, f) ? "equal" : "not equal") << '\n';
return 0;
}
2.0:
#include<iostream>
using namespace std;
template<typename T>
bool isEqualTo(T& x, T& y)
{
if (x == y) return true;
else return false;
}
//函数模板
class Class
{
public:
int x;
double y;
Class(int xx = 0, double yy = 0) { x = xx; y = yy; }
friend istream& operator>>(istream& in, Class& z);
friend ostream& operator<<(ostream& out, const Class& m);
};
istream& operator>>(istream& in, Class& z) {
in >> z.x >> z.y;
return in;
}
ostream& operator<<(ostream& out, const Class& m) {
out << "(" << m.x << "," << m.y << ")";
return out;
}
bool isEqualTo(Class & x, Class& y)
{
if (x.x ==y.x&& y.y==x.y) return true;
else return false;
}
int main()
{
//原始版本
int a;
int b;
cout << "Enter two integer values: ";
cin >> a >> b;
cout << a << " and " << b << " are "<< (isEqualTo(a, b) ? "equal" : "not equal") << '\n';
char c;
char d;
cout << "\nEnter two character values: ";
cin >> c >> d;
cout << c << " and " << d << " are "<< (isEqualTo(c, d) ? "equal" : "not equal") << '\n';
double e;
double f;
cout << "\nEnter two double values: ";
cin >> e >> f;
cout << e << " and " << f << " are "<< (isEqualTo(e, f) ? "equal" : "not equal") << '\n';
Class g;
Class h;
cout << "\nEnter two Class values: ";
cin >> g >> h;
cout << "The class objects " << g << " and " << h << " are "<< (isEqualTo(g, h) ? "equal" : "not equal") << '\n';
return 0;
}
3.0:
#include<iostream>
using namespace std;
template<typename T>
bool isEqualTo(T& x, T& y)
{
if (x == y) return true;
else return false;
}
//函数模板
class Class
{
public:
int x;
double y;
Class(int xx = 0, double yy = 0) { x = xx; y = yy; }
bool operator==(Class& z);
friend istream& operator>>(istream& in, Class& z);
friend ostream& operator<<(ostream& out, const Class& m);
};
istream& operator>>(istream& in, Class& z) {
in >> z.x >> z.y;
return in;
}
ostream& operator<<(ostream& out, const Class& m) {
out << "(" << m.x << "," << m.y << ")";
return out;
}
bool Class:: operator==(Class& z)
{
if (z.x == x && z.y == y) return 1;
else return 0;
}
int main()
{
//原始版本
int a;
int b;
cout << "Enter two integer values: ";
cin >> a >> b;
cout << a << " and " << b << " are "<< (isEqualTo(a, b) ? "equal" : "not equal") << '\n';
char c;
char d;
cout << "\nEnter two character values: ";
cin >> c >> d;
cout << c << " and " << d << " are "<< (isEqualTo(c, d) ? "equal" : "not equal") << '\n';
double e;
double f;
cout << "\nEnter two double values: ";
cin >> e >> f;
cout << e << " and " << f << " are "<< (isEqualTo(e, f) ? "equal" : "not equal") << '\n';
Class g;
Class h;
cout << "\nEnter two Class values: ";
cin >> g >> h;
cout << "The class objects " << g << " and " << h << " are "<< (isEqualTo(g, h) ? "equal" : "not equal") << '\n';
return 0;
}
T3:
题目要求:
Define a class template called Vector(a single-column- Matrix). The templates caninstantiate a vector of any element type. Overloaded >> and << operators: to enableinput and output of a vector, respectively.
代码实现:
#include<iostream>
#include<string>
using namespace std;
template<typename T, int size>
class Vector
{
T array[size];
public:
Vector() { }
friend istream& operator>>(istream& in, Vector<T,size>& z)
{
for (int i = 0; i < size; i++)
{
in >> z.array[i];
}
return in;
}
friend ostream& operator<<(ostream& out, const Vector<T, size>& m)
{
for (int i = 0; i < size; i++)
{
out << m.array[i] << " ";
}
out << endl;
return out;
}
};
int main()
{
cout << "int:" << endl;
Vector<int, 3>str1;
cin >> str1;
cout << str1;
cout << "char:" << endl;
Vector<char, 3>str2;
cin >> str2;
cout << str2;
cout << "string:" << endl;
Vector<string, 3>str3;
cin >> str3;
cout << str3;
return 0;
}
感谢这位大佬博客的讲解