Practice of operator overloading

For the built-in type, the compiler knows how to calculate, if you want to define a class by
Person class, private data Score built, and how the constructor C Person
= A + B; (where a, b are the object);

#include<iostream>
using namespace std;
class Person
{
public:
	Person() { score = 5; }
private:
	int score;
};

For built-in types, the compiler knows how to calculate, if you want by class defined a
need for + overloaded, you can define a Person & Plus (Person & p)
changed to achieve the object by value is clearly not a + b directly is more convenient . Need through
the use of operator + op manner provided by the compiler to overloaded operators to achieve the purpose,
may be in the form of function Person & operator + (Person & a ) of the definition of a class that implements
a single parameter passed can also Person operator + (Person & a, Person & b )
means to achieve a plurality of transmission parameters, specific codes are as follows:

#include<iostream>
using namespace std;
class Person
{
friend Person operator+(Person& a, Person& b);//访问私有数据
public:
	Person() { score = 5; }
private:
	int score;
};
Person operator+(Person& a, Person& b)//实现相加重载
{
	Person temp;
	temp.score = a.score + b.score;
	return temp;
}
int main()
{
    Person a,b;
    a=a+b;
    return 0;
}

Can direct operation of a + b, the addition can be envisaged with other operators such as overloading
score data directly output a left shift operator where the data is private because then the main
function directly output cout obviously want to use improbable by left shift operator overloading
can be achieved this content to achieve cout << a; forms do not usually use the letter by members
to implement the functions as member functions of the function can not meet cout << a number of forms so that cout
on the left side, and therefore may be realized by defining the content of global function.

#include<iostream>
using namespace std;
class Person
{
friend Person operator+(Person& a, Person& b);//访问私有数据
friend ostream& operator<<(ostream& cout, Person& p);//仅实现左移运算符号重载;
public:
	Person() { score = 5; }
private:
	int score;
};
ostream& operator<<(ostream& cout, Person& p)//实现左移重载
{
	cout << p.score;
	return cout;
}
int main()
{
    Person a;
    cout << a;
}

Direct self-energizing elements is achieved by defining a front ++.

class Person
{
friend Person operator+(Person& a, Person& b);//访问私有数据
friend ostream& operator<<(ostream& cout, Person& p);//仅实现左移运算符号重载;
friend Person& operator++(Person& a);//实现前置++重载;
public:
	Person() { score = 5; }
private:
	int score;
};
ostream& operator<<(ostream& cout, Person& p)//实现左移重载
{
	cout << p.score;
	return cout;
}
Person& operator++(Person& a)//实现前置递增重载;
{
	a.score++;
	return a;
}
int main()
{
    Person a;
    a++;
    cout <<a;
}

由于后置++时必须返回临时对象,重载左移运算符中第二个参数也
必须时使用值而非引用值,如果保持引用作为函数参数会使得cout
<<a++;尽管在第一次运行流畅这是由于编译器的优化,第二次实现
时就会崩溃这是用于编译器已经将这部分内存释放了是一个临时变
量拷贝给源对象使得源对象在执行内容后被释放内存;

#include<iostream>
using namespace std;
class Person
{
	friend void test(Person p);
	friend ostream& operator<<(ostream& cout, Person& p);//仅实现左移运算符号重载;
	friend Person operator+(Person& a, Person& b);
	friend Person& operator++(Person& a);//实现前置++重载;
public:
	Person() { score = 5; }
	Person operator++(int)
	{
		Person temp = *this;
		score++;
		return temp;
	}//实现后置++重载
private:
	int score;
};
void test(Person p)
{
	cout << p.score;
}
int main()
{
    Person a;
    a++;
    test(a);  
    return 0;  
}

本文内容粗糙,可以学习b站上黑马程序员相关这部分内容。

Guess you like

Origin www.cnblogs.com/pekkasuper/p/12590108.html