C++ 泛型编程实现复数类

1. 所有函数都写在类的内部

#include <iostream>
using namespace std;

//所有函数都写在类的内部没有出问题
template <typename T>
class Complex
{
	
	friend ostream& operator<<(ostream& out, Complex& c)
	{
		out << c.a << " + " << c.b << "i" << endl;
		return out;
	}
	friend Complex& MySub(Complex& c1, Complex& c2)
	{
		Complex tmp = Complex(c1.a - c2.a, c1.b - c2.b);
		return tmp;
	}
public:
	Complex(T a, T b)
	{
		this->a = a;
		this->b = b;
	}
	Complex operator + (Complex & c2)
	{
		Complex tmp = Complex(a + c2.a, b + c2.b);
		return tmp;
	}

private:
	T a;
	T b;
};

int main()
{
	Complex<int> c1(3, 2), c2(4, 5);
	Complex<int> c3 = MySub(c1, c2);
	Complex<int> c4 = c1 + c2;
	cout << c4 << endl;
	cout << c3 << endl;

	return 0;
}

2. 所有函数都写在类的外部(同一cpp中)

#include <iostream>
using namespace std;

//所有函数都写在类的内部没有出问题
template <typename T>
class Complex
{
	friend ostream& operator<< <T>(ostream& out, Complex& c);//特别注意
public:
	Complex(T a, T b);
	Complex operator + (Complex& c2);
	Complex MySub(Complex& c);

private:
	T a;
	T b;
};

template<typename T>
Complex<T>::Complex(T a, T b)
{
	this->a = a;
	this->b = b;
}

template <typename T>
Complex<T> Complex<T>::operator+(Complex<T>& c2)
{
	Complex tmp(a + c2.a, b + c2.b);
	return tmp;
}

template<typename T>
Complex<T> Complex<T>::MySub(Complex& c)
{
	Complex tmp(a - c.a, b - c.b);
	return tmp;
}

template <typename T>
ostream& operator<<(ostream& out, Complex<T>& c)
{
	out << c.a << " + " << c.b << "i" << endl;
	return out;
}

int main()
{
	Complex<int> c1(3, 2), c2(4, 5);
	Complex<int> c3 = c1.MySub(c2);
	Complex<int> c4 = c1 + c2;
	cout << c4 << endl;
	cout << c3 << endl;

	return 0;
}

特别需要注意,在类外部实现重载<<时,要加上声明<T>,这是C++编译器的一个弊端,但始终没有修复

3. 所有函数都写在类的外部(.h和.cpp分开)

复数类_所有函数写在类的外部(h和cpp分开).h

#include <iostream>
using namespace std;

template <typename T>
class Complex
{
	friend ostream& operator<< <T>(ostream& out, Complex& c);//特别注意
public:
	Complex(T a, T b);
	Complex operator + (Complex& c2);
	Complex MySub(Complex& c);

private:
	T a;
	T b;
};

复数类_所有函数写在类的外部(h和cpp分开).hpp

#pragma once
#include <iostream>
#include "复数类_所有函数写在类的外部(h和cpp分开).h"
using namespace std;

template<typename T>
Complex<T>::Complex(T a, T b)
{
	this->a = a;
	this->b = b;
}

template <typename T>
Complex<T> Complex<T>::operator+(Complex<T>& c2)
{
	Complex tmp(a + c2.a, b + c2.b);
	return tmp;
}

template<typename T>
Complex<T> Complex<T>::MySub(Complex& c)
{
	Complex tmp(a - c.a, b - c.b);
	return tmp;
}

template <typename T>
ostream& operator<<(ostream& out, Complex<T>& c)
{
	out << c.a << " + " << c.b << "i" << endl;
	return out;
}

dm09_复数类_main.cpp

#pragma once
#include <iostream>
#include "复数类_所有函数写在类的外部(h和cpp分开).hpp"
using namespace std;

int main()
{
	Complex<int> c1(3, 2), c2(4, 5);
	Complex<int> c3 = c1.MySub(c2);
	Complex<int> c4 = c1 + c2;
	cout << c4 << endl;
	cout << c3 << endl;

	return 0;
}

特别需要注意,包含头文件时必须包含实现的函数体.hpp(不成文规定)。
原因:二次编译惹的祸。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

banjitino

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

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

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

打赏作者

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

抵扣说明:

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

余额充值