C++ 模板类demo
#include <iostream>
using namespace std; template <typename T>
class MyVector
{ friend ostream & operator<< <T>(ostream &out, const MyVector &obj);
public:
MyVector(int size = ); //构造函数
MyVector(const MyVector &obj); // 拷贝构造函数
~MyVector(); //析构函数 public: T& operator[] (int index);//返回引用
// a3 = a2 = a1;
MyVector &operator=(const MyVector &obj); public:
int getLen()
{
return m_len;
} protected:
T *m_space;
int m_len;
};
#include <iostream>
using namespace std;
#include "MyVector.h" template <typename T>
ostream & operator<<(ostream &out, const MyVector<T> &obj)
{
for (int i=; i<obj.m_len; i++)
{
out << obj.m_space[i] << " ";
//out << t1;
}
out << endl;
return out;
} //MyVector<int> myv1(10);
template <typename T>
MyVector<T>::MyVector(int size) //构造函数
{
m_space = new T[size];
m_len = size;
} //MyVector<int> myv2 = myv1;
template <typename T>
MyVector<T>::MyVector(const MyVector &obj) // 拷贝构造函数
{
//根据myv1的大小分配内存
m_len = obj.m_len;
m_space = new T[m_len]; //copy数据
for (int i=; i<m_len; i++)
{
m_space[i] = obj.m_space[i];
} } template <typename T>
MyVector<T>::~MyVector() //析构函数
{
if (m_space != NULL)
{
delete [] m_space;
m_space = NULL;
m_len = ;
}
} template <typename T>
T& MyVector<T>::operator[] (int index)
{
return m_space[index];
} // a3 = a2 = a1;
template <typename T>
MyVector<T> & MyVector<T>::operator=(const MyVector<T> &obj)
{
//先把a2的旧的内存释放掉 if (m_space != NULL)
{
delete[] m_space;
m_space = NULL;
m_len = ;
} //根据a1分配内存
m_len = obj.m_len;
m_space = new T[m_len]; //copy数据
for (int i=; i<m_len; i++)
{
m_space[i] = obj[i];
}
return *this; // a2 = a1; 返回给a2 的自身
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std; #include "MyVector.cpp" //1 优化Teacher类, 属性变成 char *panme, 购置函数里面 分配内存
//2 优化Teacher类,析构函数 释放panme指向的内存空间
//3 优化Teacher类,避免浅拷贝 重载= 重写拷贝构造函数
//4 优化Teacher类,在Teacher增加 <<
//5 在模板数组类中,存int char Teacher Teacher*(指针类型) //=====>stl 容器的概念 class Teacher
{
public:
Teacher()
{
age = ;
strcpy(name, "");
} Teacher(char *name, int age)
{
this->age = age;
strcpy(this->name, name);
}
void printT()
{
cout << name << ", " << age << endl;
}
private:
int age;
//char name[32];
char *pName2;
}; void main()
{
Teacher t1("t1", ), t2("t2", ), t3("t3", ), t4("t4", ); MyVector<Teacher> tArray(); tArray[] = t1;
tArray[] = t2;
tArray[] = t3;
tArray[] = t4; for (int i=; i<; i++)
{
Teacher tmp = tArray[i];
tmp.printT();
}
cout << tArray; system("pause");
}
void main02()
{
MyVector<char> myv1();
myv1[] = 'a';
myv1[] = 'b';
myv1[] = 'c';
myv1[] = 'd'; cout << myv1; system("pause");
} void main01()
{
MyVector<int> myv1(); for (int i=; i<myv1.getLen(); i++)
{
myv1[i] = i+;
cout << myv1[i] << " ";
}
cout << endl; MyVector<int> myv2 = myv1;
for (int i=; i<myv2.getLen(); i++)
{
cout << myv2[i] << " ";
} cout << myv2 << endl; cout<<"hello..."<<endl;
system("pause");
return ;
}
C++ 模板类demo的更多相关文章
- C++_类入门5-智能指针模板类
智能指针是行为类似于指针的类对象,但这种对象还有其他功能. 本节介绍三个可帮助管理动态内存分配的智能指针模板(auto_ptr.unique_ptr和shared_ptr). void remodel ...
- 单链表的C++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
- 模板类 error LNK2019: 无法解析的外部符号
如果将类模板的声明和实现写在两个独立的文件中,在构建时会出现"error LNK2019: 无法解析的外部符号 "的错误. 解决方法有: 第一种方法,就是把类模板中成员函数的声明和 ...
- 关于g++编译模板类的问题
今天搞了我接近4个小时,代码没错,就是调试没有通过,无论怎么也没有想到是编译器的问题 g++不支持c++模板类 声明与实现分离,都要写到.h文件里面. 以后记住了.
- C++11特性(模板类 initializer_list)
[1]initializer_list模板类 C++primer 原文如下: 通读原文相关篇幅,分析解读内容如下: 提供initializer_list类的初衷,为了便于将有限个同一类型(或可转换为同 ...
- C++11模板类使用心得
1.推荐使用std::shared_ptr<TaskT>代替指针TaskT*使用,shared_ptr是一种智能指针,能自主销毁释放内存,在c++11中被引入,在多线程编程中有很大的用处, ...
- c++模板类
c++模板类 理解编译器的编译模板过程 如何组织编写模板程序 前言常遇到询问使用模板到底是否容易的问题,我的回答是:“模板的使用是容易的,但组织编写却不容易”.看看我们几乎每天都能遇到的模板类吧,如S ...
- C++ 模板函数与模板类
一.模板函数 函数模板提供了一类函数的抽象,即代表了一类函数.当函数模板被实例化后,它会生成具体的模板函数.例如下面便是一个函数模板:
- 模板类重载<<运算符
写了一个Matrix模板类,需要重载<<, 1.需要友元函数 2.需要此函数的实现在.h中(本人试验出来的,放在.cpp中编译不通过) template <typename T> ...
随机推荐
- 3. 使用绘图API自定义视图 --- 旋转的方块
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; impor ...
- css水平居中和垂直居中
水平居中:内联元素:text-align:center;相对于父级居中显示块级元素:margin:0 auto;但是需要同时width,否则无法看到效果多个块级元素居中:在此想要探讨一下display ...
- Spark系列(十)TaskSchedule工作原理
工作原理图 源码分析: 1.) 25 launchedTask = true 26 } 27 } catch { 28 ...
- makefile、gdb使用记录
makefile的模板 all: rover server station rover: rover.c tcputil.o gcc rover.c tcputil.o -o rover -lpthr ...
- 【Hadoop学习】HDFS 短路本地读
Hadoop版本:2.6.0 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4146296.html 背景 ...
- The h.264 Sequence Parameter Set
转债: http://www.cardinalpeak.com/blog/the-h-264-sequence-parameter-set/ View from the Peak The h.264 ...
- 记录一次Android交叉编译ffmpeg排查错误
Android版本手机直播引擎中,引用了libvlc开源库.项目接过来,发现编译脚本中使用了很多用户名下的绝对路径.项目相关人离职,导致这个脚本实际上已经废掉.而且不知道相关路径下有没有其他文件和第三 ...
- Android问题-打开DelphiXE8与DelphiXE10新建一个空工程提示"out of memory"
错误信息: [DCC Error] E2597 d:\XE8\Embarcadero\Studio\16.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-l ...
- stm32f407 定时器 用的APB1 APB2 及 定时器频率
上午想要用Timer10做相对精确的延时功能,但是用示波器发现实际延时数值总是只有一半,百思不得其解.仔细查阅各处资料结合实际研究后对stm32f407的14个定时器的时钟做一个总结: 下面来源: h ...
- 详解Java解析XML的四种方法
XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这里面包括的内容有DOM ...