使用继承的 C++ 语法

c++ syntax using inheritance

本文关键字:C++ 语法 继承      更新时间:2023-10-16

我在继承的类和构造函数以及其中的方法的语法方面遇到了问题。

我想实现一个类日期和一个子类date_ISO,它将以特定顺序设置给定的日、月、年,并通过方法将其写入字符串。 我认为我的基类日期工作正常,但我在派生date_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

类 Date_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;   
}

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

我很抱歉这个冗长的问题,并感谢任何帮助。

您已经知道如何在构造函数中使用构造函数初始值设定项列表,就像在构造函数中一样Date

您以相同的方式"调用"父类构造函数。在您的情况下

DateISO::DateISO(short day, short month, short year)
: Date(day, month, year)  // "Call" the parent constructor
{}

除了一些程序员的回答之外,还可以继承一个带有"using"的构造函数,如下所示:

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

这将重新声明子类中的所有基类构造函数,并且所有参数都将转发给基构造函数,因此如果您的基类有多个构造函数并且您只想继承其中一个,或者如果您的子类具有必须初始化的其他成员,这可能不合适, 但是对于所有成员都在基类中并且子类仅添加方法的情况,这应该没问题。有关详细信息,请参阅"继承构造函数"部分。