代码之家  ›  专栏  ›  技术社区  ›  Toma

使用继承的c++语法

  •  1
  • Toma  · 技术社区  · 7 年前

    我对继承的类、构造函数和方法的语法有问题。

    我想实现一个class date和一个子class date\u ISO,它将以特定的顺序设置给定的日期、月份、年份,并通过一个方法将其写入字符串。 我觉得我的基类日期工作得很好,但我对派生的date\u ISO有问题

    如何使用正确的语法继承Date之类的构造函数,以及如何实现和执行它?

    课程日期代码:

    #ifndef DATE_HPP
    #define DATE_HPP
    #include <sstream>
    #include <string>
    #include <iostream>
    
    class Date
    {
    
        private:
        short day;
        short month;
        short year;
    
        public:
        std::string getDD(short day);
        std::string getMM(short month);
        std::string getYear(short year);
    
        Date(short day, short month, short year);
    
        virtual std::string format() =0;
        virtual ~Date() =0;
    
    };
    
            std::string Date::getDD(short day)
        {
            std::stringstream s_day;
    
            if(day < 10)
            {
            s_day << '0' << day;
            }
            return s_day.str();
        }
    
        std::string Date::getMM(short month)
        {
            std::stringstream s_month;
            if(month < 10)
            {
            s_month << '0' << month;
            }
            return s_month.str();
        }
    
    
        std::string Date::getYear(short year)
        {
            std::stringstream s_year;
            s_year << year;
            return s_year.str();
        }
    
        Date::Date(short day, short month, short year) : day(day), month(month), year(year){}
    
    #endif
    

    课程日期代码\u iso:

    #ifndef DATEISO_HPP
    #define DATEISO_HPP
    #include "Date.hpp"
    #include <sstream>
    #include <string>
    #include <iostream>
    
    class DateISO : public Date
    {
        public:
        std::string format();
        DateISO(short day, short month, short year);
    };
    
        std::string DateISO::format()
        {
            std::stringstream ss;
            ss << this->getYear() << this->getMM() << this->getDD();
            return ss.str();
        }
    
        DateISO::DateISO(short day, short month, short year){}
    
        Date::~DateISO()
        {
        }
    
    #endif
    

    后续问题: 我用这里的技巧编写了一个小代码,但我没有正确使用它。我正在尝试使用多态指针来帮助创建派生类的一些对象,但首先我想检查创建派生类的简单对象时发生了什么。它不编译,我的所有get方法都出现了“未定义引用”错误。

    我添加了新代码(旧代码不再相关,但仍在那里进行比较)。

    课程日期代码:

    #ifndef DATE_HPP
    #define DATE_HPP
    #include <sstream>
    #include <string>
    #include <iostream>
    
    class Date
    {
    
        private:
        short day;
        short month;
        short year;
    
        public:
        std::string getTT(short day);
        std::string getMM(short month);
        std::string getYear(short year);
    
        Date(short day, short month, short year);
    
        virtual std::string format() =0;
        virtual ~Date() =0;
    
    };
    
            std::string Date::getTT(short day)
        {
            std::stringstream s_day;
            this->day = day;
            if(day < 10)
            {
            s_day << '0' << day;
            } else {
            s_day << day; 
            }
            return s_day.str();
    
        }
    
        std::string Date::getMM(short month)
        {
            std::stringstream s_month;
            this->month = month;
            if(month < 10)
            {
            s_month << '0' << month;
            } else {
            s_month << month;
            }
            return s_month.str();
        }
    
    
        std::string Date::getYear(short year)
        {
            this->year = year;
            std::stringstream s_year;
            s_year << year;
            return s_year.str();
        }
    
        Date::Date(short day, short month, short year) : day(day), month(month), year(year)//prueft die Attribute
        {
            //some code
        }
    
    #endif
    

    类别日期代码ISO:

    #ifndef DateISO_HPP
    #define DATEISO_HPP
    #include "Date.hpp"
    #include <sstream>
    #include <string>
    #include <iostream>
    
    class DateISO : public Date
    {
        public:
        std::string getTT();
        std::string getMM();
        std::string getYear();
    
        std::string format();
        DateISO(short day, short month, short year);
        ~DateISO();
    };
    
    
    
        std::string DateISO::format()
        {
            std::stringstream ss;
            ss << DateISO::getYear() << DateISO::getMM() << DateISO::getTT();
            return ss.str();
        }
    
        DateISO::DateISO(short day, short month, short year) : Date(day, month, year){}
    
        DateISO::~DateISO()
        {
            //some code
        }
    
    #endif
    

    和测试干管代码:

    #include "DateISO.hpp"
    //#include "DateDE.hpp"
    #include "Date.hpp"
    #include <sstream>
    #include <string>
    #include <iostream>
    
    int main()
    {
        DateISO date_iso(5,9,2017);
        std::cout << date_iso.format();
    
    return 0;   
    }
    

    如何使用多态指针创建和管理对象? 我创建的对象有什么问题(这给了我一个错误)?

    我很抱歉问了这么长的问题,感谢您的帮助。

    2 回复  |  直到 7 年前
        1
  •  3
  •   Some programmer dude    7 年前

    您已经知道如何使用 构造函数初始化列表 就像你在 Date 构造函数。

    您可以用同样的方式“调用”父类构造函数。在你的情况下

    DateISO::DateISO(short day, short month, short year)
        : Date(day, month, year)  // "Call" the parent constructor
    {}
    
        2
  •  0
  •   Sean Burton    7 年前

    除了一些程序员的答案之外,还可以通过如下方式“使用”继承构造函数:

    class DateISO : public Date
    {
        using Date::Date;
    };
    

    这将重新声明子类中的所有基类构造函数,并且所有参数都将转发给基类构造函数,因此如果基类有多个构造函数,并且您只想继承其中一个,或者如果您的子类有其他必须初始化的成员,则这可能不合适,但是对于所有成员都在基类中而子类只添加方法的情况,这应该是可以的。请参阅' Inheriting Constructors '以获取更多信息。