【董哥云班课】继承与派生 题目2:DataTime类

在这里插入图片描述
公有继承:Class B :public A
派生类中public成员与protected成员访问权限不变

私有继承:Class B :private A
派生类中public成员与protected成员访问均变成私有的

保护继承:ClassB :protected A
派生类中public成员变成protected成员,protected成员不变

基类的private成员在派生类中均不可直接访问

这个题主要想记录一下一些语法啥的
题目代码

#include<iostream>
#include<sstream>
using namespace std;

string itoa(int n)
{
	stringstream int_string;
	string s;
	int_string << n;
	int_string >> s;
	return s;
}

//定义日期类
class Data
{
	int Year, Month, Day;
public:
	//对日期的初始化
	Data(int y = 2000, int m = 1, int d = 1)
	{
		Year = y;
		Month = m;
		Day = d;
	}
	//设置日期
	void Set_data(int y, int m, int d)
	{
		Year = y; Month = m; Day = d;
	}
	//日期按字符串存储
	void Get_data(string& data_str)
	{
		data_str.append(itoa(Year));
		data_str.append("/");
		data_str.append(itoa(Month));
		data_str.append("/");
		data_str.append(itoa(Day));
	}

};
//定义时间类
class Time
{
	int Hour, Minute, Second;
public:
	//对时间的初始化
	Time(int h = 0, int m = 0, int s = 0)
	{
		Hour = h; Minute = m; Second = s;
	}
	//设置时间
	void Set_time(int h, int m, int s)
	{
		Hour = h; Minute = m; Second = s;
	}
	//时间按字符串存储
	void Get_time(string& time_str)
	{
		time_str.append(itoa(Hour));
		time_str.append(":");
		time_str.append(itoa(Minute));
		time_str.append(":");
		time_str.append(itoa(Second));
	}
};
//定义派生类
class DataTime :public Data, public Time
{
public:
	DataTime()
	{ }
	//设置日期与时间
	void SetDataTime(int y, int m, int d, int h, int mi, int se)
	{
		Set_data(y, m, d);
		Set_time(h, mi, se);
	}
	//按字符串存储
	void GetDataTime(string& dt_str)
	{
		string data_str, time_str;
		Get_data(data_str);
		Get_time(time_str);
		dt_str.append(data_str);
		dt_str.append(" ");
		dt_str.append(time_str);
	}
};


int main()

{
	DataTime DT;
	string dt;
	DT.GetDataTime(dt);
	cout << dt << endl;
	dt.clear();
	DT.SetDataTime(2021, 4, 15, 22, 50, 30);
	DT.GetDataTime(dt);
	cout << dt;
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值