#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; }这个程序哪里错了
时间: 2024-03-31 10:38:53 浏览: 33
这个程序没有明显的语法错误,但是在调用 `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 { public: Date(int y, int m, int d) { year = y; months = m; date = d; } ~Date() { } protected: int year; int months; int date; }; class Time :public Date { public: Time(int y, int m, int d, int h, int mi, int s) :Date(y, m, d) { hour = h; min = mi; sec = s; } void display() { int a = sec + 1; cout << "年:" << year << endl; cout << "月:" << months << endl; cout << "日:" << date << endl; cout << "时:" << hour << endl; cout << "分:" << min << endl; cout << "秒:" << a << endl; } ~Time(){ } private: int hour; int min; int sec; }; int main() { Time time1(2003, 8, 12, 7, 34, 23); time1.display(); return 0;优化这段代码
这段代码已经相对简洁明了了,但还是可以进行一些优化。以下是我提出的优化建议:
1. 使用初始化列表来初始化成员变量,而不是在构造函数中进行赋值操作。这样可以提高代码的执行效率。
2. 将析构函数定义为虚函数,以便于继承。
3. 将类的成员变量定义为私有的,通过公共的接口函数来访问和修改成员变量的值,这样可以提高代码的安全性。
4. 在输出时,可以使用"\n"代替endl,以提高代码的执行效率。
下面是优化后的代码:
```
#include <iostream>
using namespace std;
class Date {
public:
Date(int y, int m, int d) : year(y), months(m), date(d) {}
virtual ~Date() {}
int getYear() const { return year; }
int getMonth() const { return months; }
int getDate() const { return date; }
private:
int year;
int months;
int date;
};
class Time : public Date {
public:
Time(int y, int m, int d, int h, int mi, int s) : Date(y, m, d), hour(h), min(mi), sec(s) {}
virtual ~Time(){}
int getHour() const { return hour; }
int getMin() const { return min; }
int getSec() const { return sec; }
void display() const {
int a = sec + 1;
cout << "年:" << getYear() << "\n";
cout << "月:" << getMonth() << "\n";
cout << "日:" << getDate() << "\n";
cout << "时:" << hour << "\n";
cout << "分:" << min << "\n";
cout << "秒:" << a << "\n";
}
private:
int hour;
int min;
int sec;
};
int main() {
Time time1(2003, 8, 12, 7, 34, 23);
time1.display();
return 0;
}
```
#include <iostream> using namespace std; class Date { public: Date(int = 1, int = 1, int =2005); void display(); private: int month; int day; int year; }; Date::Date(int m, int d, int y) :month(m), day(d), year(y) { } void Date::display() { cout << month << "/" << day << "/" << year << endl; } int main() { Date d1(10, 13, 2005); Date d2(12, 30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; }
这段代码定义了一个名为Date的类,并在主函数中创建了四个Date类的对象d1、d2、d3和d4,然后分别调用了它们的display函数,将它们的月、日、年输出到屏幕上。
Date类有三个私有成员变量:月份、日期和年份,以及一个公有的构造函数和一个公有的display函数。构造函数有三个参数,分别代表月、日、年,其中月和日有默认参数1,年有默认参数2005。display函数将月、日、年输出到屏幕上,格式为“月/日/年”。
在主函数中,用不同的参数创建了四个Date类的对象,并依次调用它们的display函数,将它们的日期输出到屏幕上。其中d1是10月13日2005年,d2是12月30日2005年,d3是10月1日2005年,d4是1月1日2005年。