In which type of inheritance do all derived classes share a common base class?
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Hierarchical Inheritance is when multiple derived classes inherit from a single base class. All the derived classes share the features of that one common base class. This kind of inheritance structure resembles a hierarchy or tree structure.
To define an abstract class in C++, at least one member function must be declared as _______.
- inline
- pure virtual
- private
- constant
In C++, an abstract class is defined by having at least one pure virtual function. This means the function is declared but not defined within the class, ensuring that derived classes must implement it.
Which of the following scenarios is most suitable for using a struct instead of a class?
- When needing multiple inheritance.
- When encapsulation and private members are vital.
- When only simple data storage without methods is required.
- When polymorphism is a primary concern.
In C++, both structs and classes can have member functions, inherit from others, and serve many of the same roles. The primary difference is the default access level (structs default to public; classes default to private). However, structs are conventionally used for simple data storage without associated behavior or methods.
You are tasked with designing a lightweight 3D point class to be used in a performance-critical graphics application. The class should store three float values and should allow public access to these without using getters and setters. Which user-defined data type would be best suited for this purpose?
- Class
- Struct
- Union
- Array
In C++, structs are typically used for lightweight user-defined data types. They default to public member access, making it easier to access the members directly without the need for getters and setters. Classes default to private access and are generally used when encapsulation is a primary concern.
What implications does the "Rule of Three" have in C++ class design?
- Always implement three constructors
- Always use three public members in a class
- Always create three instances of a class
- Manage the copy constructor, assignment operator, and destructor
The "Rule of Three" in C++ suggests that if a class requires a user-defined destructor, copy constructor, or assignment operator, it likely requires all three. This rule arises due to the intricacies of dynamic resource management (like memory allocation) in C++. Implementing or omitting one without the others can lead to unexpected behavior.
How does the virtual base class resolve the diamond problem in C++?
- By preventing multiple copies
- By duplicating base class
- By altering access specifiers
- By overloading functions
The diamond problem arises in C++ multiple inheritance when one class inherits from two classes that have a common base class. This can lead to ambiguity regarding which base class member should be accessed. The "virtual" keyword before the base class prevents the compiler from creating multiple instances of the base class, thus resolving the diamond problem.
Which operator is used to access the memory address of a variable in C++?
- * (Asterisk)
- & (Ampersand)
- % (Percent)
- + (Plus)
The "&" (ampersand) operator is used to get the memory address of a variable in C++. For instance, if int x = 10;, then &x will give the memory address where the integer x is stored. The asterisk (*), on the other hand, is used for dereferencing.
Which of the following statements about the struct and class keywords is true?
- Only structs can have member functions
- Structs are always public, and classes are always private
- Both can have member functions, but default access is different
- Classes support inheritance, structs don't
Both struct and class in C++ can have member functions, data members, and can support inheritance. The primary difference is the default access level: members of a struct are public by default, whereas members of a class are private. Other functionalities between them are essentially identical.
To remove all instances of a particular value from a C++ STL vector, you should use the _______ algorithm.
- find
- sort
- erase
- remove
While erase can remove elements from a vector, to efficiently remove all instances of a value, you'd first use remove to move those elements to the end and then use erase. This is known as the "erase-remove" idiom in C++.
The keyword _______ is used to access the base class’s members in the derived class.
- super
- this
- parent
- base
In C++, the "base" keyword is not actually used. The correct way to access base class members is through the use of the base class name or the scope resolution operator. However, "base" is a term from C#.