10. a) Suppose class Y is derived from class X. Each class declares a constructo
ID: 3760591 • Letter: 1
Question
10. a) Suppose class Y is derived from class X. Each class declares
a constructor and a destructor. Class Y has a string member
object that has a constructor and destructor. In what order do
the three constructors and destructors run when instances of
class Y are created and destroyed?
b) Should X's destructor be declared virtual? Why or why not,
and what difference would it make?
--------------------------------------------------------------------
11. What is a pure virtual function? Why would you define a pure
virtual function? Give an example of a pure virtual function.
--------------------------------------------------------------------
12. What is an abstract base class? What does it do? Why would you
make a class abstract?
--------------------------------------------------------------------
Explanation / Answer
11)
>A pure virtual function is a virtual function that you want to force derived classes to override. If a class has any unoverridden pure virtuals, it is an "abstract class" and you can't create objects of that type.
Example;
class AbstractClass
{
public:
virtual void f(int) = 0;
};
class StillAbstract : public AbstractClass
{
};
class Concrete : public StillAbstract
{
public:
void f(int) { /*..........*/ }
};
AbstractClass a;
StillAbstract b;
Concrete c;
>It is actually pretty rare to see a pure virtual function with an implementation in real-world code, but having that implementation may be desirable when you think that classes which derive from the base class may need some sort of default behavior for the pure virtual function
12)
>An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
>The purpose of abstract classes is to function as base classes which can be extended by subclasses to create a full implementation
>Abstract classes improve the situation by preventing a developer from instantiating the base class, because a developer has marked it as having missing functionality. It also provides compile-time safety so that you can ensure that any classes that extend your abstract class provide the bare minimum functionality to work.
>Abstract classes are for "is a" relationships and interfaces are for "can do".
>Abstract classes let you add base behavior so programmers don't have to code everything, while still forcing them to follow your design
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.