3. Templates, BST Questions & Answers
3. Templates, BST Questions & Answers What happens when declaring a variable or file more than once (i.e. redefining), and why - ANSCompiler error Including external classes in a file: syntax, where to include, and purpose - ANS-#include "<filename>.h" So the compiler is aware of the public interface for that class to be able to use it Include in <filename>.cpp, other classes that need its interface, and/or Preprocessor directives (in order) for defining a class: syntax, purpose, and issues if not included - ANS- #ifndef CLASSNAME_ - if not already defined (in compiler?), then... #define CLASSNAME_ - define this classname by starting to read the file and compiling it code... #endif - stop reading and compiling this file How to prefix method implementations in .cpp - ANS-<ClassName>::<methodName>() { } C++ allows one source file to contain implementations of methods from multiple files, thus must scoperesolve to the class it belongs Syntax to define a type for a class (the hard way), and disadvantages - ANS-typedef <TypeName> typeDefinitionName; class <ClassName> { <TypeName> item; } If a new type needs to be defined for this class, must re-write a new class with the new type, and then update it everywhere Syntax to define a type for a class (the best way), and benefits - ANS-template<class TypeName> class <ClassName { <TypeName> item; } Everytime the class is used, just pass in the type via the <> after template e.g. PlainBox<double> numberBox; e.g. PlainBox<MagicWand> wandBox; Describe all necessary ArrayList attributes - ANS-static const int DEFAULT_CAPACITY = 100; // default cap <DataType> items[DEFAULT_CAPACITY + 1]; // ignore items[0] int itemCount; // number of elements in the list int maxItems; // current max capacity of list CONTINUES...
Written for
- Institution
- 3. Templates, BST Question & Answer
- Course
- 3. Templates, BST Question & Answer
Document information
- Uploaded on
- January 7, 2024
- Number of pages
- 4
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
-
3 templates bst questions answers
-
what happens when declaring a variable or file mor