dimanche 24 juin 2018

trying to get different outputs from polymorphic function

Hi StackOverflow community!
I'm expecting different outputs depending on the class of object to be printed but that's not the case.

Ive got the base class Medium:

#include "Medium.h"
#include <string>
#include <iostream>

unsigned int Medium::currentID = 1;

Medium::Medium(std::string initTitel): titel(initTitel), status(false)
{
    ID = currentID++;
}

Medium::~Medium(void)
{
}


void Medium::ausgabe() const
{
    std::cout << "ID: " << ID << std::endl;
    std::cout << "Titel: " << titel << std::endl;
    switch (status)
    {
    case true:
        std::cout << "Status : Das Medium ist seit dem " 
                  << datumAusgeliehen  << " an " 
                  << personAusgeliehen.getName() << " ausgeliehen." 
                  << std::endl;
        break;

    case false:
        std::cout << "Status: Medium ist zurzeit nicht verliehen." << std::endl;
        break;
    }
}  

The function prints ID, title and status to console.
Now depending on the type of Medium to be printed, i would like to add extra information to be printed. E.g. if the Medium is a book, information about author should be printed too. Now ive got an extra subclass called Buch which also has an output function:

"Medium.h"

#include "Medium.h"
class Buch: public Medium
{
public:
    Buch();
    Buch(std::string initTitel, std::string initAutor);
    virtual ~Buch();

    void ausgabe() const;

private:
    std::string autor;
};  

Cpp:

#include "Buch.h"
Buch::Buch(std::string initTitel, std::string initAutor): Medium(initTitel), 
autor(initAutor)
{
// TODO Auto-generated constructor stub
}

Buch::~Buch()
{
    // TODO Auto-generated destructor stub
}

void Buch::ausgabe() const
{
    Medium::ausgabe();
    std::cout << "Autor: " << autor << std::endl;
}  

As far as I know due to class Buch when calling the output function ausgabe the extra information autor should be added automatically. thanks for any help. :)

Aucun commentaire:

Enregistrer un commentaire