鱼C论坛

 找回密码
 立即注册
查看: 2124|回复: 2

[已解决]类的问题

[复制链接]
发表于 2021-6-30 15:21:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 傾小靈 于 2021-6-30 15:43 编辑
  1. #include<iostream>
  2. using namespace std;


  3. class Time
  4. {
  5.         public: Time(int h,int m,int s)
  6.                 {
  7.                         hours = h;
  8.                         minutes = m;
  9.                         seconds = s;
  10.                 }

  11.         void display()
  12.         {
  13.                 cout<<"出生时间: "<<hours<<"时"<<minutes<<"分"<<seconds<<"秒"<<endl;
  14.         }

  15.         protected:
  16.                 int hours,minutes,seconds;
  17. };


  18. class Date
  19. {
  20.         public:
  21.                 Date(int m,int d,int y)
  22.                 {
  23.                         month = m;
  24.                         day = d;
  25.                         year = y;
  26.                 }
  27.                 void display()
  28.                 {
  29.                         cout<<"出生年月: "<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
  30.                 }
  31.         protected:
  32.                 int month,day,year;
  33. };

  34. class Birthtime: public Time, public Date
  35. {
  36.         public:
  37.                 string childname;
  38.                 Birthtime(string childname, int year, int month, int day, int hours, int minutes, int seconds);

  39.         void display()
  40.         {
  41.                 cout << childname<<endl;
  42.                 Date::display();
  43.                 Time::display();
  44.         }
  45.        
  46. };


  47. int main()
  48. {
  49.         Birthtime N[] = { Birthtime("张三", 2002, 1, 8, 6, 6, 6),Birthtime("李四", 2002, 5, 28, 8, 8, 58),Birthtime("王五", 2002, 12, 7, 8, 9, 11) };
  50.         for (int i = 0; i < 3; i++)
  51.         {
  52.                 cout << "孩子的详细信息为  名字  ";
  53.                 N[i].display();
  54.         }
  55.         return 0;
  56. }
复制代码


目的是用类表示孩子的生日日期和姓名
编译器显示[Error] ld returned 1 exit status
要怎么改?
救救孩子吧!
最佳答案
2021-6-30 17:45:45
  1. Birthtime(string childname, int year, int month, int day, int hours, int minutes, int seconds);    //这里构造函数没有定义
复制代码

改为
  1. Birthtime(string childname, int year, int month, int day, int hours, int minutes, int seconds) :
  2.         Time(hours, minutes, seconds), Date(month, day, year), childname(childname) {}
复制代码


111111111.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-30 17:45:45 | 显示全部楼层    本楼为最佳答案   
  1. Birthtime(string childname, int year, int month, int day, int hours, int minutes, int seconds);    //这里构造函数没有定义
复制代码

改为
  1. Birthtime(string childname, int year, int month, int day, int hours, int minutes, int seconds) :
  2.         Time(hours, minutes, seconds), Date(month, day, year), childname(childname) {}
复制代码


111111111.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-30 18:00:44 From FishC Mobile | 显示全部楼层
谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 11:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表