C++初学笔记“模板”:矩阵运算

(后期补充!这张图可以帮助理解复制构造函数和构造函数之间的关系。) 

C++初学“模板”:矩阵运算

一道作业题,题目:

(1)定义类模板T_Counter,实现基本类型数据的 + 、 - 、 * 、 = 、 >> 、 << 运算;
(2)类模板T_Matrix,实现矩阵运算。

(题目要求很模糊,我为了少出点bug降低难度以交作业,都是写最简单的情况,其实最理想的应该就是写矩阵,n维向量,complex等都更实用)

先单看(2)的代码;

主函数:

#include"T_Matrix.h"
#include <iostream>

int main()
{
    T_Matrix<int>c1(2);
    T_Matrix<int> c2(2);
    cout << "c1+c2=" << c1 + c2 << endl;
    cout << "c1-c2=" << c1 - c2 << endl;
    cout << "c1*c2=" << c1 * c2 << endl;
}

再添加一个类模板T_Matrix:

头文件如下:

#pragma once
//只定义+,-,*运算,且只做一维向量(二维做数组套娃)
//简言之,两个矩阵相加减,即它们相同位置的元素相加减!
//(上下这两条都是运算规则说明,因为没听过矩阵运算是啥)
//注意:只有对于两个行数、列数分别相等的矩阵(即同型矩阵),加减法运算才有意义,即加减运算是可行的.
#include<iostream>
using namespace std;
template<typename T>
class T_Matrix
{
public:
	T_Matrix(int n);
	virtual ~T_Matrix();
	T_Matrix(const T_Matrix& c1);
	//重载构造函数,如果没有会执行发生错误,应该是占同一块动态内存之类的
	//且运算符时执行了4次重载构造函数,同时马上执行析构函数,而不是我想象的整个程序结束后才来3次析构;
	//已经打上函数执行提示了,可以深入继续思考内存的关系

private:
	T* mdata;
	int size=0;//数据个数
	template<typename T>//为什么都定义在private?
	friend T_Matrix<T> operator+(const T_Matrix<T>c1, const T_Matrix<T>c2);
	template<typename T>
	friend T_Matrix<T> operator-(const T_Matrix<T>c1, const T_Matrix<T>c2);
	template<typename T>
	friend T_Matrix<T> operator*(const T_Matrix<T>c1, const T_Matrix<T>c2);

	template < typename T>
	friend ostream& operator<<(ostream& os, const T_Matrix<T>& c3);

};

template<typename T>
inline T_Matrix<T>::T_Matrix(int n)
{
	if (n > 0)
		size = n;
	mdata = new T[size];
	for (int i = 0; i < n; i++)
	{
		cout << "输入第" << i+1 << "个数据:";
		cin >> mdata[i];
	}
	cout << "构造函数执行" << endl;
}

template<typename T>
inline T_Matrix<T>::~T_Matrix()
{
	delete[]mdata;
	cout << "析构函数已执行" << endl;
}

template<typename T>
inline T_Matrix<T>::T_Matrix(const T_Matrix& c1)
{
	cout << "重载构造函数" << endl;
	size = c1.size;
	mdata = new T[size];
	for (int i = 0; i < size; i++)
		mdata[i] = c1.mdata[i];
}

template<typename T>
inline T_Matrix<T> operator+(const T_Matrix<T> c1, const T_Matrix<T> c2)
{
	T_Matrix<T>c3 = c1;
	for (int i = 0; i < c3.size; i++)
	{
		c3.mdata[i] = c1.mdata[i] + c2.mdata[i];
	}
	return c3;
}

template<typename T>
inline T_Matrix<T> operator-(const T_Matrix<T> c1, const T_Matrix<T> c2)
{
	T_Matrix<T>c3 = c1;
	for (int i = 0; i < c3.size; i++)
	{
		c3.mdata[i] = c1.mdata[i] - c2.mdata[i];
	}
	return c3;
}

template<typename T>
inline T_Matrix<T> operator*(const T_Matrix<T> c1, const T_Matrix<T> c2)
{
	T_Matrix<T>c3 = c1;
	for (int i = 0; i < c3.size; i++)
	{
		c3.mdata[i] = c1.mdata[i] * c2.mdata[i];
		//A(ij)=a1(i,...)*a2(...,j)i为第一个的行,j为第二个的列
	}
	return c3;
}

template<typename T>
inline ostream& operator<<(ostream& os, const T_Matrix<T>& c3)
{
	os << "(";
	for (int i = 0; i < c3.size; i++)
	{
		os << c3.mdata[i];
		if (i != c3.size)
			os << ",";
	}
	os << ")" << endl;
	// TODO: 在此处插入 return 语句
	return os;
}

这基本就是我的原代码(搞这作业前后也出不少乌龙,bug,尤其是我一开始函数的实现是写在类的cpp文件里的...怎么都是报错,我还看不懂那种。。。最后才知道模板得都写在头文件里...)

瞎bb环节:

这算是发的第一篇比较像那么回事的正式文吧。

发文章也就当作笔记了,作业做不少,个人注释也写得比较勤(因为感觉自己比较菜,很容易忘记思路,注释还可以挑出当时遇到了什么问题和哪些注意点),但很少回看,只有有时会发现写过类似函数回去复制粘贴“代码重用”......

而在这发,不仅笔记看得舒服,还容易分类,特别是比起原代码文件更像真正的学习笔记一样容易写总结体会;更希望会有人来看来一起交流,指导指导。 

 下面再附上(1)的码:

// testN3.02.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//(1)定义类模板T_Counter,实现基本类型数据的 + 、 - 、 * 、 = 、 >> 、 << 运算;
//(2)类模板T_Matrix,实现矩阵运算。


#include <iostream>
#include"T_Count.h"
using namespace std;

int main()
{
	T_Count<int> t1(5), t2(6);
	cout << "t1+t2=" << t1 + t2 << endl;
	T_Count<int> t3,t4;
	t3 = t1 + t2;
	cout << "t3=" << t3 << endl;
	cout << "t1-t2=" << t1 - t2 << endl;

	cout << "t1*t2=" << t1 * t2 << endl;
	t3 = t1=t2;
	cout << "t3=t1=t2=" << t3 << endl;
	cout << "t1-t2=" << t1 - t2 << endl;
	t4 = t1;
	cout << "t4=t1 -> t4=" << t4 << endl;
	cin >> t4;
	cout << "(new)t4=" << t4 << endl;
}

T_Count.h: 

#pragma once
#include<iostream>
using namespace std;
//要把定义也写在头文件里才行...
template<typename T>
class T_Count
{
public:
	T_Count();
	T_Count(T ni);
private:
	template<typename T> friend T_Count<T> operator+(const T_Count<T> t1, const T_Count<T> t2);
	template<typename T> friend T_Count<T> operator-(const T_Count<T>& t1, const T_Count<T>& t2);
	template<typename T> friend T_Count<T> operator*(const T_Count<T> t1, const T_Count<T> t2);
	//template<typename T> T_Count<T> operator=(const T_Count<T> t1);
	// Matirx<T>& operator=(const Matirx<T> & Other)
	template<typename T>
	T_Count<T>& operator=(const T_Count<T>& t1)
	{
		T r = t1.inter;
		return T_Count(r);
	}
	template<typename T> friend ostream& operator<<(ostream& os, const T_Count<T>& t1);
	template<typename T> friend istream& operator>>(istream& is, T_Count<T>& t1);

	T inter; 
};

template<typename T>
T_Count<T>::T_Count()
{
	inter = 0;
}
template<typename T>
T_Count<T>::T_Count(T ni)
{
	inter = ni;
}

template<typename T>
T_Count<T> operator+(const T_Count<T> t1, const T_Count<T> t2)
{
	T n = t1.inter + t2.inter;
	return T_Count<T>(n);
}

template<typename T>
inline T_Count<T> operator-(const T_Count<T>& t1, const T_Count<T>& t2)
{
	T n = t1.inter - t2.inter;
	return T_Count<T>(n);
}

template<typename T>
inline T_Count<T> operator*(const T_Count<T> t1, const T_Count<T> t2)
{
	T n = t1.inter * t2.inter;
	return T_Count<T>(n);
}


template<typename T>
ostream& operator<<(ostream& os, const T_Count<T>& t1)
{
	os << t1.inter;
	return os;
}

template<typename T>
istream& operator>>(istream& is, T_Count<T>& t1)
{
	cout << "输入一个新的T_Count类型的对象:";
	is >> t1.inter;
	return is;
}

night night~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值