why derivates class don't take parameters from previous class? C++ CLASS

Viewed 70

i need some help, i don't understand why this code don't working, it seems like if the derivates class don't see the parameters introducted from the previous class, but use "empty constructor" instead the constructor. Indeed it compiles, but the output present wrong parts.

This is the code

#include <iostream>
#include <string>
using namespace std;

//first class

class persona 
{
    public:
        persona() : m_nome("nome"), m_cognome("cognome") {;}
        persona(string nome, string cognome) 
            : m_nome(nome), m_cognome(cognome) {;}
        string nome() {return m_nome;}
        string cognome() {return m_cognome;}

    protected:
        string m_nome;
        string m_cognome;
};

//derivate class 

class dipendente : public persona 
{
    public:
        dipendente() : m_assunzione("assunzione"), m_stipendio(0) {;}
        dipendente(string nome, string cognome, string assunzione, double stipendio) 
            : m_assunzione(assunzione), m_stipendio(stipendio) {;}
        string assunzione() {return m_assunzione;}
        double stipendio() {return m_stipendio;}
    
    protected:
        string m_assunzione;
        double m_stipendio;

};

//derivate class

class dirigente : public dipendente
{
    public:
        dirigente() : m_livello("livello"), m_campo("campo") {;}
        dirigente(string nome, string cognome, string assunzione, double stipendio, string livello, string campo) 
            : m_livello(livello) , m_campo(campo) {;}
        string livello() {return m_livello;}
        string campo() {return m_campo;}

    protected:
        string m_livello;
        string m_campo;
};

//overloading operator<<

std::ostream &operator<<(std::ostream &stream, persona c)
{
    stream << "Dati di\n" << c.nome() << " " << c.cognome() << endl;
    return stream;
}

std::ostream &operator<<(std::ostream &stream, dipendente c)
{
    stream << "Dati di\n" << c.nome() << " " << c.cognome() << endl 
           << "Data assunzione: " << c.assunzione() << endl     
           << "Stipendio: " << c.stipendio() << endl;
    return stream;
}

std::ostream &operator<<(std::ostream &stream, dirigente c)
{
    stream << "Dati di\n" << c.nome() << " " << c.cognome() << endl 
           << "Data assunzione: " << c.assunzione() << endl 
           << "Stipendio: " << c.stipendio() << endl 
           << "Livello: "<< c.livello() << endl 
           << "Campo: " << c.campo() << endl;
    return stream;
} 

This is main

#include <iostream>
#include "persone.h"
using namespace std;

int main()
{
    persona giacomo("giacomo","poretti"); //output persona class
    dipendente giovanni("giovanni","storti","12-10-2000", 23); //output dipendente class
    dirigente aldo("aldo","baglio","13-07-1998", 2345.43, "A1", "programmazione sistemi  di smaltimento"); //output dirigenti class

    cout << giacomo << endl;
    cout << giovanni << endl;
    cout << aldo << endl;

    return 0;
}

it compiles, but this is output

Dati di //persona class: right
giacomo poretti 

Dati di //dipendenti class: wrong
nome cognome //wrong
Data assunzione: 12-10-2000
Stipendio: 23

Dati di //dirigenti class: wrong
nome cognome //wrong
Data assunzione: assunzione //wrong
Stipendio: 0 //wrong
Livello: A1 
Campo: programmazione sistemi di smaltimento

I commented //wrong the output that wrong output

0 Answers
Related