My book covered superficially many test topics from the Object-Oriented domain and I need some explanation and a hint which book does cover such topics.
There's a test question:
#include <iostream>
class A {
public:
virtual void f(int n=2) {// takes the argument value from here
std::cout << n+1 << " in A";
}
};
class B : public A {
private:
virtual void f(int n=3) {
std::cout << n - 1 << " in B";// this class is used
}
};
int main() {
A* p = new B;
p->f();
}
Output:
1 in B
, that is the argument from A
fed into B
.
I understand why the object of the class B
is created. I don't understand why is the value of 2 taken as the argument. I also checked if the class A's value would be taken if the variable's name were changed -- it is still used.
Any literature to read about this or educational videos on such OOP topics is welcome.