鱼C论坛

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

[已解决]类的问题

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

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

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

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


class Time
{
        public: Time(int h,int m,int s)
                {
                        hours = h;
                        minutes = m;
                        seconds = s;
                }

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

        protected:
                int hours,minutes,seconds;
};


class Date
{
        public:
                Date(int m,int d,int y)
                {
                        month = m;
                        day = d;
                        year = y;
                }
                void display()
                {
                        cout<<"出生年月: "<<year<<"年"<<month<<"月"<<day<<"日"<<endl; 
                }
        protected:
                int month,day,year;
};

class Birthtime: public Time, public Date
{
        public:
                string childname;
                Birthtime(string childname, int year, int month, int day, int hours, int minutes, int seconds);

         void display()
        {
                cout << childname<<endl;
                Date::display();
                Time::display();
        }
        
};


int main()
{
        Birthtime N[] = { Birthtime("张三", 2002, 1, 8, 6, 6, 6),Birthtime("李四", 2002, 5, 28, 8, 8, 58),Birthtime("王五", 2002, 12, 7, 8, 9, 11) };
        for (int i = 0; i < 3; i++) 
        {
                cout << "孩子的详细信息为  名字  ";
                N[i].display();
        }
        return 0; 
}

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

111111111.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

111111111.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-30 18:00:44 From FishC Mobile | 显示全部楼层
谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 16:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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