#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的更多相关文章

  1. C++_类入门5-智能指针模板类

    智能指针是行为类似于指针的类对象,但这种对象还有其他功能. 本节介绍三个可帮助管理动态内存分配的智能指针模板(auto_ptr.unique_ptr和shared_ptr). void remodel ...

  2. 单链表的C++实现(采用模板类)

    采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作.  链表结构定义 定义单链表 ...

  3. 模板类 error LNK2019: 无法解析的外部符号

    如果将类模板的声明和实现写在两个独立的文件中,在构建时会出现"error LNK2019: 无法解析的外部符号 "的错误. 解决方法有: 第一种方法,就是把类模板中成员函数的声明和 ...

  4. 关于g++编译模板类的问题

    今天搞了我接近4个小时,代码没错,就是调试没有通过,无论怎么也没有想到是编译器的问题 g++不支持c++模板类 声明与实现分离,都要写到.h文件里面. 以后记住了.

  5. C++11特性(模板类 initializer_list)

    [1]initializer_list模板类 C++primer 原文如下: 通读原文相关篇幅,分析解读内容如下: 提供initializer_list类的初衷,为了便于将有限个同一类型(或可转换为同 ...

  6. C++11模板类使用心得

    1.推荐使用std::shared_ptr<TaskT>代替指针TaskT*使用,shared_ptr是一种智能指针,能自主销毁释放内存,在c++11中被引入,在多线程编程中有很大的用处, ...

  7. c++模板类

    c++模板类 理解编译器的编译模板过程 如何组织编写模板程序 前言常遇到询问使用模板到底是否容易的问题,我的回答是:“模板的使用是容易的,但组织编写却不容易”.看看我们几乎每天都能遇到的模板类吧,如S ...

  8. C++ 模板函数与模板类

    一.模板函数 函数模板提供了一类函数的抽象,即代表了一类函数.当函数模板被实例化后,它会生成具体的模板函数.例如下面便是一个函数模板:

  9. 模板类重载<<运算符

    写了一个Matrix模板类,需要重载<<, 1.需要友元函数 2.需要此函数的实现在.h中(本人试验出来的,放在.cpp中编译不通过) template <typename T> ...

随机推荐

  1. 扩展Fitnesse的ScriptTable:支持if-then

    Fitnesse的ScriptTable只能顺序执行所有行,本博文介绍如何让ScriptTable支持if-then,来条件执行一行. 首先普及一下概念,什么是Fitnesse,听一听.NET版Cuc ...

  2. python easy_install centos 下安装过程和原理解析

    一.easy_install 安装过程 其安装过程有很多种,我也找了很多的例子,但是结果都不太好,以下方法的结果是不错的. easy_install与yum类似,使用easy_install,可以轻松 ...

  3. cocos2d-x 添加背景音乐和音效-SimpleAudioEngine

    首先,要想使用音效,需要启用音效引擎库CocosDenshion中的SimpleAudioEngine类, #include "SimpleAudioEngine.h" Cocos ...

  4. C#快速排序详解

    使用快速排序法对一列数字进行排序的过程 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists). 步骤为: 从数列中挑出一个元素,称 ...

  5. 十字链表 Codeforces Round #367 E Working routine

    // 十字链表 Codeforces Round #367 E Working routine // 题意:给你一个矩阵,q次询问,每次交换两个子矩阵,问最后的矩阵 // 思路:暴力肯定不行.我们可以 ...

  6. HDU4456-Crowd(坐标旋转+二位树状数组+离散化)

    转自:http://blog.csdn.net/sdj222555/article/details/10828607 大意就是给出一个矩阵 初始每个位置上的值都为0 然后有两种操作 一种是更改某个位置 ...

  7. HDU 5792 World is Exploding (树状数组)

    World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  8. HDU 1875 畅通工程再续 (最小生成树)

    畅通工程再续 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/M Description 相信大家都听说一个"百岛湖&q ...

  9. jquery ajax请求后台 的简单例子

    jQuery.ajax(url,[settings]) 概述 通过 HTTP 请求加载远程数据. jQuery 底层 AJAX 实现.简单易用的高层实现见 $.get, $.post 等.$.ajax ...

  10. 更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found

    在Xcode中当你在更新了你得证书而再重新编译你的程序,真机调试一直会出现 Code Sign error: Provisioning profile ‘XXXX’ can't be found是不是 ...