64 696
Assignments Done
99,4%
Successfully Done
In September 2018

Answer to Question #58338 in C++ for jonney

Question #58338
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);
Expert's answer
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

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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions