日复一日 - C/C++ 类与抽象
时间: 2023-09-13 21:08:26 浏览: 66
好的,你的问题是关于 C/C++ 类与抽象的。类是 C++ 中面向对象编程的基本概念之一,它允许我们将数据和操作数据的函数组合成一个完整的数据类型。抽象是指隐藏类的实现细节,使得用户只需要关注类的公共接口,而不需要了解其内部实现。
在 C++ 中,我们可以通过定义纯虚函数来实现抽象类。纯虚函数是一个没有实现的虚函数,它的存在只是为了让子类来实现它。如果一个类中至少有一个纯虚函数,那么这个类就是抽象类,不能被实例化。
下面是一个简单的抽象类的例子:
```c++
class Shape {
public:
virtual double area() const = 0; // 纯虚函数
virtual double perimeter() const = 0; // 纯虚函数
};
```
在这个例子中,Shape 类是一个抽象类,它有两个纯虚函数 area() 和 perimeter(),这两个函数没有实现,它们的实现留给子类去完成。由于 Shape 类是抽象类,不能被实例化,只能作为基类来派生出其他类。
希望这个例子可以回答你的问题。如果你还有其他问题,请随时问我。
相关问题
编译器:C++ (g++) 改造练习13-1(日复一日)中的Date类并提交,使其可以与一个整数n相加或相减,得到该日期N天后/前的日期。 提示: 请参考题目(日复一日)中的Date类实现; 注意考虑闰月; 整数n的取值范围为[1,10000]。 裁判测试程序样例: #include <iostream> #include <string> #include <assert.h> using namespace std; //在此处补充Date类的定义 int main() { int y, m, d; cin >> y >> m >> d; Date d1(y,m,d); int n; cin >> n; cout << d1.toText() << " + " << n << " = " << (d1 + n).toText() << endl; cout << d1.toText() << " - " << n << " = " << (d1 - n).toText() << endl; return 0; } 输入样例: 2022 8 31 2 说明:意为求2022年8月31日的后两天和前两天的日期。 输出样例: 2022-8-31 + 2 = 2022-9-2 2022-8-31 - 2 = 2022-8-29 请注意:函数题只需要提交相关代码片段,不要提交完整程序。
```cpp
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
class Date {
private:
int year, month, day;
static const int monthDays[13];
public:
Date(int y = 1, int m = 1, int d = 1) {
year = y;
month = m;
day = d;
}
Date(const Date& date) {
year = date.year;
month = date.month;
day = date.day;
}
Date& operator=(const Date& date) {
year = date.year;
month = date.month;
day = date.day;
return *this;
}
bool isLeap() const {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int monthDaysNum() const {
if (month == 2 && isLeap()) {
return 29;
}
return monthDays[month];
}
void addOneDay() {
day++;
if (day > monthDaysNum()) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
void minusOneDay() {
day--;
if (day == 0) {
month--;
if (month == 0) {
month = 12;
year--;
}
day = monthDaysNum();
}
}
string toText() const {
string res = "";
res += to_string(year) + "-";
if (month < 10) {
res += "0";
}
res += to_string(month) + "-";
if (day < 10) {
res += "0";
}
res += to_string(day);
return res;
}
Date operator+(int n) const {
Date res = *this;
while (n--) {
res.addOneDay();
}
return res;
}
Date operator-(int n) const {
Date res = *this;
while (n--) {
res.minusOneDay();
}
return res;
}
};
const int Date::monthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int main() {
int y, m, d;
cin >> y >> m >> d;
Date d1(y,m,d);
int n;
cin >> n;
cout << d1.toText() << " + " << n << " = " << (d1 + n).toText() << endl;
cout << d1.toText() << " - " << n << " = " << (d1 - n).toText() << endl;
return 0;
}
```
颁奖词100字关于石油工人要求立意高
品的颁奖词
尊敬的各位来宾、亲爱的石油工人们:
今天,我们在这里举行隆重的颁奖仪式,为那些不畏风雨、默默无闻、为石油事业奉献毕生精力的石油工人们颁发奖励。
石油工人是我们国家经济建设的中坚力量,是为我们现代化事业不断奋斗的伟大奉献者。他们在油田上日复一日地辛勤耕耘,用自己的劳动兑现了国家的发展梦想。
从长达半个世纪的沧桑历程中,我们领悟到:立意高品是一种信仰,是一种追求,也是一种坚守。只有不断提高自身的品质,才能在激烈的市场竞争中立于不败之地,成就卓越。
因此,我谨代表全国石油工人,向那些不断提高自身品质、勇往直前、追求卓越的石油工人们致敬!你们是我们前进的动力,我们的光荣使命。
最后,我衷心祝愿全体石油工人在新的一年里,继续保持立意高品的信念和毅力,再创辉煌!