Question

In: Computer Science

Give reasonably brief, but complete answers to the following. 10. a) Suppose class Y is derived...

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?

--------------------------------------------------------------------

Solutions

Expert Solution

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


Related Solutions

Give reasonably brief, but complete answers to the following. 10. a) Suppose class Y is derived...
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...
1. Give brief answers (a) Give an example of a process in which a system is...
1. Give brief answers (a) Give an example of a process in which a system is not heated, but its temperature increases. Explain this process on the basis of the 1st law of thermodynamics. (b) You brought a cup of hot coffee to a classroom. After a while, the coffee cooled down to room temperature. How did the entropy of the coffee change in this process? How did the entropy of the Universe vary in this process, where the Universe...
The following questions are simply applications of Bernoulli’s principle. (Give complete sentences as Answers.) Why is...
The following questions are simply applications of Bernoulli’s principle. (Give complete sentences as Answers.) Why is the draft in a fireplace better on a windy day? What keeps a Frisbee in flight? Why does your car lurch toward an oncoming truck as it passes by? Why does the wind increase the size of water waves? Why do waves form as water approaches the shore?
Provide brief, but complete, answers to the following questions.(i)Why would one want to model some phenomenon...
Provide brief, but complete, answers to the following questions.(i)Why would one want to model some phenomenon as random but not others? For instance, an outcome of a coin toss is modeled as random but not the path of a projectile tossed into the air.(ii)What information completely characterizes, probabilistically speaking, a random variable? That is, having this information, one knows all one can possibly know about the nature of randomness.(iii)What would one want the relationship between population and sample moments to...
Write brief but complete answers to the following questions a) Write Schrodinger's time-independent,1-D equation b) What...
Write brief but complete answers to the following questions a) Write Schrodinger's time-independent,1-D equation b) What does this equation represent? c) What requirements must the wave equation satisfy? d) What are the conditions for an acceptable solution to this equation?
Give a brief definition of the following terms (a
Give a brief definition of the following terms (a
Please give a brief explanation on why these are the correct answers. Question 5: You believe...
Please give a brief explanation on why these are the correct answers. Question 5: You believe that Imani Inc’s stock will have an expected return of 18.2%. The company is expected to pay a dividend of $1.60 per year. The current market risk premium is 7%, what should you be willing to pay for the stock? Correct Answer: $8.79 Question 6: Suppose you invest 50% of your portfolio in Dorsey Pharmaceuticals which has a beta of 0.63, 25% of your...
How can the following code be corrected? Give at least two good answers. 1 public class...
How can the following code be corrected? Give at least two good answers. 1 public class H2ClassC { 2   H2ClassC (int a) {} 3 } // end class H2ClassC 4 5 class H2ClassD extends H2ClassC{ 6 } // end class H2ClassD
Suppose x is 1 and y is 10. What is the output of the following code?...
Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) && ( y <11)                                     Display statement                         else                                     Display nothing 2. Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) || ( y <11)                                     Display statement                         Else                                     Display nothing 3. Write a java program that prints 1 to infinite numbers. 4. The...
10. (9 pt) What reagents and collisions are needed to complete the following reaction? Give a...
10. (9 pt) What reagents and collisions are needed to complete the following reaction? Give a complete mechanism, and use a free energy reaction coordinate diagram to show WHY the desired product is formed rather than other potential products. Are you using kinetic or thermodynamic control? 2,3-diethyl-1,3-butadiene -> racemic mixture of R and S versions of 3-bromo-2-ethyl-3-methyl-1-pentene
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT