Skip to content

庄朋龙的博客

有志者自有千方万计,无志者只感千难万难。

Menu
  • 首页
  • 生活
  • 创业
  • 编程
  • 运维
  • 视频课程
    • 跨境独立站
    • Shopee跨境电商
    • TEMU跨境电商
  • 资源分享
    • 网站推荐
    • 开源推荐
    • 可商用字体
    • 书籍分享
Menu

世界,您好!

Posted on 2015年10月28日2025年3月4日 by 庄朋龙
#include <iostream>
using namespace std;
int g_ret = -1000;
class Array
{
public:
	Array(int size) :_size(size)
	{
		m_data = new int[size];
	}
	
	~Array()
	{
		delete []m_data;
		m_data = NULL;
	}
	
	Array (const Array& other) //拷贝构造函数
	{
		this->_size = other._size;
		this->m_data = new int[other._size];
		
		memcpy(this->m_data,other.m_data,other._size*sizeof(int));
	}
	Array& operator = (const Array& o) //复制运算符重载
	{
		if(o == *this) //先判断一下是否一样,要是一样的话 就不用赋值了
			return *this;
		else
		{
			delete []this->m_data;
			this->_size = o._size;
			this->m_data = o.m_data;
			memcpy(this->m_data,o.m_data,o._size*sizeof(int));
			return *this;
		}
	}
	
	int & operator[] (int index)
	{
		if(this->_size > index && index >= 0)
		{
			return this->m_data[index];
		}else{
			cout<<"error"<<endl;
			return g_ret;
		}
	}
	
	friend bool operator != (const Array&a1,const Array& a2);
	friend bool operator == (const Array&a1,const Array& a2);
	
	friend istream& operator >>(istream& in, Array& arr);
	friend ostream& operator <<(ostream& out,const Array& arr);
private:
    int _size;
    int *m_data;
};

bool operator == (const Array&a1, const Array& a2)
{
	if(a1._size == a2._size) //先判断一下要赋值的两个是否相同
	{
		for(int i=0; i<a1._size; i++)
		{
			if(a1.m_data[i] != a2.m_data[i])
		//判断一下 两个类的 m_data 成员是否相同
				return false;
		}
		return true;
	}
	else
		return true;
} 
bool operator != (const Array &a1,const Array & a2)
{
	return !(a1 == a2);
}

istream& operator >>(istream& in, Array& arr)
{
	for(int i=0; i< arr._size; ++i)
	{
		in>>arr[i];
	}
	return in;
}
ostream& operator <<(ostream& out,const Array& arr)
{
	for(int i = 0; i < arr._size; ++i)
	{
		out<<arr.m_data[i]<<" ";
	}
	out<<endl;
	return out;
}

int main()
{
	Array a(5);
	cin>>a;
	cout<<a;
	return 0;
}

 

Category: 资源分享

4 thoughts on “世界,您好!”

  1. meng说道:
    2015年10月30日 下午1:32

    留个脚印

    1. 庄朋龙说道:
      2015年11月2日 下午9:13

      评论测试

      1. 庄朋龙说道:
        2015年11月2日 下午9:14

        评论测试456

    2. 庄朋龙说道:
      2015年11月2日 下午9:14

      评论测试123

Comments are closed.

全栈开发者×创业偏执狂

相信代码能改变命运,也相信凌晨四点的服务器警报里有真实的人生。

我的创业项目:
SHOPAGG / SmallShop / DigitShops / 文硕阁

© 2025 庄朋龙的博客