Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


How can a class ensure it only has one instance?

  1. By making its constructor private

  2. Using the 'singleton' pattern with a static member of the same class type

  3. Declaring all its methods as static

  4. By making the class abstract

The correct answer is: Using the 'singleton' pattern with a static member of the same class type

The 'singleton' pattern is a design pattern that ensures a class only has one instance while also providing a global point of access to that instance. Option A is incorrect because making the constructor private doesn't prevent multiple instances from being created, it just prevents outside classes from creating instances. Option C is incorrect because declaring all methods as static doesn't prevent multiple instances from being created without the use of the 'singleton' pattern. Option D is incorrect because making the class abstract doesn't ensure only one instance can be created.