Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone help me? The following is a take-home quiz. Please turn it in next c

ID: 3700220 • Letter: C

Question


can someone help me?

The following is a take-home quiz. Please turn it in next class. 1. (10 points) Suppose class D is derived from class B, and class B has a public member function whose declaration is virtual void fO; class D has its version of the function, void fO. void g(B& b) b.f); 1: D dobject; g(dObject); Suppose this is embedded in an otherwise correct and complete program. Which version of f0 will be called? a) D:f0 b) B:f0 c) This is illegal. You can't pass a D object argument for a B reference parameter. Answer Explanation:

Explanation / Answer

1. Correct Answer is: (a)

Explanation: As we know that a single pointer variable can be used to refer to the objects of base and derived classes. But it has been observed that a base pointer always executes the functions of the base class, even when it contains the address of derived class. To deal with this issue the concept of virtual functions is used. Where we make a function with same name in both the classes and the function of base class is declared as virtual function using virtual keyword.

When a function is made virtual then C++determines which function to use at run time based on the type of object pointed by the base pointer rather than the type of pointer. As the object of derived class D is used here to call the function hence the version of class D will be executed.

2. Correct Answer is: (b)

Explanation: As described in the above answer here again the object of D is used to call the function but now the function declared in base class is not a virtual function, hence the priority will always be given to the version of base class. If the function declared in base class is normal function then priority will always be given to base class. The complier ignores the content of the pointer and chooses the member function that matches the type of the pointer which is base class in this case.

3. Yes there is error in code. As per rule if there is a constructor in base class then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor. Here also we have a constructor in base class and a constructor in derived class as well but the constructor of derived class is not passing the argument for the base class constructor hence there is the error in the definition of derived class D and in the statement where the object declared of class D. The constructor of D should pass parameter for base class. The errors are mentioned below:

class D: public B

{

public:

D (int a): B(a)        // passing arguments to the base class .

{}

};

int main()

{

D y(5);                 // value Need to be passed here for base class constructor.

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote