Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a main class:

class Sportist{
    private:
        string ime;
        int godina_na_ragjanje;
        int godisna_zarabotuvacka_EUR;
    public:
        Sportist(string i, int g_n_r, int g_z_EUR){
            ime = i;
            godina_na_ragjanje = g_n_r;
            godisna_zarabotuvacka_EUR = g_z_EUR;
        }
};

And now I have a new class like this:

class Fudbaler:public Sportist{
    private:
        int broj_na_odigrani_natprevari;
        int danocna_stapka;
    public:
        Fudbaler(string ime, int godina, int zarabotuvacka, int b, int d){
            :Sportist(ime, godina, zarabotuvacka)
            broj_na_odigrani_natprevari = b;
            danocna_stapka = d;
        }
        float danok(){
            return godisna_zarabotuvacka_EUR * danocna_stapka;
        }
        friend ostream& operator<<(ostream &os, Fudbaler F){
            return os << "Ime: " << ime << endl
                      << "Godina na raganje: " << godina_na_ragjanje << endl
                      << "Godisna zarabotuvacka(EUR): " << godisna_zarabotuvacka_EUR << endl
                      << "Danok sto treba da plati: " << danok();
        }
};

I want to call the constructor from the first class in the second class, but I get errors that I haven't provided arguments which I do.. and also, I want to know how to access the private elements from the first class in the second, because it's taken as 'public', so how can I use them in my functions, like danok().

Errors while calling the constructor:

no matching function for call to 'Sportist::Sportist()'

candidates are:

Sportist::Sportist(std::string, int, int)

candidate expects 3 arguments, 0 provided

Error while calling variables using public method:

'int Sportist::godisna_zarabotuvacka_EUR' is private

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

You do not initialize Sportist before you enter your Fudbaler constructor function body. Therefore the Compiler tries to use a default constructur of Sportist which does not exists. You Need to initialize Sportist before entering the Fudbaler constructor body.

Initializers are appended after the closing parenthesis before the function body in curly brackets:

    Fudbaler(string ime, int godina, int zarabotuvacka, int b, int d)
       : Sportist(ime, godina, zarabotuvacka), 
        broj_na_odigrani_natprevari(b), 
        danocna_stapka(d)
    {
    }

Private variables are private and cannot be accessed in child classes. If you want to Access the Sportist members in Fudbaler member function you need to declare them protected (only accessible in this class and child classes) or public (generally accessible).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...