如何从另一个类调用构造函数和变量? [英] How to call constructor and variables from another class?

查看:91
本文介绍了如何从另一个类调用构造函数和变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要班级:

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();
        }
};

我想从第二个类的第一个类中调用构造函数,但是却收到错误消息我还没有提供参数,我也想知道如何从第二个类的第一个类访问私有元素,因为它被视为公共,所以我如何在函数中使用它们,例如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().

调用构造函数时出错:

没有匹配的函数来调用'Sportist :: Sportist()'

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

候选者是:

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

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

候选人需要3个参数,提供了0个参数

candidate expects 3 arguments, 0 provided

使用公共方法调用变量时出错:

Error while calling variables using public method:

'int Sportist :: godisna_zarabotuvacka_EUR'是私有的

'int Sportist::godisna_zarabotuvacka_EUR' is private

推荐答案

您不初始化 Sportist ,然后输入 Fudbaler 构造函数主体。因此,编译器尝试使用 Sportist 的默认构造函数,该构造函数不存在。
您需要先初始化 Sportist ,然后输入 Fudbaler 构造函数主体。

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)
    {
    }

私有变量是私有变量,不能在子类中访问。
如果要访问 Fudbaler 成员函数中的 Sportist 成员,则需要声明它们受保护的(仅在此类和子类别中可访问)或公共(通常可访问)。

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

这篇关于如何从另一个类调用构造函数和变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆