Operator overload (C + +) -- instance

Posted by abriggs on Fri, 13 Mar 2020 16:28:09 +0100

Program instance

Plural class

Adding operation of complex type: real part plus real part, virtual part plus virtual part
1. heavy load "+":

CComplex CComplex:: operator+(const CComplex &src)
{
	return CComplex(src.mreal + mreal, src.mimage + mimage);
}

We found a problem, similar to com4 = com1 + 10; it can be successful, because the right operand is passed in as a parameter for type conversion, while com4 = 10 + com1; is wrong, because the overloaded function needs to be called on the left side of the operator, so we implemented it as a global function (friend):

CComplex operator+(const CComplex &lhs, const CComplex &rhs)  
// It is defined as a global implementation similar to comp4 = 10 + comp3. The parameters on the left will be implicitly converted
{
	return CComplex(lhs.mreal + rhs.mreal, lhs.mimage + rhs.mimage);
}

2. Overload I / O operators "< and" > ":

ostream& operator<<(ostream &out, const CComplex &src)
{
	out << "mreal = " << src.mreal << ", mimage = " << src.mimage << endl;
	return out;
}

istream& operator>>(istream &in, CComplex &src) // Cannot be declared const because of modification  
{
	in >> src.mreal >> src.mimage;
	return in;
}

3. Overload "+ =" operator

void CComplex :: operator+=(const CComplex &src)
{
	mreal += src.mreal;
	mimage += src.mimage;
}

4. Overload the pre + + operator

CComplex& CComplex :: operator++()
{
	mreal++;
	mimage++;
	return *this;
}

5. Overload post + + operator

// When the post monocular operator is overloaded as a member function of a class, the function must have an integer parameter.
CComplex CComplex :: operator++(int)
{
	return CComplex(mreal++, mimage++);
}

6. complete Code

class CComplex
{
public:
	CComplex(int real = 0, int image = 0):mreal(real), mimage(image) {};
	CComplex operator+(const CComplex &src);
	void operator+=(const CComplex &src);
	CComplex operator++(int);
	CComplex& operator++();
private:
	int mreal;
	int mimage;

	friend istream& operator>>(istream &in, CComplex &src);
	friend ostream& operator<<(ostream &out, CComplex &src);
	friend CComplex operator+(const CComplex &lhs, const CComplex &rhs);
};

CComplex CComplex:: operator+(const CComplex &src)
{
	return CComplex(src.mreal + mreal, src.mimage + mimage);
}

ostream& operator<<(ostream &out, const CComplex &src)
{
	out << "mreal = " << src.mreal << ", mimage = " << src.mimage << endl;
	return out;
}
istream& operator>>(istream &in, CComplex &src) // Cannot be declared const because of modification  
{
	in >> src.mreal >> src.mimage;
	return in;
}

void CComplex :: operator+=(const CComplex &src)
{
	mreal += src.mreal;
	mimage += src.mimage;
}

CComplex CComplex :: operator++(int)//Post + +
{
	return CComplex(mreal++, mimage++);
}

CComplex& CComplex :: operator++()//Prefix + + +
{
	mreal++;
	mimage++;
	return *this;
}

CComplex operator+(const CComplex &lhs, const CComplex &rhs)  
// It is defined as a global implementation similar to comp3 = 10 + comp2. The parameters on the left will be implicitly converted
{
	return CComplex(lhs.mreal + rhs.mreal, lhs.mimage + rhs.mimage);
}

int main()
{
	CComplex com(10, 10);
	cout << com;
	CComplex com1(20, 10);
	com1 += com;
	cout << com1;
	
	CComplex com2;
	com2 = com + com1;
	cout << com2;

	CComplex com3 = com2++;
	cout << com3;
	cout << com2;

	com3 = ++com2;
	cout << com3;
	cout << com2;

	CComplex com4;
	cin >> com4;
	cout << com4;

	com4 += com1;
	cout << com4;

	com4 = com1 + 10;
	cout << com4;

	com4 = 10 + com1;
	cout << com1;
	cout << com4;
}

String class implementation (partial)

class CString
{
public:
	CString(char *p = NULL)
	{
		if(p != NULL)
		{
			_pstr = new char[strlen(p) + 1];//Plus 1 because the string ends with '\ 0', and strlen is the effective length
			strcpy(_pstr, p);
		}
		else
		{
			_pstr = new char[1];
			*_pstr = '\0';//This is done so that you don't have to consider nullptr every time you use it
		}
	}
	~CString()
	{
		if (_pstr != NULL)
		{
			delete []_pstr;
			_pstr = NULL;
		}
	}
	CString(const CString &src)
	{
		_pstr = new char[strlen(src._pstr) + 1];
		strcpy(_pstr, src._pstr);
	}
	CString& operator=(const CString &src)
	{
		if(this == &src)
			return *this;

		delete []_pstr;

		_pstr = new char[strlen(src._pstr) + 1];
		strcpy(_pstr, src._pstr);

		return *this;
	}

	bool operator>(const CString &src)const
	{
		return (strcmp(_pstr, src._pstr) > 0);
	}

	bool operator<(const CString &src)const
	{
		return (strcmp(_pstr, src._pstr) < 0);
	}
	bool operator==(const CString &src)const
	{
		return (strcmp(_pstr, src._pstr) == 0);
	}

	int length()const
	{
		return strlen(_pstr);
	}
	char operator[](int index)const//Read operation only, write const, common object and constant object can call
	{
		return _pstr[index];
	}
	const char* c_str()const
	{
		return _pstr;
	}

private:
	char *_pstr;

	friend CString operator+(const CString &lhs, const CString &rhs);
	friend ostream& operator<<(ostream &out, const CString &str);
	friend istream& operator>>(istream &in, CString &str);
};

CString operator+(const CString &lhs, const CString &rhs)
{
	char *newString = new char[lhs.length() + rhs.length() + 1];
	strcpy(newString, lhs._pstr);
	return CString(strcat(newString, rhs._pstr));
}

ostream& operator<<(ostream &out, const CString &str)
{
	out << str._pstr;
	return out;
}

istream& operator>>(istream &in, CString &str)
{
	char temp[100];
	in >> temp;
	str._pstr = new char[strlen(temp) + 1];
	strcpy(str._pstr, temp);
	return in;
}
int main()
{
	CString str1 = "aaa";
	CString str2 = str1;
	CString str3;
	str3 = str1;
	CString str4 = str1 + str3;
	str4 = str1 + "bbb";
	str4 = "aaa+ str1;
	cout << str4 << endl;
	if (str4 > str1)
	{
		cout << "str4 > str1" << endl;
	}
	else
	{
		cout << "str4 < str1" << endl;
	}

	int len = str4.length();
	for (int i = 0; i < len; i++)
	{
		//str4.operator[](int index)
		cout << str4[i]<< " ";
	}
	cout << endl;

	//string   char[]
	char buf[1024] = { 0 };
	strcpy(buf, str4.c_str());
	cout << "buf:" << buf << endl;

	CString test;
	cin >> test;
	cout << test << endl;;

	CString test1 = "AAAA";
	cout << test1 << endl;
	cout << (test == test1) << endl;
	return 0;
}
27 original articles published, 22 praised, 553 visited
Private letter follow