类的封装性 定义一个日期(Date)类和一个时间(Time)类

本文展示了如何使用C++定义一个`Date`类和一个`Time`类,包括构造函数、析构函数和拷贝构造函数的实现。类中包含了年、月、日和时、分、秒等私有数据成员,并提供了多种方式来访问和修改这些私有数据成员,如公有成员函数、返回引用的成员函数、指针和引用参数等。代码实例详细展示了类对象如何通过不同方法引用和操作私有数据成员。

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

定义一个日期(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';
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值