In: Computer Science
Give reasonably brief, but complete answers to the following.
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? --------------------------------------------------------------------
a) At first the constructor of class X should be called followed by String Constructor of Y.
Then at second position, destructor of class Y and X should be called.
Hence when object of class Y String is created, at first class X constructor is started then class Y’s String constructor is created.
Destructor :
At first, class Y’s destructor is destroyed. Then class X destructor is destroyed.
Example program :
#include <iostream>
using namespace std;
class X
{
public:
X()
{
cout<<"X construtor \n";
}
~X()
{
cout<<"Destructor of X";
cout<<"\n";
}
};
class y :public X
{char *s;
public:
y()
{
cout<<"Constructor of Y";
cout<<endl;
}
y(char *str)
{
cout<<"String constructor of Y";
cout<<*str;
cout<<endl;
}
~y()
{
cout<<"Y Destructor";
cout<<endl;
}
};
int main() {
y objY(" Trial Program ") ;
return 0;
}
b) Yes we should declare X’s destructor as virtual as if we don’t declare virtual destructor in base and derived class will give following output sequence.
Base Constructor
Derived Constructor
Base Destructor
By modifying to virtual destructor output would be in below format
~X(){printf("\nBase Destructor ");}
virtual ~X(){printf("\nDerived Destructor ");}
Output:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
11.
If we want that derived class should always override a function then the function is known as pure virtual function. If we have a class in which un-overridden pure virtual functions are present then we call that class as Abstract Class and its object cannot be created.
Example :
class ClassAbstract {
public:
// declaring a pure virtual function:
// currently it is abstract
virtual void pureVirtFunc(int) = 0;
};
class AbsractStill : public ClassAbstract {
// this is not overrinding pureVirtFunc(int),
// Hence the class is abstract.
};
class Solid : public AbstractStill {
public:
// Overriding pureVirtFunc(int),
// Hence this class is concrete class
void pureVirtFunc(int) { /*Code*/ }
};
ClassAbstract a; // error!, it isabstract class
AbsractStill b; // error!, it is still abstract class
Solid c; // ok!, it is concrete class
12. Abstract class is a class which has one or more than one abstract methods. An abstract method is one which is just declared without any implementation. We may not create an instance of Abstract class as it forces to provide implementation of abstract methods.
Hope this will help