dimanche 29 novembre 2015

Multiple classes, output

i got a class for a date and a class for a point of time. Now i want to combine them. My problem is that i'm not able to get the output working, it always uses the initialized date. Here is my code:

main.cpp

#include <iostream>
#include <iomanip>

#include "date.hpp"
#include "time.hpp"

using namespace std;

int main() {
Datum d1;
Datum d2(03, 12, 2015);

cout << "d1: " << d1 << endl;
cout << "d2: " << d2 << endl << endl;

zeit z1;
cout << "z1: " << z1 << endl;
zeit z2(d2, 23, 30);
cout << "z2: " << z2 << endl;
return 0;
}

date.cpp

#include "date.hpp"
#include <iostream>

Datum::Datum(unsigned int d, unsigned int m, unsigned int y)
{
    day = d;
    month = m;
    year = y;
    return;
}
Datum::Datum() : Datum(1, 1, 2000) { return; }
unsigned int Datum::getday() { return day; }
unsigned int Datum::getmonth() { return month; }
unsigned int Datum::getyear() { return year; }

std::ostream& operator<<(std::ostream& os, Datum& z)
{
    os << z.getday() << ".";
    os << z.getmonth() << ".";
    os << z.getyear();
    return os;
}

date.hpp

#ifndef DATUM_HPP_
#define DATUM_HPP_
#include <iostream>

class Datum {
private:
    unsigned int day;
    unsigned int month;
    unsigned int year;
public:
    Datum(unsigned int, unsigned int, unsigned int);
    Datum();
    unsigned int getday();
    unsigned int getmonth();
    unsigned int getyear();

    friend std::ostream& operator<<(std::ostream&, Datum&);
};
#endif

time.cpp

#include "time.hpp"
#include <iostream>

zeit::zeit(Datum date, unsigned int h, unsigned int m)
{
    std::cout << date.getday() << "." << date.getmonth() << "." <<  date.getyear() << std::endl;
    min = m;
    hour = h;
    return;
}
zeit::zeit() : zeit(Datum(),0,0) { return; }
unsigned int zeit::getmin() { return min; }
unsigned int zeit::gethour() { return hour; }

std::ostream& operator<<(std::ostream& os, zeit& z)
{
    os << z.date << ", ";
    if (z.gethour() < 10)
        os << "0" << z.gethour();
    else
        os << z.gethour();
    os << ":";
    if (z.getmin() < 10)
        os << "0" << z.getmin();
    else
        os << z.getmin();
    return os;
}

time.hpp

#ifndef ZEIT_HPP_
#define ZEIT_HPP_
#include "date.hpp"

class zeit {
private:
    unsigned int min;
    unsigned int hour;
    Datum date;
public:
    zeit(Datum, unsigned int, unsigned int);
    zeit();
    unsigned int getmin();
    unsigned int gethour();

    friend class Datum;
    friend std::ostream& operator<<(std::ostream&, zeit&);
};
#endif

This is the output i get:

d1: 1.1.2000
d2: 3.12.2015

1.1.2000
z1: 1.1.2000, 00:00
3.12.2015
z2: 1.1.2000, 23:30

What am i doing wrong? Ty for any help!

Aucun commentaire:

Enregistrer un commentaire