当前位置: 首页 > 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.gsyq.cn/news/60810.html

相关文章:

  • 2025年中国前五大轮胎品牌:专业测评与选购指南
  • 2025年中国前十大轮胎品牌:最新官方榜单深度解析
  • 虚拟科学峰会推动技术交流创新
  • 2025年11月南京装修公司综合实力排行榜(品牌智鉴榜推荐)
  • 揭开Claude Opus 4.5神秘面纱
  • 实用指南:基于“开源AI智能名片链动2+1模式S2B2C商城小程序”的会员制培养策略研究
  • Switch大气层20-整合包1-9-0测试版
  • 2025年家用轿车轮胎推荐:权威综合排名与选购指南
  • macOS开启自带的TFTP Server
  • MISC图片隐写
  • php中的phar反序列化基础
  • AT_arc083_d [ARC083F] Collecting Balls 笔记
  • 数据采集第三次作业-102302128吴建良
  • 102302116_田自豪_作业3
  • 畅通工程 最小生成树
  • Oracle数据库物理备份与恢复实战指南
  • 实用指南:Kafka面试精讲 Day 30:Kafka面试真题解析与答题技巧
  • 有用的包 #Python
  • 2025 人事管理工具选型:不同方案优劣势测评,中小企业闭眼抄作业
  • 2025年大众途观L更换轮胎推荐:五大专业品牌最新推荐
  • 树上背包优化
  • 2025年11月十大效果图公司推荐榜单:用户口碑评价与性能参数对比
  • 2025年11月十大效果图公司推荐榜单:专业分析与权威评测对比
  • 2025 年 11 月高壓清洗服務廠家推薦排行榜,管道/下水道/污水管/市政管道高壓清洗,化糞池/隔油池/污水池專業清洗,家庭/商鋪/小區/工廠高效深度清潔首選!
  • 如何在C++中实现面向对象编程?
  • 最简单的畅通工程
  • 唯物辩证法3大观点11原理
  • 加盟稳赚?2025广东自习室加盟TOP5品牌及盈利方案
  • AI写论文方法全揭秘:轻松掌握高效论文写作技巧
  • 2025年11月最新出炉!南京装修公司推荐首推欧阅恒装 TOP5权威榜单