+ 6

A wrong question in Sololearn game (C++)?

I got the following question in Sololearn game: A derived class doesn't inherit access to which of the followings of a base class? (+) Private data (+) Friend functions (-) Protected data (+) Overloaded operators Answer #1: I agree, a derived class can not access to private data of a base class. Answer #2: friend functions for classes is usual functions: if a function was defined before use it may be used. Vice versa, friend functions have access to all class data. So, an inheritance can't change an access to friend functions from a class. I think that the author of the question meant that base class friend functions don't have access to derived class data. If so, I agree, but think it is better to rewrite the answer#2 or the question. Answer #3: I agree, a derived class can access to protected data of a base class. Answer #4: I disagree. Here, I can't guess what the author meant. Overloaded operators that were declared as class methods are inherited according their access specifiers (only protected and public). In that case, the answer#4 is wrong. Overloaded operators that were declared as standalone functions don't belong to the class, and so, access to them is defined as to a usual function (if it was declared before use, it may be used). In that case, answer#4 is also wrong. What do you think? P.S. I marked it as wrong 2 or 3 weeks ago, but got it again. Appologize in advance to the author of the question, if I was wrong.

26th Feb 2020, 8:48 AM
andriy kan
andriy kan - avatar
4 Respuestas
+ 4
Screenshot of the question: https://drive.google.com/file/d/19A0qzrQNxCMZpHAhEQnxkpxDpE2h8kTw/view?usp=sharing It is the result. Sololearn showed which answers must be selected.
26th Feb 2020, 9:15 AM
andriy kan
andriy kan - avatar
+ 4
@Hatsy Rei I sent complaint 2 or 3 weeks ago (using send complaint button), but got that wrong question again. operator=() is also inherited by derived class (if it was decalred and has public or protected access specifiers). It is just hidden by default assignment operator of a derived class. But you can call it, for example, like this: class A{ int x = 0; public: A&operator=(int value){ x = value; return *this; }; int getX() const { return x; } }; class B: public A{}; int main() { A a; B b; a = 3; b.A::operator=(5); cout << a.getX() << endl; cout << b.getX() << endl; return 0; } the output will be 3 5. If operator=() were not inherited, there would be an error in this code. There is no way to call not inherited methods. https://code.sololearn.com/cfs8X0f0S6zg
26th Feb 2020, 11:58 AM
andriy kan
andriy kan - avatar
+ 3
According to https://docs.microsoft.com/en-us/cpp/cpp/general-rules-for-operator-overloading?view=vs-2019 "All overloaded operators except assignment (operator=) are inherited by derived classes." The quiz answer does appear to be wrong, at least for all other operators except =. I did a little test and sure enough, class A { public: int operator+(int y) { return y; } }; class B : public A {}; int main() { std::cout << B()+39; } Unless the quiz is asking about something else, perhaps a completely different question than what we perceive from its wording, a fix is likely needed. If you've flagged the question but it remains unrectified, perhaps consider notifying the administrators directly using the in-app feedback feature or through mail. Linking this thread should help. info@sololearn.com
26th Feb 2020, 11:27 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Can you post a screenshot?
26th Feb 2020, 9:02 AM
Aymane Boukrouh
Aymane Boukrouh - avatar