当前位置:网站首页>9. class and object practice and initialization list

9. class and object practice and initialization list

2022-06-23 01:41:00 For financial freedom!

class String
{
    
public:
	String(const char* str = nullptr)
	{
    
		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)
	{
    
		m_data = new char[strlen(other.m_data)+1];
		strcpy(m_data,other.m_data);
	}
	~String()
	{
    
		delete[] m_data;
		m_data = nullptr;
	}
	String& operator=(const String& other)
	{
    
		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;
}:

str3=str1=str2
The reason why continuous assignment is feasible is :String& operator=(const String& other), The return type is String&

Circular queue :

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];
		}
		return *this;
	}
	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];
		}
		return *this;
	}
	~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 top()
	{
    
		return _pQue[_front];
	}
	bool full()
	{
    
		return (_rear+1)%_size == _front;
	}
	bool empty()
	{
    
		return _front == _rear;
	}
private:
	int* _pQue;
	int _front;// Team leader position 
	int _rear;// At the end of the line 
	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;
	}
};

Initialization list , Determines how member variables are initialized ! If the member variable is a class object , The class constructor of this class object is not the default , There is no default , such as :

class CDate
{
    
	CData(int x,int y,int z);
};
class CGoods
{
    
public:
	CGoods(int price):_date(1,2,3),_price(price)
	{
    }
private:
	CDate _date;
	int _price;
}
// This is legal , Because the initialization list is executed first , and CGoods():_date(1,2,3) and 
//CDate _date(1,2,3) It's the same in assembly 
// If the above does not initialize the list ,CDate _date; This will report an error , Because the default constructor is not provided or the constructor default value is not provided 

 Insert picture description here
The execution order of the initialization list depends on the order of the member variables , Instead of initializing the order of the list !

原网站

版权声明
本文为[For financial freedom!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220514413885.html