注册 登录
编程论坛 C++教室

帮看看这个简单的程序为什么结果有乱码,如何修改

khaz 发布于 2011-07-05 21:50, 574 次点击
程序代码为:
#include<iostream>
using namespace std;

class Date {
 public:
   Date() {
      strcpy(year, "1900");
      strcpy(mon, "01");
      strcpy(year, "01");
   } Date(char *y, char *m, char *d) {
      strcpy(year, y);
      strcpy(mon, m);
      strcpy(date, d);
   }

   void print();

 private:
   char year[5];
   char mon[3];
   char date[3];
};
void Date::print()
{
   cout << year << "-" << mon << "-" << date << " ";
}

class Time {
 public:
   Time() {
      strcpy(houor, "00");
      strcpy(mini, "00");
      strcpy(second, "00");
   } Time(char *h, char *m, char *s) {
      strcpy(houor, h);
      strcpy(mini, m);
      strcpy(second, s);
   }

   void print() {
      cout << houor << "-" << mini << "-" << second << " ";
   }

 private:
   char houor[3];
   char mini[3];
   char second[3];
};



class DateTime:public Date, public Time {
 public:
   DateTime():a(2), Date(), Time() {
   } DateTime(char *yyyy, char *mm, char *dd, char *HH, char *MM, char *SS,
              int k):a(k), Date(yyyy, mm, dd), Time(HH, MM, SS) {
   }
   void print() {
      Date::print();
      Time::print();
      cout << endl;
      cout << a << endl;

   }


 private:
   int a;
};

int main()
{
   DateTime b;
   b.print();
   DateTime a("2011", "07", "05", "20", "05", "03", 3);
   a.print();
}
执行结果为:
01-01-樚?0 00-00-00
2
2011-07-05 20-05-03
3
3 回复
#2
lucky5635912011-07-06 07:33
申请的内存不够吧!
#3
rjsp2011-07-06 08:28
Date::Date() 中没有对 date 赋值。
#4
guoqingchun2011-07-07 14:01
class Date {
public:
   Date() {
      strcpy(year, "1900");
      strcpy(mon, "01");
      strcpy(date, "01");//////////////////////应该是date,你写成year了
   }
   Date(char *y, char *m, char *d) {
      strcpy(year, y);
      strcpy(mon, m);
      strcpy(date, d);
   }

   void print();

private:
   char year[5];
   char mon[3];
   char date[3];
};
1