Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

How to call constructor and variables from another class?

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

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

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...