#include<iostream>
using namespace std;
class Date;
//对Date类的提前声明
class Time
{
public:
Time(int m,int s);
operator=(Time &t1,Date &d1);
//重载运算符“=”的函数,用于Date类和Time类对象间的赋值。
private:
int minute,sec;
} ;
class Date
{
public:
Date(int y,int d);
friend Time::operator=(Time &t1,Date &d1);//声明运算符重载函数为该类的友元函数,可以访问该类的私有成员
private:
int year,day;
};
Time::Time(int m,int s):minute(m),sec(s){}
Date::Date(int y,int d):year(y),day(d){}
Time::operator=(Time &t1,Date &d1)
{
t1.m=d1.y;
t1.s=d1.d;
return t1;
}
int main()
{
Time t1;
Date d1(2,3);
t1=d1;
cout<<t1.m<<":"<<t1.s<<endl;
return 0;
}
这是我一开始编写的程序,我是想把d1(2,3)赋给t1,我看了书后想做个练习,希望大家可以教教我。谢谢···