自定义类模板的简单实践

目录

T1:

题目要求:

代码实现:

T2:模板中的运算符重载

题目要求:

代码实现:

1.0:

2.0: 

 3.0:

 T3:

题目要求:

代码实现:


 

T1:

题目要求:

Use an int template nontype parameter numberOfElements and a type parameter
elementType to help create a template for the Array class. This template will enable
Array objects to be instantiated with a specified number of elements of a specified
element 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 its
two arguments of the same type with the equality operator (==) and returns true if
they are equal and false if they are not equal.
Use this function template in a program that calls isEqualTo only with a variety of
built-in types.

 

  Now write a separate version of the program that calls isEqualTo with a
user-defined class type Complex, but does not overload the equality operator. What
happens when you attempt to run this program?
  Now overload the equality operator (with the operator function) operator ==. Now
what 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 enable
input 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;
}

感谢这位大佬博客的讲解

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ItsNorth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值