C++小例子——04 列向量模板类ColVector

本文介绍了如何将C++的列向量类改造成模板类,允许元素类型多样化,如vector和tensor。通过在头文件中声明模板类的实例化,实现了头文件和cpp文件的分离编译。提供了ColVector.h、ColVector.cpp和main.cpp的代码示例,并通过测试验证了所有功能的正确性。
摘要由CSDN通过智能技术生成

把03列向量类改成了模板类,如此一来,列向量的元素类型不再局限于double,而可以是vector,tensor等。对于模板类而言,要想实现头文件和cpp文件的分开编译处理,头文件中需要对模板类进行实例化的声明。

写好的代码如下:

ColVector.h头文件

// 2020-07-30
// class ColVector -- head file
// A column vector template class
// type could be double, vector, tensor, etc
// Author: Guanhua Mei

#ifndef ColVector_H
#define ColVector_H

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

template<class T>
class ColVector
{
   
private:
	unsigned int m_nSize;	// capicity of column vector, number of elems
	T* m_pColVct;			// column vector array pointer
public:
	// constructors
	// construct with size and value
	ColVector(unsigned int = 0, T = T());

	// copy constructor, deep copy
	ColVector(const ColVector<T>&);

	// copy assignment, deep copy
	ColVector& operator=(const ColVector<T>&);

	// move constructor, shallow copy
	ColVector(ColVector<T>&&) noexcept;

	// move assignment, shallow copy
	ColVector& operator=(ColVector<T>&&) noexcept;

	// destructor
	~ColVector();

	// return size
	unsigned int size() const {
    return m_nSize; }

	// get value at idx
	T getCompIdx(unsigned int) const;

	// set value at idx
	void setCompIdx(unsigned int, const T& = T());

	// set all
	void setAll(const T& = T());

	// operator [] to fetch value
	T& operator[](unsigned int);
	const T& operator[](unsigned int) const;

	// operator += -= *= /=, legality wil not be checked!!
	ColVector& operator+=(const ColVector&);
	ColVector& operator+=(const T&);
	ColVector& operator-=(const ColVector&);
	ColVector& operator-=(const T&);
	ColVector& operator*=(const ColVector&);
	ColVector& operator*=(const T&);
	ColVector& operator/=(const ColVector&);
	ColVector& operator/=(const T&);

	// friend operator + - * /, legality wil not be checked!!
	template<class Type>
	friend ColVector<Type> operator+(const ColVector<Type>&, const ColVector<Type>&);
	template<class Type>
	friend ColVector<Type> operator+(const ColVector<Type>&, const Type&);
	template<class Type>
	friend ColVector<Type> operator+(const Type&, const ColVector<Type>&);
	template<class Type>
	friend ColVector<Type> operator-(const ColVector<Type>&, const ColVector<Type>&);
	template<class Type>
	friend ColVector<Type> operator-(const ColVector<Type>&, const Type&);
	template<class Type>
	friend ColVector<Type> operator-(const Type&, const ColVector<Type>&);
	template<class Type>
	friend ColVector<Type> operator*(const ColVector<Type>&, const ColVector<Type>&);
	template<class Type>
	friend ColVector<Type> operator*(const ColVector<Type>&, const Type&);
	template<class Type>
	friend ColVector<Type> operator*(const Type&, const ColVector<Type>&);
	template<class Type>
	friend ColVector<Type> operator/(const ColVector<Type>&, const ColVector<Type>&);
	template<class Type>
	friend ColVector<Type> operator/(const ColVector<Type>&, const Type&);
	template<class Type>
	friend ColVector<Type> operator/(const Type&, const ColVector<Type>&);

	// print
	void print();

	// outputstream operator <<
	template<class Type>
	friend std::ostream& operator<<(std::ostream&, const ColVector<Type>&);
};

// instantiation template class
template class ColVector<double>;
template class ColVector<Vector>;

// instantiation template friend functions
// instantiation of operator <<
template std::ostream& operator<<(std::ostream&, const ColVector<double>&);
template std::ostream& operator<<(std::ostream&, const ColVector<Vector>&);

// friend functions need to be declarated outside again!
// friend function of outputstream operator <<
template<class T>
std::ostream& operator<<(std::ostream&, const ColVector<T>&);

// friend functions of operator + - * /, legality wil not be checked!!
template<class Type>
ColVector<Type> operator+(const ColVector<Type>&, const ColVector<Type>&);
template<class Type>
ColVector<Type> operator+(const ColVector<Type>&, const Type&);
template<class Type>
ColVector<Type> operator+(const Type&, const ColVector<Type>&);
template<class Type>
ColVector<Type> operator-(const ColVector<Type>&, const ColVector<Type>&);
template<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值