friend void display(Date &d,Time &t);
时间: 2024-05-19 14:15:49 浏览: 18
Sorry, as an AI language model, I do not have access to the complete code of the program you are referring to. Can you please provide more information or context so I can better understand the question and provide a helpful response?
相关问题
#include <iostream> using namespace std; class Date; class Time {public: Time(int,int,int); void display(Date&); private: int hour; int minute; int sec; }; class Date {public: Date(int,int,int); friend void display(Date & d); private: int month; int day; int year; }; Time::Time(int h,int m,int s) {hour=h; minute=m; sec=s; } Date::Date(int m,int d,int y) {month=m; day=d; year=y; } void Time::display(Date &d) {cout<<d.month<<'.'<<d.day<<','<<d.year<<endl; cout<<hour<<':'<<minute<<':'<<sec<<endl;} int main() {Time t1(10,13,56); Date d1(12,25,2004); t1.display(d1); return 0; }这个程序哪里错了
这个程序没有明显的语法错误,但是在调用 `t1.display(d1)` 函数时,应该将参数类型改为 `Date&`,而不是 `Time&`。因为 `display()` 函数是在 `Time` 类中定义的,需要传入一个 `Date` 类型的对象来显示日期,所以应该修改为 `void display(Date&);`,并在调用时传入 `Date` 类型的对象。
如果这里算作是错误的话,还可以指出`display()` 函数的友元声明应该是 `friend void display(Date&);`,而不是 `friend void display(Date & d);`,这里多了个空格。但这个问题不会导致程序出错,只是一种代码风格问题。
#include <iostream> using namespace std; class Date; class Time {public: Time(int,int,int); friend void display(const Date &,const Time &); private: int hour; int minute; int sec; }; Time::Time(int h,int m,int s) {hour=h; minute=m; sec=s; } class Date {public: Date(int,int,int); friend void display(const Date &,const Time &); private: int month; int day; int year; }; Date::Date(int m,int d,int y) {month=m; day=d; year=y; } void display(const Date &d,const Time &t) { cout<<d.year<<"/"<<d.month<<"/"<<d.day<<endl; cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; } int main() { Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0; }
这段代码实现了一个简单的日期时间显示程序。其中定义了两个类:Date 和 Time,分别表示日期和时间。类中定义了构造函数,用于初始化对象的成员变量。类中还定义了友元函数 display,用于输出日期和时间。在主函数中,创建了一个时间对象 t1 和一个日期对象 d1,并将它们作为参数传递给 display 函数。最终输出的结果为:2004/12/25 和 10:13:56。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)