void Print() const { cout << _year << "-" << _month << "-" << _day << endl; }//在这里const的作用是什么
时间: 2024-06-13 13:07:22 浏览: 5
在这个例子中,`void Print() const`中的`const`关键字的作用是修饰`this`指针,使得`this`指向的成员变量不能被改变。也就是说,如果在`Print()`函数中有任何尝试修改成员变量的操作,编译器都会报错。这样做的好处是可以保证`Print()`函数不会对对象的状态进行修改,从而保证了函数的安全性和可靠性。
举个例子,如果我们在`Print()`函数中尝试修改成员变量`_year`的值,编译器就会报错,因为`Print()`函数被声明为`const`,而`const`成员函数不能修改成员变量的值。
```cpp
void Print() const
{
_year = 2022; // 编译器会报错
cout << _year << "-" << _month << "-" << _day << endl;
}
```
相关问题
#include <iostream> #include <string> using namespace std; class Date { friend class person; public: int year,month,day; friend ostream & operator <<( ostream&os, const Date&date ) { os<<date.year<<"_"<<date.month<<"_"<<date.day; return os; } Date(int year=2000,int month=1,int day=1):year(year),month(month),day(day) {} void print() { cout<<year<<","<<month<<","<<day; } }; class person { public: string Name; string Num; Date brith; person(string Name="张三", string Num="123456789", int year=2000,int month=1,int day=1): Name(Name),Num(Num),brith(year,month,day) {} string GetName() { return Name; } string GetNum() { return Num; } Date Getbrith() { return brith; } }; bool compare(person&p1,person&p2) { if(person&p1.year>person&p2.year) { return true; } else { return false; } } if(compare(person&p1.year,person&p2.year)) { cout<<"p1的年龄大"<<endl; } else { cout<<"p2的年龄大"<<end; } class student: private person { private: int StuNum; }; class teacher:public person { private: Date Time; }; class CTA :public student,public teacher { private: int No; }; int main() { person p1("张三","123456789",2000,1,1); person p2("李四","123456789",2001,1,1); cout<<"序号"<<endl; cout<<" 1 "<<endl; cout<<" "<<p1.GetName()<<endl; cout<<" "<<p1.GetNum()<<endl; cout<<" "<<p1.Getbrith()<<endl; cout<<" 2 "<<endl; cout<<" "<<p2.GetName()<<endl; cout<<" "<<p2.GetNum()<<endl; cout<<" "<<p2.Getbrith()<<endl; }有什么问题
该代码存在以下问题:
1. 在比较函数 `compare` 中,使用了 `person&p1.year` 和 `person&p2.year`,这个语法是错误的,应该是 `p1.brith.year` 和 `p2.brith.year`。
2. 类 `student` 和 `teacher` 的继承方式应该改为 `public`,否则无法访问基类中的成员函数和数据成员。
3. 类 `CTA` 继承了 `student` 和 `teacher`,但是这两个类都继承了 `person`,这就导致 `CTA` 中有两份 `person` 的数据成员,这样会导致访问不确定性和冗余。应该将 `student` 和 `teacher` 改为继承自一个共同的基类,这个基类中包含 `person` 类型的数据成员和成员函数。
4. 在 `main` 函数中,没有输出序号 3。
5. 在输出日期时,应该使用 `brith.print()` 而不是 `cout<<brith`,因为 `brith` 是一个 `Date` 类型的对象,没有定义 `<<` 运算符重载。
6. 在输出年龄大小时,应该使用 `p1.Getbrith().year` 和 `p2.Getbrith().year` 来获取出生年份,然后进行比较,而不是使用 `compare` 函数。
7. 类 `CTA` 中的数据成员 `No` 没有被使用,可能是不必要的。
⑴ 完善“hdate.h”中类的定义: class Date { private: int year,month,day; public: void set_date(int y=2000,int m=1,int d=1) //对数据成员赋值 { year = y; month = m; day = d; } int get_year() //返回year { return year; } int get_month() //返回month { return month; } int get_day() //返回day { return day; } int isleapyear(void); //是闰年返回1,不是闰年返回0 void print_date(void) {cout<<year<<'-'<<month<<'-'<<day<<endl;} }; int Date::isleapyear(void) //是闰年返回1,不是闰年返回0 { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return 1; } else { return 0; } } ⑵ 按注释要求完善下列程序(exp_103.cpp) #include<iostream.h> #include"hdate.h" void main(void) { Date da1,da2; int y,m,d; da1.set_date(2004,5,1); da1.print_date(); cout<<"year=";cin>>y; cout<<"month=";cin>>m; cout<<"day=";cin>>d; //调用方法set_date(),用消息y,m,d对da2的数据成员赋值 //调用方法输出将da2用“ 年 月 日”格式输出年月日 //调用方法输出da2的年是否为闰年 }
⑴ 完善“hdate.h”中类的定义:
```cpp
class Date
{
private:
int year, month, day;
public:
void set_date(int y = 2000, int m = 1, int d = 1) // 对数据成员赋值
{
year = y;
month = m;
day = d;
}
int get_year() // 返回year
{
return year;
}
int get_month() // 返回month
{
return month;
}
int get_day() // 返回day
{
return day;
}
int is_leap_year() const; // 是闰年返回1,不是闰年返回0
void print_date() const // 输出日期
{
cout << year << '-' << month << '-' << day << endl;
}
};
int Date::is_leap_year() const // 是闰年返回1,不是闰年返回0
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 1;
}
else
{
return 0;
}
}
```
⑵ 按注释要求完善下列程序(exp_103.cpp)
```cpp
#include <iostream>
#include "hdate.h"
using namespace std;
int main()
{
Date da1, da2;
int y, m, d;
da1.set_date(2004, 5, 1);
da1.print_date();
cout << "year=";
cin >> y;
cout << "month=";
cin >> m;
cout << "day=";
cin >> d;
da2.set_date(y, m, d); // 调用方法set_date(),用消息y,m,d对da2的数据成员赋值
cout << "da2: ";
da2.print_date(); // 调用方法输出将da2用“年 月 日”格式输出年月日
cout << "da2's year is leap year? ";
if (da2.is_leap_year()) // 调用方法输出da2的年是否为闰年
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
return 0;
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)