定义一个日期(Date)类和一个时间(Time)类,分别定义私有数据成员年、月、日和时、分、秒,通过成员函数的方式输出年、月、日和时、分、秒,实现指定的功能。
①定义构造函数、析构函数和拷贝构造函数。
②定义display公有成员函数,通过display函数分别输出年、月、日和时、分、秒。
③类的对象如何引用私有数据成员,定义成员函数。
通过公有成员函数对私有数据成员赋值;
通过函数返回值类型为引用的成员函数对私有数据成员赋值;
利用函数返回值访问私有数据成员;
利用形式参数为指针访问私有数据成员;
利用形式参数为引用访问私有数据成员。
代码仅供参考
#include <iostream>
using namespace std;
class Date
{
public:
Date()//定义构造成员函数,函数名与类名相同
{
year = 0;
month = 0;
day = 0;
}
Date(const Date&d)//定义拷贝构造函数
{
year = d.year;
month = d.month;
day = d.day;
}
~Date()//定义析构函数
{
cout << "析构函数调用……" << endl;
}
void getValue1()
{
cin >> year >> month >> day;
}
//通过函数返回值类型为引用的成员函数对私有数据成员赋值
int &changeY()
{
return year;
}
int &changeM()
{
return month;
}
int &changeD()
{
return day;
}
void display1()
{
cout << "1、通过公有成员函数对私有数据成员赋值: " << endl;
cout << "year: " << year << endl;
cout << "month: " << month << endl;
cout << "day: " << day << endl;
}
//通过函数返回值访问私有数据成员
int dateY()
{
return year;
}
int dateM()
{
return month;
}
int dateD()
{
return day;
}
//利用形式参数为指针访问私有数据成员
void getValue1(int *y, int *m, int *d)
{
*y = year;
*m = month;
*d = day;
}
//利用形式参数为引用访问私有数据成员
void getValue3(int &y, int &m, int &d)
{
y = year;
m = month;
d = day;
}
private:
int year;
int month;
int day;
};
class Time
{
public:
Time()//定义构造成员函数,函数名与类名相同
{
hour = 0;
minute = 0;
sec = 0;
}
Time(const Time&e)//定义拷贝构造函数
{
hour = e.hour;
minute = e.minute;
sec = e.sec;
}
~Time()//定义析构函数
{
cout << "析构函数调用……" << endl;
}
void getValue2()
{
cin >> hour >> minute >> sec;
}
//通过函数返回值类型为引用的成员函数对私有数据成员赋值
int &changeH()
{
return hour;
}
int &changeM()
{
return minute;
}
int &changeS()
{
return sec;
}
void display2()
{
cout << "hour: " << hour << endl;
cout << "minute: " << minute << endl;
cout << "sec: " << sec << endl;
}
//通过函数返回值访问私有数据成员
int timeH()
{
return hour;
}
int timeM()
{
return minute;
}
int timeS()
{
return sec;
}
//利用形式参数为指针访问私有数据成员
void getValue2(int *h, int *m, int *s)
{
*h = hour;
*m = minute;
*s = sec;
}
//利用形式参数为引用访问私有数据成员
void getValue4(int &h, int &m, int &s)
{
h = hour;
m = minute;
s = sec;
}
private:
int hour;
int minute;
int sec;
};
int main()
{
//通过公有成员函数对私有数据成员赋值
cout << "请输入年、月、日、时、分、秒:" << endl;
Date date;
date.getValue1();
date.display1();
Time time;
time.getValue2();
time.display2();
cout << "---------------------------------------------------------";
cout << '\n';
//利用函数返回值访问私有数据成员
cout << "2、利用函数返回值访问私有数据成员: " << endl;
cout << "year:" << date.dateY() << endl;
cout << "month:" << date.dateM() << endl;
cout << "day" << date.dateD() << endl;
cout << "hour:" << time.timeH() << endl;
cout << "minute:" << time.timeM() << endl;
cout << "sec:" << time.timeS() << endl;
cout << "---------------------------------------------------------";
cout << '\n';
//利用形式参数为指针访问私有数据成员
cout << "3、利用形式参数为指针访问私有数据成员: " << endl;
int a,b,c,d,e,f;
date.getValue1(&a, &b, &c);
time.getValue2(&d, &e, &f);
cout << "year:" << a << endl;
cout << "month:" << b << endl;
cout << "day" << c << endl;
cout << "hour:" << d << endl;
cout << "minute:" << e << endl;
cout << "sec:" << f << endl;
cout << "---------------------------------------------------------";
cout << '\n';
//通过函数返回值类型为引用的成员函数对私有数据成员赋值
cout << "4、通过函数返回值类型为引用的成员函数对私有数据成员赋值: " << endl;
//date.changeY() = 2990; 改变数值,data.cahgneY的值也改变
date.changeY() = a;
date.changeM() = b;
date.changeD() = c;
time.changeH() = d;
time.changeM() = e;
time.changeS() = f;
cout << "year:" << date.dateY() << endl;
cout << "month:" << date.dateM() << endl;
cout << "day" << date.dateD() << endl;
cout << "hour:" << time.timeH() << endl;
cout << "minute:" << time.timeM() << endl;
cout << "sec:" << time.timeS() << endl;
cout << "---------------------------------------------------------";
cout << '\n';
//利用形式参数为引用访问私有数据成员
cout << "5、利用形式参数为引用访问私有数据成员" << endl;
date.getValue3(a, b, c);
time.getValue4(d, e, f);
cout << "year:" << a << endl;
cout << "month:" << b << endl;
cout << "day" << c << endl;
cout << "hour:" << d << endl;
cout << "minute:" << e << endl;
cout << "sec:" << f << endl;
cout << "---------------------------------------------------------";
cout << '\n';
}