2

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

Biffen
  • 6,249
  • 6
  • 28
  • 36
avanzo
  • 21
  • 3
  • 4
    You miss to call base class constructor -> `dipendente(string nome, string cognome, string assunzione, double stipendio) : persona(nome, cognome), m_assunzione(assunzione), m_stipendio(stipendio) {}` (the part with `persona(nome, cognome)`). – Jarod42 Jun 28 '22 at 09:02
  • In class `dipendente`, the constructor you have written of the form `dipendente() : m_assunzione("assunzione"), m_stipendio(0) {;}` doesn't specify anything that would affect the base class (`persona`) or any of its members at all. Why do you expect the arguments passed to the derived class constructor would affect members of the base class, when you haven't specified any connections between them? – Peter Jun 28 '22 at 09:04
  • Unrelated: the `operator<<` should take its second argument by const reference, not as a copy. – Botje Jun 28 '22 at 09:11
  • thank you so much, it works. It wasn't specified in the slides and i could.t find other posts that could explain it to me. – avanzo Jun 28 '22 at 09:30
  • FYI: [Constructors and member initializer lists](https://en.cppreference.com/w/cpp/language/constructor): _In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual bases and non-static data members._ There is an [example](https://en.cppreference.com/w/cpp/language/constructor#Example) to illustrate this. – Scheff's Cat Jun 28 '22 at 09:56

0 Answers0