Answer to Question #213956 in C++ for ali

Question #213956

Answer the following short questions. Your answer should be precise.

a)     Can we declare base class constructor or destructor as virtual if yes then why we want to do?

b)    How we can declare a class as abstract and why we need abstract classes? 

Why we cannot able to create objects of abstract classes give reasonable justification of that.     


1
Expert's answer
2021-07-07T01:50:38-0400

a) Can we declare base class constructor or destructor as virtual if yes then why we want to do?

Answer:

A virtual constructor is not possible, but virtual destructor is possible.

Virtual destructor is useful if we need to delete an instance of a derived class through a pointer to base class.

b) How we can declare a class as abstract and why we need abstract classes? 

Answer:

The purpose of an abstract class is to define a common protocol for a set of concrete subclasses. This is useful when we define objects that describe abstract ideas.

class Shape {
protected:
      int width;
      int height;   
public:
      virtual int Area() = 0; 
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }
};

c) Why we cannot able to create objects of abstract classes give reasonable justification of that.

Answer:

We cannot able to create objects of abstract classes because they have an abstract methods without body. It's abstract and an object is concrete.

The compiler will display errors, if we try to run such program. 


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS