C++ 多重继承需要访问私有变量

C++ 多重继承需要访问私有变量,c++,inheritance,virtual,C++,Inheritance,Virtual,我不知道如何访问私有变量多重继承 在这段代码中,我创建了person类 在上一节课中,我使用“从多重继承”创建“教师助理”类,但我不知道如何打印str和工资 我不知道怎么做。我可以在stteacher类中创建两个变量,或者将str和salary从private变量更改为public变量,但我认为我应该使用多重继承来实现这一点。 请帮帮我。访问私有变量的一种模式是创建一个返回该变量的公共函数: class teacher : public virtual person { long sal

我不知道如何访问私有变量多重继承

在这段代码中,我创建了person类

在上一节课中,我使用“从多重继承”创建“教师助理”类,但我不知道如何打印str和工资

我不知道怎么做。我可以在stteacher类中创建两个变量,或者将str和salary从private变量更改为public变量,但我认为我应该使用多重继承来实现这一点。
请帮帮我。

访问私有变量的一种模式是创建一个返回该变量的公共函数:

class teacher : public virtual person
{
    long salary;

public:

    teacher(char*n, char*ln, long s);
    virtual void print(ostream&o);
    long get_salary(void){return salary;}
};
那么stteacher::print的实现将是:

void stteacher::print(ostream& o)
{
    person::print(o);
    o << get_salary();

}

类中的私有数据不能被任何非成员、非友元代码访问。时期是否使用继承是不相关的

因此,不同类访问该数据的唯一方法是:

提供访问器函数,以便调用者可以获取数据。如果是public,任何人都可以调用它,但如果它是受保护的函数,则只有派生类才能访问它

或者,让需要访问的类成为该类的朋友。友谊是C++程序员通常不鼓励的,但是,只有这样才是真正的最后手段。< /P> 将对数据的访问更改为公共。非常气馁,因为这完全破坏了封装


默认情况下,类是私有的,这意味着在指定访问修饰符之前的任何内容都是私有的

私有方法/局部变量只能由定义它们的类及其朋友类访问。在你的例子中,要定义一个朋友类,你应该首先告诉学生和老师什么是stteacher

C++自上而下地读取代码,所以如果您想使用变量/类/宏/任何东西,您应该在其用法之上声明它

在代码中,这看起来像:

extern class stteacher; //You tell C++ that 'stteacher' is a class

class person {};

class student : public virtual person {
    long str;
    friend class stteacher; //You make stteacher a friend of student
public:
    student(long str) : str(str) {}
};

class teacher : public virtual person {
    long salary;
    friend class stteacher; //You make stteacher a friend of teacher
public:
    teacher(long salary) : salary(salary) {}
};

class stteacher : public student, public teacher {
public:
    stteacher(long str, long salary) : student(str), teacher(salary) {}
    void print() {
        std::cout << "Salary: " << salary << "\nStr: " << str << std::endl;
    }
}

int main() {
    long str = 10, salary = 100;
    stteacher(str, salary).print();
    return 0;
}
工资:100

Str:10

即使这样做有效,我建议最好使用一个更具阿片剂的访问修饰符,比如protected

受保护的内容可以被定义它的类、它的朋友类和继承它的类访问

使用受保护的访问修饰符,上述代码如下所示: 外部班主任//你告诉C++,“STAGER”是一个类< /P>
class person {};

class student : public virtual person {
protected: //Can be accessed by it's childs
    long str;
public:
    student(long str) : str(str) {}
};

class teacher : public virtual person {
protected: //Can be accessed by it's childs
    long salary;
public:
    teacher(long salary) : salary(salary) {}
};

class stteacher : public student, public teacher {
public:
    stteacher(long str, long salary) : student(str), teacher(salary) {}
    void print() {
        std::cout << "Salary: " << salary << "\nStr: " << str << std::endl;
    }
}

int main() {
    long str = 10, salary = 100;
    stteacher(str, salary).print();
    return 0;
}
工资:100

Str:10


您可以将这些变量从private更改为protected。这和多重继承有什么关系?避免多重继承。@Ron:为什么?它会带来一些问题,但是如果你有一个适当的问题,它可以极大地简化事情。虽然定义成员函数通常是一个好主意,但是作为一个SO示例,通常值得内联定义它们-它有效地减少了代码量。OT:使用std:string,而不是char数组。。。。或者,如果您只希望该函数对派生类可用,而不是对所有人可用,则可以使该函数受保护。
void stteacher::print(ostream& o)
{
    person::print(o);
    o << get_salary();

}
extern class stteacher; //You tell C++ that 'stteacher' is a class

class person {};

class student : public virtual person {
    long str;
    friend class stteacher; //You make stteacher a friend of student
public:
    student(long str) : str(str) {}
};

class teacher : public virtual person {
    long salary;
    friend class stteacher; //You make stteacher a friend of teacher
public:
    teacher(long salary) : salary(salary) {}
};

class stteacher : public student, public teacher {
public:
    stteacher(long str, long salary) : student(str), teacher(salary) {}
    void print() {
        std::cout << "Salary: " << salary << "\nStr: " << str << std::endl;
    }
}

int main() {
    long str = 10, salary = 100;
    stteacher(str, salary).print();
    return 0;
}
class person {};

class student : public virtual person {
protected: //Can be accessed by it's childs
    long str;
public:
    student(long str) : str(str) {}
};

class teacher : public virtual person {
protected: //Can be accessed by it's childs
    long salary;
public:
    teacher(long salary) : salary(salary) {}
};

class stteacher : public student, public teacher {
public:
    stteacher(long str, long salary) : student(str), teacher(salary) {}
    void print() {
        std::cout << "Salary: " << salary << "\nStr: " << str << std::endl;
    }
}

int main() {
    long str = 10, salary = 100;
    stteacher(str, salary).print();
    return 0;
}