大一下学期核酸检测(文件)作业总结

通过这一次作业,有了更多的收获。

1、对于函数的概念有了更加深刻的理解,开头定义函数的类型不一定是标准的系统自带返回值类型或者是void类型,他可以是用户的自定义类型,也就是将你自己定义的一个类作为返回值。

class Date{
private:
    int year,month,day;
public:
    Date(int y,int m,int d){
    year=y;
    month=m;
    day=d;
    }
    Date(){
    year=0;
    month=0;
    day=0;
    }
class Data{
private:
    string ID,name,mobile,fruit;
    int checked;
    Date date;
public:
    Data(string id,string n,string m,int c,Date d,string f);
    Data();
    string getID(){ return ID; }
    string getname() { return name; }
    string getmobile() { return mobile; }
    int getchecked() { return checked; }
    Date getdate() { return date; }                      这里就是返回只为一个类的对象
    string getfruit() { return fruit; }
    void setc(int c,Date d,string f);
    bool operator<(const Data&D)const;
    friend ostream&operator<<(ostream&out,Data &a);
};

2、对于重载运算符有了更深入的了解(一些原来忽略的地方逐渐重视了)

比如说重载输入输出与运算符时必须使用友元函数,而使用友元函数时,由于他实际上不是类中的成员函数,所以不需要在类外写函数体时加上作用域“::”

假如重载运算符时没有使用友元函数,这时就需要你在类外写函数体时加上作用域"::"。

ostream&operator<<(ostream &out,Data &a){
       out<<"Date:[ID:"<<a.getID();
       out<<",name:"<<a.getname();
       out<<",mobile:"<<a.getmobile();
       out<<",checked:"<<a.getchecked();
       out<<",Date:("<<a.getdate().getyear()<<"."<<a.getdate().getmonth()<<"."<<a.getdate().getday();
       out<<"),fruit:"<<a.getfruit()<<"]"<<endl;
       return out;
bool Data::operator<(const Data&D)const{
      return checked<D.checked;
}

#include<iostream>
#include<fstream>
using namespace std;
class Date
{
public:
	Date(int year1=2022,int month1=10,int day1=12){year=year1;month=month1;day=day1;	}
	int year;
	int month;
	int day;
	friend ostream& operator <<(ostream&out,Date& date2);  //这个友元函数假如放在data类那里,就会报错
};
class Data 
{
public:
	Date getdate(){ cout<< date; }
private:
	Date date;

};
ostream &operator <<(ostream&out,Date& date2)
{out<<date2.year<<"年"<<date2.month<<"月"<<date2.day<<"日";
}
//Date Data::getdate() { return Date; }
int main()
{ 
Data a;
Date s;
//cout<<s<<endl;
a.getdate();
return 0;
}

重载运算符之后,有关这个类的对象的操作会遵循新重载的运算符的含义进行处理。

此外,友元函数要放在所要修改含义的对应类的位置。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛哥带你学代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值