Snowman
Snowman

Reputation: 32081

Question on abstract base classes in c++

Suppose class stringGetter contains exactly one pure virtual function: the overloaded paren- theses operator, string operator()(int x). Also suppose that class getPageString is a public stringGetter that implements operator().

Which of the following C++ statements will certainly result in a compiler error?

(a) stringGetter * a = new stringGetter;
(b) stringGetter * a = new getPageString;
(c) stringGetter * a;
getPageString * b = new getPageString;
a=b
(d) Exactly two of these will result in a compiler error.
(e) It is possible that none of these will result in a compiler error.

I'm a little fuzzy on abstract base classes, and I cant find good example cases online that do assignments like the ones below. I like asking questions on here about this kind of stuff, as I often learn more about things I wasnt even intending on learning. I cant even begin to make a guess on which of these would cause a compiler error. Can anyone go through a-c and tell me why or why not it would cause a compiler error?

Upvotes: 0

Views: 197

Answers (4)

Todd Allen
Todd Allen

Reputation: 648

Looks like a bit of a trick question. (a) definitely results in a compiler error, as already stated, and you've already gotten an excellent answer as to why. However, in option (c), there is no semicolon after the "a=b" statement. That'll result in a compiler error, too, as it's a syntax error. Note the question didn't say "Which of these will cause a compiler error due to the class instantiation?"

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92391

You cannot have instances of an abstract class, which rules out (a). Option (c) is just a more difficult way of doing (b).

Upvotes: 1

usamytch
usamytch

Reputation: 413

(a) - actual instantiation of abstract class (new stringGetter) takes place only there.

Upvotes: 0

Mahesh
Mahesh

Reputation: 34665

(a) results compiler error because instances cannot be created for abstract classes.

Upvotes: 8

Related Questions