Take the declarations
class I
{ public: virtual int key() = 0; };
class C: public I
{ public: virtual int key() { ... } int value() { ... } };
class D: public C
{ public: string name() { ... } };
class E:
{ public: virtual int key() { ... } };
void print(I* x) { ... }
Which of the following declarations/commands yield compilation errors or runtime errors
and what is the exact reason?
a) I* i = new I();
b) C* c = new C();
c) D* d = new D();
d) E* e = new E();
e) I* j = c;
f) I* k = d;
g) I* l = e;
h) C* f = d;
i) D* g = c;
j) D* h = dynamic_cast<D*>(c);
k) D* m = dynamic_cast<D*>(f);
l) print(j);
m) print(c);
n) print(d);
o) print(e);
1
Expert's answer
2016-03-09T09:04:05-0500
a) I* i = new I(); It will yield compilation error, because the abstract classes can't have objects!
g) I* l = e; It will yield compilation error (converting from E* to I*), because class E and I are different and not connected
i) D* g = c; It will yield compilation error (downcasting from child to parent)
j) D* h = dynamic_cast<D*>(c); It will not yield the compilation error, but since it is downcasting, if we call the function, which is the only child class ( for example: value() ) this will cause an error
o) print(e); It will yield compilation error (converting from E* to I*), because class E and I are different and not connected
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments
Leave a comment