C++模块类与operator学习笔记

C++模块类基本用法为

template <class type>
class test
{
public:
       test(type data);
private:
       type data;
}
operator用于操作符重载
const test& operator=(const test&);
friend ostream& operator<<(ostream&, const test&);//此处为友原重载输出流


下面给出一个具体的代码实现

#include <map>
#include <utility>
#include <vector>
#include <iostream>
#include <string>
using namespace std;

template <class Type1,class Type2>
class pair1
{
	template<class Type1,class Type2>
	friend ostream& operator<<(ostream& out,const pair1<Type1,Type2>& t);
public:
	pair1()
	{
	}
	pair1(Type1 data1, Type2 data2)
	{
		first_parameter = data1;
		second_parameter = data2;
	}
	~pair1();
	const pair1& operator=(const pair1<Type1,Type2>&);
private:
	Type1 first_parameter;
	Type2 second_parameter;

};

template<class Type1, class Type2>
pair1<Type1,Type2>::~pair1()
{

}

template<class Type1, class Type2>
const pair1<Type1,Type2> &pair1<Type1,Type2>::operator=(const pair1& t)
{
	
	first_parameter = t.first_parameter;
	second_parameter = t.second_parameter;
	return *this;
}

template<class Type1, class Type2>
ostream& operator<<(ostream& out,const pair1<Type1,Type2>& t)
{
	out << "(" << t.first_parameter << ", ";
	out << t.second_parameter << ")" << endl;
	return out;
}

int main()
{
	pair1 <string,int> x ("num:",2);
	pair1 <string,int> y;
	pair1 <string,int> z("b",3);
	y = z;
	cout << x;
	cout << y;
	system("pause");
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值