gpt4 book ai didi

使用继承的c++语法

转载 作者:行者123 更新时间:2023-11-30 02:21:32 29 4
gpt4 key购买 nike

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

我想实现一个类日期和一个子类 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 方法都收到“ undefined reference ”错误。

我添加了新代码(旧代码不再相关但仍用于比较)。

类日期代码:

#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

类 dateISO 的代码:

#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
{}

关于使用继承的c++语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48204133/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com