I need each question fully answered to help me study for my midterm exam thank y
ID: 3568625 • Letter: I
Question
I need each question fully answered to help me study for my midterm exam thank you
1) If a data member is declared as private, protected or public in a base class, discuss how it
can or cannot be accessed in a public member function of a derived class.
2) Discuss the difference between overloading and overriding a member function in a derived
class.
3) Give an example of a situation where it is necessary to define a virtual destructor.
4) What is the feature that makes a base class an abstract class?
5) When designing a class hierarchy involving classes A and B, what are the two questions
that must be asked to determine whether class B should be derived from class A?
6) What does the following code do?
void drawSquares(Shape **p, int size)
{
Square *sp;
for ( int i = 0; i < size; i++ )
{
sp = dynamic_cast<Square*>(p[i]);
if ( sp )
sp->draw();
}
}
7) Explain why a factory member function should be a static member function.
8) Write a function template sort that takes two references to generic objects and switches
them if the first argument is larger than the second. It is only assumed that operator<() is
defined for the objects considered.
9) Assume that v is a vector<int>. Write a for loop that prints all the elements of v on
standard output. Use an iterator.
Explanation / Answer
1)
The access specifiers are shown below:
Access
Public
Private
Protected
Same class
Yes
Yes
Yes
Derived class
Yes
No
Yes
Outside class
Yes
No
No
2)
Overloading
Overriding
1)More than one function share a common name
1) A Function in base class is redefined in the derived class.
2)Function parameters are different for each function
2) The functions parameters are same.
3)
The virtual destructor is used whenever a base class pointer is pointing to its derived class. Any attempt made to delete the base class pointer results in de-allocating the memory occupied by the base class. Therefore instead the derived class getting destroyed the base class does. Now as the base class gets destroyed the base class pointer which was pointing to its derived class hold no meaning as it is already destroyed.
In such a case we should make the destructors of the base class virtual so that whenever a delete is called on the base class pointer then as the destructor is virtual the compiler will call the destructor of the respective derived class. Hence the scenario won
Access
Public
Private
Protected
Same class
Yes
Yes
Yes
Derived class
Yes
No
Yes
Outside class
Yes
No
No
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.