C++第六节课作业

本文详细讲解了运算符重载的概念,如何通过函数调用来实现自定义类型数据的运算,以及特殊运算符如cin/cout、++/--、文本重载和[]的友元重载方法。实例演示了如何在Fox类中应用这些技巧,包括友元函数的声明与实现,以及流运算符的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.运算符重载:

    赋予运算符有操作自定义类型数据的功能

    运算符重载的本质就是函数调用 

    函数返回值由运算之后的值进行决定

    函数名    : operator 加上重载运算符组成函数名 如operator+,operator- 

    具体参数个数为需要重载函数的形式

#include<iostream>
#include<string>

using namespace std;
class Fox 
{
public:
      Fox(string name="", int age = 0) : name(name), age(age) {}//构造函数
	  void printf()
	  {
		  cout << name << "\t" << age << endl;
	  }
	   bool operator>(Fox fox)  //重载大于号
	  {
		  if (this->age>fox.age)
		  {

			return true;
		  }
		  else
		  {
			  return false;
	      }
	  
	  }
	   friend Fox operator+(Fox fox1, Fox fox2);//友元函数的声明
	   
protected:
	string name;
	int age;

};
Fox operator+(Fox fox1, Fox fox2)//友元重载+号的实现
{
	return Fox(fox1.name + fox2.name, fox1.age + fox2.age);
}
int main() 
{
	
	Fox yueyue("月", 18);
	Fox yueyue2("月2", 13);
	Fox yueyue3;
	if (yueyue>yueyue2)
	{
		cout << "yueyue大" << endl;
	}
	else
	{
		cout << "yueyue不大" << endl;
	}
	yueyue3 = yueyue + yueyue2;
	yueyue3.printf();
	while (1);
	return 0;
}

    2.特殊运算符重载 

    cin类型 istream类对象

    cout类型 ostream类对象

    流运算符 << >> 

   其他运算符
    = () -> [] 只能采用类的成员函数形式重载
   流重载采用友元方式

   .  .*  ?:   :: 运算符不能重载

#include<iostream>
#include<string>

using namespace std;
class Fox 
{
public:
      Fox(string name="", int age = 0) : name(name), age(age) {}//构造函数
	  void printf()
	  {
		  cout << name << "\t" << age << endl;
	  }

	   friend istream& operator>>(istream& in, Fox& fox);//友元函数的声明
	   friend ostream& operator<<(ostream& out, Fox& fox);//友元函数的声明
	   
protected:
	string name;
	int age;

};
istream& operator>>(istream& in,Fox& fox)//因为要修改数据类型所以传引用
{
	in >> fox.name >> fox.age;
	return in;//为了能做连续输入的操作 返回 in
}
ostream& operator<<(ostream& out, Fox& fox)
{
	out << fox.name << "\t" << fox.age;
	return out;//为了能做连续的输出 所以返回 ostream 流
}
int main() 
{
	Fox fox1;
	Fox fox2;
	cin >> fox1 >> fox2;
	cout << fox1 << fox2;
	while (1);
	return 0;
}

   ++ -- 运算符重载。

  在重载函数中填入无用的int 型参数  代表该为后置加加

#include<iostream>
#include<string>

using namespace std;
class Fox 
{
public:
      Fox(string name="", int age = 0) : name(name), age(age) {}//构造函数
	  void printf()
	  {
		  cout << name << "\t" << age << endl;
	  }

	   friend istream& operator>>(istream& in, Fox& fox);//友元函数的声明
	   friend ostream& operator<<(ostream& out, Fox& fox);//友元函数的声明
	   Fox operator++(int)//带表该为后置加加
	   {

		   return Fox(name,age++);
	   }
	   Fox operator++()//带表该为前置加加
	   {

		   return Fox(name, ++age);
	   }

protected:
	string name;
	int age;

};
istream& operator>>(istream& in,Fox& fox)//因为要修改数据类型所以传引用
{
	in >> fox.name >> fox.age;
	return in;//为了能做连续输入的操作 返回 in
}
ostream& operator<<(ostream& out, Fox& fox)
{
	out << fox.name << "\t" << fox.age;
	return out;//为了能做连续的输出 所以返回 ostream 流
}
int main() 
{
	Fox fox1;
	Fox fox2;
	Fox fox3;
	Fox fox4;
	cin >> fox1 >> fox2;
	cout << fox1 << fox2;
	fox3 = fox2++;
	cout << fox3;
	fox4=++fox1;
	cout << fox4;
	while (1);
	return 0;
}

   文本重载 语法是 unsigned long long  operator""

   例子

#include<iostream>
#include<string>
using namespace std;
unsigned long long operator""_day(unsigned long long num)
{
	return 24 * num;
}

int main() 
{

	int num = 1_day;//算出一个值 返回给变量


	return 0;
}

综合:

#include<iostream>
#include<string>
using namespace std;
class Double
{
public:
	Double(double num = 0.0) :num(num) {}//构造函数
	double& data() //修改这个类中数据成员提供的接口
	{
		return num;
	}
	friend  istream& operator>>(istream&in, Double& Vdouble);
	friend  ostream& operator<<(ostream&out, Double& Vdouble);
	Double operator+(const Double& num2)//加入const 是为了常量相加
	{
		return Double(this->num + num2.num);//通过类去做重载 
	}
	friend Double operator-(const Double& num1, const Double& num2)
	{
		return Double(num1.num - num2.num);
	}
	Double operator+=(const Double& num1) //我们的数据类型相加
	{
		return Double(this->num + num1.num);
	}
	Double operator+=(const double& num1) //和默认的类型数据相加
	{
		return Double(this->num + num1);
	}
	Double operator++(int) //后置加加
	{
		return Double(this->num++);
	}
	Double operator++()//前置加加
	{
		return Double(++(this->num));
	}
    int * operator&() //放回一个地址
	{
		return (int*)(&this->num);
	}

	Double operator-() 
	{
		return(-this->num);
	}
protected:

	double num;
};
 istream& operator>>(istream& in, Double& Vdouble)
{
	 in >> Vdouble.num;
	 return in;
}//流重载
 ostream& operator<<(ostream& out, Double& Vdouble)
 {
	 out << Vdouble.num;
	 return out;
 }

int main()
{

	Double arr1(1.1);//1.1
	Double arr2 = 2.1;//2.1
	Double arr3;//0.0
	Double arr=1.5;//1.5
	arr3 = arr1 - arr;//arr3=1.1-1.5
	cout << arr3 << arr1 << arr;
	cin >> arr1;
	cout << arr1;
	arr2 = ++arr;//arr2=2.5
	cout << arr2;//2.5
	cout << arr;//arr=2.5
	cout << &arr << endl;
	arr = -arr;
	cout << arr;//-2.5

	while (1);
	return 0;
}

[]运算符重载

#include<iostream>
using namespace std;

class Array
{
public:

	Array(int size)
	{
		arraybest = new int(size);
	}
	~Array()
	{
		delete[] arraybest;
	}
	int& operator[](int i)//重载中括号
	{
		return arraybest[i];
	}
	

protected:

	int* arraybest;

};
int main() 
{

	while (1);
	return 0;
}

作业:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值