当前位置: 首页 > news >正文

类模板的实现

pragma once

include

using namespace std;
template
class Myvector
{
friend ostream& operator<< (ostream& out, const Myvector & othervectr); //用有元函数重载输出运算符 ,注意必须要加
public:
Myvector(int zise=128);
Myvector(const Myvector & othervectr); //拷贝构造函数
T& operator [](int index); //实现下标访问的重载操作

Myvector& operator =(const Myvector & othervectr);
~Myvector();
int getlen();

private:
int len;
T* basedate; //存储数组的元素
};

include "Myvector.h"

include<Windows.h>

template
Myvector::Myvector( int zise)
{
if ( zise>0)
{
this->len = zise;
this->basedate = new T[len]; //给basedate 分配内存,实际上已经指向了数组 Myvector
}
}

template
Myvector::Myvector(const Myvector& othervectr)
{
// 根据传入的对象元素个数分配内存空间
this->len = othervectr.len;
this->basedate = new T[len];
for (int i=0;i<len;i++)
{
this->basedate[i] = othervectr.basedate[i];
}
}
template
T& Myvector::operator[](int index) //通过下标访问数组的元素
{
return basedate[index]; //此时的 basedate[index] 相当于 数组a[i]
}
template
Myvector& Myvector::operator=(const Myvector& othervectr)
{

if (this->basedate != NULL)
{delete[] this->basedate;this->basedate = NULL;this->len = 0;
}// 根据传入的对象元素个数分配内存空间
this->len = othervectr.len;
this->basedate = new T[len];
for (int i = 0; i < len; i++)
{this->basedate[i] = othervectr.basedate[i];
}
return *this;

}

template
Myvector::~Myvector()
{
if (basedate!= NULL)
{
delete[] basedate;
basedate = NULL;
len = 0;
}
}

template
int Myvector::getlen()
{
return this->len;
}
template
ostream & operator<<(ostream& out, const Myvector& othervectr) //注意这里没有
{
for (int i=0; i< othervectr.len;i++)
{
if (i!= othervectr.len-1)
{
out << othervectr.basedate[i] << ",";
}
else
{
out << othervectr.basedate[i];
}

 }return out;

}

include<Windows.h>

include"Myvector.cpp"

include

using namespace std;
int main()
{
Myvector myvect1(9);

for (int i = 0; i < myvect1.getlen(); i++)
{myvect1[i] = i*3+4;
}
cout << myvect1<<endl;     //调用输出运符重载 Myvector<string> myvect2(3) ;
myvect2[0] = "张三";
myvect2[1] = "李四";
myvect2[2] = "老五";Myvector<string> myvect3(myvect2);    //调用拷贝构造函数Myvector<string>myvect4(1);myvect4 = myvect3; //调用重载运算符for (int i = 0; i < myvect2.getlen(); i++)
{cout << "输出数组myvect2的第" << i << "个元素:" << myvect2[i] << endl;if (i == myvect2.getlen() - 1){cout << "--------默认构造函数调用----------\n\n";}
}for (int i = 0; i < myvect3.getlen(); i++)
{cout << "输出数组myvect3的第" << i << "个元素:" << myvect3[i] << endl;if (i== myvect3.getlen()-1){cout << "--------拷贝构造函数调用----------\n\n";}
}for (int i = 0; i < myvect4.getlen(); i++)
{cout << "输出数组myvect4的第" << i << "个元素:" << myvect4[i] << endl;if (i == myvect4.getlen() - 1){cout << "--------重载运算符调用----------\n\n";}
}////cout << "数组的长度:" << myvect.getlen() << endl;
//int a[10]= { 89, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
////int len = sizeof(a) / (a[0]);    //求数组的长度
//for (int i=0; i< 10; i++)
//{
//	cout<<"输出数组的第"<<i+1<<"个元素:"<<a[i]<<endl;
//}////数组名实际上是一个指向数组第一个元素的常量指针
//cout << "通过数组名输出数组的第一个元素:" << *a << endl;  
system("pause");
return 0;

}

http://www.jsqmd.com/news/50924/

相关文章:

  • rime(小狼毫)+雾凇+皮肤+万象大模型+个人词库补充
  • 2025年中国前五大轮胎品牌:专业测评与选购指南
  • lru_cache装饰器的缓存清除机制原理
  • 2025年中国前十大轮胎品牌:最新官方榜单深度解析
  • 网格图分治模型
  • Python内置的lru_cache装饰器实现缓存教程
  • 2025年轮胎品牌推荐:权威TOP10全球品牌综合排名
  • 详细介绍:Git分支合并实战指南:从feature到master,一文搞定全流程!
  • 北京墙体彩绘公司推荐香鲸艺术坊,行业排名遥遥领先!
  • 虚拟科学峰会推动技术交流创新
  • java---gradle配置国内镜像
  • 2025年11月南京装修公司综合实力排行榜(品牌智鉴榜推荐)
  • 揭开Claude Opus 4.5神秘面纱
  • Image图片组件基础加载与属性设置
  • 2025年新能源汽车轮胎推荐:独家负载与静音测评报告
  • 11月25日日记
  • CF370A-Rook, Bishop and King
  • 实用指南:基于“开源AI智能名片链动2+1模式S2B2C商城小程序”的会员制培养策略研究
  • 2025年越野轮胎推荐:十大专业品牌最新全地形解析
  • 11月25日
  • Switch大气层20-整合包1-9-0测试版
  • 2025年家用轿车轮胎推荐:权威综合排名与选购指南
  • 基于.net6的一款开源的低代码、权限、工作流、动态接口平台-系统安装篇
  • macOS开启自带的TFTP Server
  • AT_arc178_c [ARC178C] Sum of Abs 2
  • 几道树上计数问题
  • 接入层傻瓜机引起的VLAN间环路
  • 实用指南:线性回归中梯度下降的最终结果是否为全局最小解
  • 2025年安全的轮胎推荐:专业制动测评与选购攻略
  • MISC图片隐写