0

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?

Snowman
  • 31,411
  • 46
  • 180
  • 303
  • 4
    Could you make this sound a little less like you copied it straight from your homework / interview? Just sayin'. – cHao Feb 21 '11 at 21:34
  • 1
    Probably is homework, considering this other question asked a few minutes ago... http://stackoverflow.com/questions/5071069/behavior-of-simple-code-with-pointers-in-c – James Feb 21 '11 at 21:35
  • I notice this is the second question tonight that sounds like a homework question. Both would be quickly solved by just trying it – thecoshman Feb 21 '11 at 21:36
  • @cHao it's not homework. Its from an old exam and I'm reviewing for my own exam I got coming up tomorrow. – Snowman Feb 21 '11 at 21:36
  • 2
    You should probably put that at the top of the question then. We work on the honor system here; if you say it's not homework, it's not. :) – James Feb 21 '11 at 21:38

4 Answers4

8

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

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • You know its abstract from the pure virtual but what is throwing me off is that its overloading the () operator which is already a valid operator. So even though it virtual isn't it already implement? I don't know if this question makes any sense. – Grammin Feb 21 '11 at 21:37
  • I understand, he is overloading the operator `()`. Which means, OP is providing his implementation. – Mahesh Feb 21 '11 at 21:40
  • "You know its abstract from the pure virtual but what is throwing me off is that its overloading the () operator which is already a valid operator." <- It is? – Edward Strange Feb 21 '11 at 21:40
  • @Crazy Eddie - If I am overloading an operator, doesn't it mean that I am providing my implementation overriding the default for the user defined type ? I understand that, we cannot change the default method signature of the operator. – Mahesh Feb 21 '11 at 21:44
  • @Mahesh - no, it doesn't mean that. There's not a default for most operators wrt user-defined types. Only `operator=` has a default. Proof: `struct X {}; int main() { X x; x(); }` <- try to compile that. – Edward Strange Feb 21 '11 at 22:11
1

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

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

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

usamytch
  • 373
  • 1
  • 5
  • 13
0

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?"

Todd Allen
  • 648
  • 1
  • 6
  • 18