+ 3
C++ challenge from quizes
What is the output and how did you get it ? class A{ public: A(){ foo();} ~A(){foo();} void foo(){ cout<<3; } void bar(){ foo();} }; class B:public A{ void foo(){cout<<2;} }; int main(){ B b; b.bar(); }
2 Respuestas
+ 13
We have class A, and class B which is derived from class A.
B b;
Object b of class B is created and the constructors are called. Since class B has no explicitly defined constructor, the program calls class A constructor, which calls foo() from class A. Prints 3.
b.bar();
Method bar() called using object b. No method in class B goes by the name bar, so class A bar() method is called. bar() method calls foo(), and this foo() is from class A ( because bar() is from class A, and cannot access class B foo() ). Prints 3.
Program ends, destructors are called. Class A destructor calls foo() from class A again. Prints 3.
Output: 333
+ 2
Those challenges are getting clearer for me thank you again <3