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.
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.
Comments
Leave a comment