C++类和结构体运算符重载区别

运算符的重载

  1. 类重载
  2. 友元重载

下边我将用结构体和类两种方式来展示他们各自的重载方式。其中结构体只有一种重载方式相当于类中的友元重载,而类中分为类重载和友元重载

一、结构体里面的运算符重载方式:(用到了+和<<的运算符重载)

#include <iostream>
using namespace std;
struct score
{
	score(int math):math(math){}
	score(int math=0,int english=0):math(math),english(english){}
    //这里用了两个构造函数,只是为了说明,结构体里构造函数的个数也是可以不止一个的,第二个构造函数用到了缺省的方式,目的是为后边创造一个默认对象做准备(意思就是可以直接score C).
    void print()//方便打印
	{
		cout << math << "\t" << english;
	}
	int math;
	int english;
	void test();  //这里只是为了演示,如果在结构体外写内容,需要    类型+ 结构体名::函数名。
	
};
//结构体函数在类外实现
void  score::test()
{
    
}
//这里我们演示一下重载加法的方式:
score operator+(score A, score B)
{
	score C;
	C.math = A.math + B.math;
	C.english = A.english + B.english;
	return C;
}
//重载<<
ostream& operator<<(ostream& out, score C)
{
	out << C.math << "\t" << C.english;
	return out;
}
//说明一下,结构体的重载不能定义在结构体中,需要在外边说明,不能在结构体内定义。¥¥¥重点细节。
int main()
{
	score num1(90, 98);
	score num2(30, 60);
	cout << num1 + num2;
 	//通过这一个cout<<就完美展示了+和<<的运算符重载效果。
    //那么我们在用结构体自身函数试一下。
    score num3=num1+num2;
    cout<<num3.print();//这样打印出来的效果一样的。
	system("pause");
	return 0;
}

二、友元重载和类重载的区别

先简单说明一下,在类中,友元重载和类重载的区别,我们就以加法为例

若我们需要将两个类对象相加,还是用代码说明吧!

#include <iostream>
#include <string>
using namespace std;
class score
{
public:
    score(int math, int english) :math(math), english(english) {}
    //友元重载  比较常用
    friend score operator+(score num1, score num2)
    {
        //参数个数为运算符数目。
        score result(num1.math + num2.math, num1.english + num2.english);
        return result;
        
    }
    //类重载
    /*score operator+(score num2)
    {
        score result(this->math + num2.math,this->english + num2.english);
        return result;
    }*/
    void Print()
    {
        cout << math << "\t" << english;
    }
    int math;
    int english;
};

int main()
{
   
    score num1(1, 2);
    score num2(2, 3);
    score num3 = num1 + num2;
    num3.Print();
   //         $$$$$$$$$$$$$重点$$$$$$$$$$$$$
    //如果我们要将num1,num2相加。num1+num2;
//友元重载中:friend score operator+(score num1,score num2)这两个数都是作为参数存在的。翻译过程是将两个参数的值相加。
    
    //而在类重载中:score operator+(score num2)
    //翻译过程是 num1.operator(num2);

    system("pause");
    return 0;
}

类中友元重载以及重载的封装方式:用的+和<<来进行演示

#include <iostream>
#include <string>
using namespace std;
class MM
{
public:
	MM(string name="", int age=0) :name(name), age(age) {}
	//用友元方式进行重载:
	friend MM operator+(MM a, MM b)
	{
		MM c;
		c.name = a.name + b.name;
		c.age = a.age + b.age;
		//MM c(a.name+b.name,a.age+b.age);
        //这句话可以替代上边三句话。
		return c;
	}
	friend ostream& operator<<(ostream& out, MM mm)
	{
		out << mm.age << "\t" << mm.name;
		return out;
	}
	//采用封装的方式来实现一下。
	//void add(MM a, MM b)
	//{
	//	this->name = a.name + b.name;
	//	this->age = a.age + b.age;
	//}
	//friend MM operator+(MM a, MM b)
	//{
	//	MM c;
	//	c.add(a, b);
	//	return c;
	//}
	//void output(ostream& out)
	//{
	//	out << this->age << "\t" <<this->name;
	//}
	//friend ostream& operator<<(ostream & out, MM gg)
	//{
	//	gg.output(out);		//将功能封装给对象,
	//	return out;
	//}
protected:
	string name;
	int age;
};
int main()
{
	MM mm("wang",10);
	MM gg("jiang",20);
	MM h = mm + gg;
	cout << mm+gg;
	system("pause");
	return 0;
}

接着说明一个比较常用的++运算符的重载

++分为了前置++和后置++,那么他们的区别是什么呢?

#include <iostream>
using namespace std;
class MM
{
public:
	MM(int age=0) :age(age) {}
	MM operator++() //前置++
	{
			MM jt = ++this->age;//前后置本质区别。
			cout << "前置" << endl;
			return jt;
	}
	MM operator++(int)//后置++   (int)只是作为标识符。
	{	
			MM jt = this->age++;//前后置本质区别。
			cout << "后置" << endl;
			return jt;
	}
	int& getAge()
	{
		return this->age;
	}
protected:
	int age;
};
int main()
{
	MM mm(3);
	MM gg(0);
	gg=mm++;//这句话的本质是,gg得到的值是mm原来的值,后mm+1;
	cout << gg.getAge(); //结果是3
    cout << mm.getAge();//结果是4;
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_51721904/article/details/121585250