交易记录类(重载输入输出流)

#include<bits/stdc++.h>
using namespace std;
class Time//时间类
{
 public:
    Time operator = (Time & t)//重载赋值运算符
    {
     month=t.month;
     day=t.day;
     hour=t.hour;
     minute=t.minute;
    }
 Time()//无参构造函数
    {
        month=day=hour=minute=0;
    }
 Time(int mo,int d,int h,int mi):month(mo),day(d),hour(h),minute(mi){}//带参构造函数
 //get&set函数
 void setMonth(int mo){month=mo;}
 void setDay(int d){day=d;}
 void setHour(int h){hour=h;}
 void setMinute(int mi){minute=mi;}
 void setTime(int mo,int d,int h,int mi){month=mo;day=d;hour=h;minute=mi;}


 int getMonth(){return month;}
 int getDay(){return day;}
 int getHour(){return hour;}
 int getMinute(){return minute;}


 //重载输出流
friend ostream &operator<<(ostream&out,Time&p);
//重载输入流
friend istream &operator>>(istream&in,Time &p);


    void displayTime()//输出,无换行
    {
        cout<<month<<" "<<day<<" "<<hour<<" "<<minute;
    }
  private:
 int month;//月
 int day;//日
 int hour;//小时
 int minute;//分钟
 };


ostream&operator<<(ostream&out,Time&p)//输出,不换行
    {
        out<<p.month<<" "<<p.day<<" "<<p.hour<<" "<<p.minute<<" ";
        return out;
    }
istream&operator>>(istream&in,Time&p)//输入
{
    in>>p.month>>p.day>>p.hour>>p.minute;
    return in;
}

class Record//记录类
{
int no;
Time date;
string type;
int jiner;//交易金额
double yue;
public:
    Record operator = (Record & t)//重载赋值运算符
    {
        no=t.no;
        type=t.type;
        date=t.date;
        jiner= t.jiner;
        yue= t.yue;
    }
Record():date(0,0,0,0)//无参构造函数
{//在一个类中初始化另一个类需用初始化列表的形式
no=0;type="存钱";jiner=0;yue=0;
}



Record(int n,int mo,int d,int h,int mi,string t,int j,double y):no(n),date(mo,d,h,mi),type(t),jiner(j),yue(y){}//有参构造函数,注意时间类的初始化方式



Record(int n,Time da,string t,int j,double y):no(n),date(da),type(t),jiner(j),yue(y){}//有参构造函数,注意时间类的初始化方式



friend istream &operator>>(istream &putin,Record &r);
friend ostream &operator<<(ostream &putout,Record &r);


//get&set函数
void setNo(int n){no=n;}
void setType(string t){type=t;}
void setJiner(int j){jiner=j;}
void setYue(int y){yue=y;}
void setDate(Time da){this->date=date;}
void setRecord(int n,Time d,string t,int j,double y){
    no=n;
    date=d;
    type=t;
    jiner=j;
    yue=y;
}



int getNo(){return no;}
int getJiner(){return jiner;}
double getYue(){return yue;}
string getType(){return type;}



void display()
{
    cout<<no<<" ";
    date.displayTime();//输出时间,不要换行
    cout<<" "<<type<<" "<<jiner<<" "<<yue<<endl;
}
};


istream &operator>>(istream&putin,Record &r)
{
    putin>>r.no>>r.date>>r.type>>r.jiner>>r.yue;;
    return putin;
}

ostream &operator<<(ostream&putout,Record&r)
{
    putout<<r.no<<" "<<r.date<<" "<<r.type<<" "<<r.jiner<<" "<<r.yue<<endl;
    return putout;
}

int main()
{
freopen("in.dat","r",stdin);
freopen("out.dat","w",stdout);

int no;
string type;
int jiner;//交易金额
double yue;
int month;
int day;
int hour;
int minute;


Time t1(4,23,8,5);
Time date;
date=t1;

cin>>no>>type>>jiner>>yue;
Record r1(no,date,type,jiner,yue);
cout<<r1;
Record r2;
cin>>r2;
cout<<r2;
return 0;
}


展开阅读全文

没有更多推荐了,返回首页