类和对象代码应用实践
实现一个String类
#include<string>
#include<iostream>
class String
{
public:
String(const char* str = nullptr)//普通构造函数
{
std::cout << this << "String" << std::endl;
if (str != nullptr)
{
m_data = new char[strlen(str) + 1];
strcpy(this->m_data, str);
}
else
{
m_data = new char[1];
*m_data = '\0';
}
}
String(const String& other)
{
std::cout << this << "String(const String& other)" << std::endl;
m_data = new char[strlen(other.m_data) + 1];
strcpy(m_data, other.m_data);
}
~String()
{
std::cout << this << "~String" << std::endl;
delete[] m_data;
m_data = nullptr;
}
String& operator=(const String& other)//赋值重载函数
{
std::cout << this << "String& operator=(const String& other)" << std::endl;
//防止自赋值
if (this == &other)
{
return *this;
}
//删除原有
delete[] m_data;
m_data = new char[strlen(other.m_data) + 1];
strcpy(m_data, other.m_data);
return *this;
}
private:
char* m_data;//用于保存字符串
};
int main()
{
//调用const char* 参数的构造函数
String str1;
String str2("hello");
String str3 = "world";
//调用拷贝构造函数
String str4 = str3;
String str5(str3);
//调用赋值重载函数
//str1 = str2;
str3 = str1 = str2;
return 0;
}
输出信息
00000026FC7FF968String
00000026FC7FF988String
00000026FC7FF9A8String
00000026FC7FF9C8String(const String& other)
00000026FC7FF9E8String(const String& other)
00000026FC7FF968String& operator=(const String& other)
00000026FC7FF9A8String& operator=(const String& other)
00000026FC7FF9E8~String
00000026FC7FF9C8~String
00000026FC7FF9A8~String
00000026FC7FF988~String
00000026FC7FF968~String
循环队列实现
#include<string>
#include<iostream>
//循环队列
class Queue
{
public:
Queue(int size = 20)
{
_pQue = new int[size];
_front = _rear = 0;
_size = size;
}
Queue(const Queue& src)
{
_size = src._size;
_front = src._front;
_rear = src._rear;
_pQue = new int[_size];
for (int i = _front; i != _rear; i = (i + 1) % _size)
{
_pQue[i] = src._pQue[i];
}
}
Queue& operator=(const Queue& src)
{
if (this == &src)
{
return *this;
}
delete[] _pQue;
_size = src._size;
_front = src._front;
_rear = src._rear;
_pQue = new int[_size];
for (int i = _front; i != _rear; i = (i + 1) % _size)
{
_pQue[i] = src._pQue[i];
}
}
~Queue()
{
delete[] _pQue;
_pQue = nullptr;
}
void push(int val)//入队操作
{
if (full())
resize();
_pQue[_rear] = val;
_rear = (_rear + 1) % _size;
}
void pop()
{
if (empty())
return;
_front = (_front + 1) % _size;
}
int front()//获取队头元素
{
return _pQue[_front];
}
bool full() { return (_rear + 1) % _size == _front; }
bool empty() { return _front == _rear; }
private:
int* _pQue;//申请队列的数组空间
int _front;//指示队头的位置
int _rear;//指向队尾的位置
int _size;//队列扩容的总大小
void resize()
{
int* ptmp = new int[2 * _size];
int index = 0;
for (int i = _front; i != _rear; i = (i + 1) % _size)
{
ptmp[index++] = _pQue[i];
}
delete[] _pQue;
_pQue = ptmp;
_front = 0;
_rear = index;
_size *= 2;
}
};
int main()
{
Queue queue;
for (int i = 0; i < 20; ++i)
{
queue.push(rand() % 100);
}
Queue queue1 = queue;
while (!queue.empty())
{
std::cout << queue.front() << " ";
queue.pop();
}
std::cout << std::endl;
while (!queue1.empty())
{
std::cout << queue1.front() << " ";
queue1.pop();
}
}
输出信息如下:
41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95 42 27 36
41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95 42 27 36
浙公网安备 33010602011771号