封装你自己的数组类;设计被存储的元素为类对象;
思考:类对象的类,应该实现的功能。
//1 优化 Teacher 类, 属性变成 char panme, 构造函数里面 分配内存
//2 优化 Teacher 类,析构函数 释放 panme 指向的内存空间
//3 优化 Teacher 类,避免浅拷贝 重载= 重写拷贝构造函数
//4 优化 Teacher 类,在 Teacher 增加 <<
//5 在模板数组类中,存 int char Teacher Teacher(指针类型)
//=====>stl 容器的概念
MyVector.h
#pragma once
#include <iostream>
using namespace std;
template <typename T>
class MyVector
{
public:
friend ostream& operator<< <T>(ostream& out, const MyVector& obj);
MyVector(int size = 0); //构造函数
MyVector(const MyVector &obj); //拷贝构造函数
~MyVector(); //析构函数
int GetLen()
{
return m_len;
}
public:
T& operator[](int index);
MyVector& operator=(const MyVector& obj);
private:
T *m_space;
int m_len;
};
MyVector.cpp
#include "MyVector.h"
#include <iostream>
using namespace std;
template <typename T>
ostream& operator<<(ostream& out, const MyVector<T>& obj)
{
for (int i = 0; i < obj.m_len; i++)
{
out << obj.m_space[i] << " ";
}
out << endl;
return out;
}
template <typename T>
MyVector<T>::MyVector(int size = 0) //构造函数
{
m_space = new T[size];
m_len = size;
}
//MyVector<int> myvl2 = myvl;
template <typename T>
MyVector<T>::MyVector(const MyVector &obj) //拷贝构造函数
{
m_len = obj.m_len;
m_space = new T[m_len];
for (int i = 0; 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_len = 0;
m_space = NULL;
}
}
template <typename T>
T& MyVector<T>::operator[](int index)
{
return m_space[index];
}
//a1 = a2
template <typename T>
MyVector<T>& MyVector<T>::operator=(const MyVector& obj)
{
//先把旧内存a1释放掉
if (m_space != NULL)
{
delete[] m_space;
m_space = NULL;
m_len = 0;
}
m_len = obj.m_len;
//再根据a2分配新内存
m_space = new T[m_len];
return *this;
}
MyVector_test.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "MyVector.cpp"
#include <iostream>
using namespace std;
//1、优化Teacher类, 属性变成char *pname; 构造函数里分配内存
//2、析构函数中释放内存
//3、避免浅拷贝 重载=运算符 重写拷贝构造函数
//4、在Teacher中增加<<运算符重载
//5、在模板数组类中存 int char Teacher Teacher*等类型
class Teacher
{
public:
Teacher()
{
age = 33;
m_p = new char[1];
strcpy(m_p, "");
}
Teacher(char *name, int age)
{
this->age = age;
this->m_p = new char[strlen(name)];
strcpy(this->m_p, name);
}
Teacher(const Teacher& obj)
{
m_p = new char[strlen(obj.m_p) + 1];
age = obj.age;
strcpy(this->m_p, obj.m_p);
}
~Teacher()
{
if (m_p = NULL)
{
delete[] m_p;
m_p = NULL;
age = 33;
}
}
void ptintT()
{
cout << m_p << ", " << age << endl;
}
public: //重载操作符
friend ostream& operator<<(ostream &out, Teacher &obj);
Teacher& operator=(const Teacher &obj)
{
//1、先释放旧内存
if (m_p != NULL)
{
delete[] m_p;
m_p = NULL;
age = 33;
}
//2、根据参数obj分配内存
m_p = new char[strlen(obj.m_p) + 1];
//3、赋值拷贝
age = obj.age;
strcpy(m_p, obj.m_p);
return *this;
}
private:
int age;
//char name[32];
char *m_p; //问题抛出:如果是一个指针则程序崩溃 深拷贝和浅拷贝(优化)
};
ostream& operator<<(ostream &out, Teacher &obj)
{
out << obj.m_p << ", " << obj.age << endl;
return out;
}
void main()
{
//MyVector中存Teacher *指针类型
Teacher t1("t1", 30), t2("t2", 31), t3("t3", 32), t4("t4", 33);
MyVector<Teacher *> tArray(4);
tArray[0] = &t1;
tArray[1] = &t2;
tArray[2] = &t3;
tArray[3] = &t4;
for (int i = 0; i < 4; i++)
{
Teacher *temp = tArray[i];
temp->ptintT();
}
system("pause");
}
void main003_存Teacher类型()
{
Teacher t1("t1", 30), t2("t2", 31), t3("t3", 32), t4("t4", 33);
MyVector<Teacher> tArray(4);
tArray[0] = t1;
tArray[1] = t2;
tArray[2] = t3;
tArray[3] = t4;
for (int i = 0; i < 4; i++)
{
Teacher temp = tArray[i];
temp.ptintT();
}
system("pause");
}
void main002_存char类型()
{
MyVector<char> myvl(10);
myvl[0] = 'a';
myvl[1] = 'b';
myvl[2] = 'c';
myvl[3] = 'd';
myvl[4] = '\0';
cout << myvl;
system("pause");
}
void main001_存int类型()
{
MyVector<int> myvl(10);
for (int i = 0; i < myvl.GetLen(); i++)
{
myvl[i] = i + 1;
cout << myvl[i] << " ";
}
cout << endl;
MyVector<int> myvl2 = myvl;
for (int i = 0; i < myvl2.GetLen(); i++)
{
cout << myvl2[i] << " ";
}
cout << endl;
cout << myvl2 << endl;
system("pause");
}