0

Actually, it's my final term exam question. Which of the following will create a compile time error?

  1. Declaring object of a concrete class in the definition of main function.
  2. Writing output statement, in constructor statement.
  3. Declaring object of a class having at least one virtual function.
  4. Declaring objects of a class having all virtual functions.

I've chosen 4th one. But still confused. Need help!

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
arximughal
  • 270
  • 3
  • 16
  • 2
    Umm...none of those cause a compiler error. – David G Aug 22 '14 at 22:39
  • 1
    Final exam question .. the answer should represent your knowledge of the subject ... not your ability to get someone else to provide the answer. – ErstwhileIII Aug 22 '14 at 22:40
  • 2
    What's a constructor statement? – chris Aug 22 '14 at 22:42
  • Are you sure they wrote "concrete" instead of "abstract"? – David G Aug 22 '14 at 22:43
  • 4 is correct if you meant "pure virtual functions" rather than "all virtual functions". – molbdnilo Aug 22 '14 at 22:45
  • You can declare classes in a function? Haha, I actually learned something! Really though, that was the only one of which I thought it would cause a compiler error, and then I figured out it didn't. I think you copied the question wrongly. – Aart Stuurman Aug 22 '14 at 22:46
  • @AartStuurman, While you can, the statement was the declaration of an instance of a class, not the class itself. – chris Aug 22 '14 at 22:47
  • I didn't copied wrong... It is the as same as it was asked. I've the snapshot too. – arximughal Aug 22 '14 at 22:52
  • @ErstwhileIII I just asked the answer here for my own sake... this question has already been asked and they are not gonna ask the same question twice a day – arximughal Aug 22 '14 at 22:54
  • @Jack molbdnilo might be right. I have never seen someone call 'pure virtual' functions an 'all virtual' function though. – Aart Stuurman Aug 22 '14 at 22:55
  • hmm... alright, then may be they spelled it wrong. I'm still checking it again and again, it is the same – arximughal Aug 22 '14 at 22:56
  • "All virtual functions" being used to mean "one or more pure virtual functions" is not worthy of an exam question and should not be counted. – chris Aug 22 '14 at 22:57

2 Answers2

1

None of the four options

  1. Declaring object of a concrete class in the definition of main function.
  2. Writing output statement, in constructor statement.
  3. Declaring object of a class having at least one virtual function.
  4. Declaring objects of a class having all virtual functions

would necessarily give a compilation error.

Any of them would give a compilation error if you include a syntax error, say.

However, it's unclear what the author means by "constructor statement", that's not a standard term.


It's possible that by "all virtual functions" the author means "one or more pure virtual functions". If so then then that's probably the intended answer.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

Some questions might be interpretable differently, so I will sum those up here. Note however, that some of these interpretations are not actually synonymes, but just my thoughts on what your teacher could have meant to say.

1. Declaring object of a concrete class in the definition of main function.

See this question: What is the difference between a concrete class and an abstract class?. This shows it is valid to instantiate or declare a concrete class. The main function is no special exception to this.

Conclusion: this is valid.

2. Writing output statement, in constructor statement.

By constructor statement, I assume the constructor function of a class or struct, or something alike, is meant. Usually, I would say that an output statement is something like writing to the console. However, as the questions use kind of weird language anyway, one could say that by output statement the return statement is meant.

Output, as in writing to the console for example, is perfectly legal in a constructor function.

class Foo
{
public:
    Foo()
    {
        std::cout << "I made a Foo\n";
    }
}

Output, as in returning something from a function, is not legal in a constructor.

class Foo
{
public:
    Foo()
    {
        return 3; // not legal
    }
}

Conclusion: If this is valid or not depends on how you interpret the question.

3. Declaring object of a class having at least one virtual function.

Virtual functions in classes are explained here: C++ Virtual/Pure Virtual Explained. The question is clear in that the one or more virtual function are not pure virtual, so:

Conclusion: this is valid.

4. Declaring objects of a class having all virtual functions.

See my explanation of answer 3 for an explanation of virtual and pure virtual functions. The straightforward interpretation of this answer is: Is it legal to declare an object of a class of which all functions are virtual? In this case the statement is valid, see answer 3.

However, you could interpret all virtual functions as pure virtual functions, in which case instantiating the class is not valid.

Conclusion: If this is valid or not depends on how you interpret the question.

Final conclusion

Answer 1 and 3 both describe valid C++. However, answer 2 and 4 can be interpreted in multiple ways, so the answer is not clear.

Personal thought

I have never ever heard of someone calling a return statement an output statement. Neither have I heard of someone calling a pure virtual function an all virtual function. I think your teacher is weird.

Community
  • 1
  • 1
Aart Stuurman
  • 3,188
  • 4
  • 26
  • 44