Home c++ syntax using inheritance
Reply: 0

c++ syntax using inheritance

user5358
1#
user5358 Published in July 20, 2018, 4:47 am

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313534 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO