Reputation: 171
I'm having trouble with the syntax of an inherited classes and constructors and methodes in them.
I want to implement a class date and a child class date_ISO that would set a given day, month, year in a specific order and through a method write it into string. I thing my base class date works fine, but I'm having problems with the derived date_ISO
How do I use correct syntax to inherite a constructor like Date, and how do I implement and execute it?
code for class 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
code for class 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
Followup question: I have written a small code with the tips i got here and I don't use it properly. I'm trying to have a polymorphic pointer that would help create a few objects of the derived class but firstly I wanted to check what happends when I create a simple objekt of the derived class. It doesnt compile and I get a "undefined referance" error to all my get methods.
I add the new code (old code no longer relevant but is still there for comparison).
code for class 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 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
code for class 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
and code for the test main:
#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;
}
How do I use a polymorphic pointer to create and manage objekts? what is wrong with the object that I created (that gives me an error)?
I am sorry for the long question and am thankful for any help.
Upvotes: 1
Views: 163
Reputation: 960
In addition to Some Programmer Dude's answer, it is also possible to inherit a constructor with 'using' like so:
class DateISO : public Date
{
using Date::Date;
};
This will redeclare all the base classes constructors in the child class, and all arguments will be forwarded to the base constructor, so this probably wouldn't be suitable if your base class had multiple constructors and you only wanted to inherit one of them, nor if your child class has additional members which must be initialised, but for your case where all members are in the base class and the child class only adds methods this should be okay. See the section on 'Inheriting Constructors' for more information.
Upvotes: 0
Reputation: 409356
You already know how to use a constructor initializer list as you do it in the Date
constructor.
You "call" a parent class constructor just the same way. In your case
DateISO::DateISO(short day, short month, short year)
: Date(day, month, year) // "Call" the parent constructor
{}
Upvotes: 3