3358 Data Structures and Algorithms Exam I
Three ways in which a majority of, but not all, operators can be overloaded:
(1) As member functions (2) As non-member, non-friend functions (3) As non-member, friend functions
const char * const GetName() const; Explain the significance of each use of the const keyword in the statement.
---1st const pointer returned cannot be used to change the character it is pointing at. ---2nd const pointer returned cannot have its value changed (be made to hold a different address). ---3rd const GetName is an accessor (not a mutator).
What are typically included in the implementation file:
---Documentation comments that are for consumption by implementers Including preconditions and postconditions (for functions meant only for implementers), and invariant ---Relevant header file(s) ---Definitions of functions specified in the matching interface (header) file
What are typically included in the interface (header) file:
---Documentation comments that are for consumption by users Including preconditions, postconditions, whether safe to use value semantics, and additional usage notes ---Macro (or inclusion) guard ---Relevant header file(s) Such as header file(s) of other component(s) referenced in this header file ---Relevant globals ---class definitions, including function prototypes
What typically are included in the application file:
---Relevant header file(s) ---Prototypes of functions that logically belong to the application. ---Relevant globals. ---The main function ---Definitions of functions that logically belong to the application
Advantages of information hiding and modular programming (-ilities)
---Reusability: A library of components (modules) can be built; many programs can use the same components. ---Maintainability: A major part of maintenance arises from the need for change due to bugs, requirements, etc. Mitigating the impact of change is thus key to tackling maintenance. Information hiding and modular programming both aid in reducing such impact through isolation/localization: when a module need change, the impact should ideally be localized to that module; interface tends to change less often than implementation so it makes sense to isolate the implementation (and hide it from whoever need not to know). In rather oversimplified terms, with separate files, if certain module needs fixing, we only need to change and recompile one implementation file; the other files, including the application file, need not be changed or even recompiled. ---Manageability: Modularly structuring a program facilitates division, testing, etc. of work/responsibilities.
we can effect function overloading by taking advantage of C++'s support for function templates when:
---all the overloaded functions have signatures that differ only in the types of parameters ---all the overloaded functions have the same operations and program logic
>> and << can only be overloaded using:
---either non-member, non-friend functions, or ---non-member, friend functions
3 main components of STL:
---iterators ---containers ---algorithms
What are the 3 key ingredients and 3 key action verbs of contract-based design technique?
->preconditions, postconditions, class invariant ->expect, guarantee, maintain
size_t
-Alias of one of the fundamental unsigned integer types -able to represent the size of any object in bytes - is the type returned by the sizeof operator and is widely used in the standard library to represent sizes and counts
Why are function signatures important?
-Compiler essentially considers a function's signature to be part of its name -Allow compiler to distinguish ("disambiguate") calls to different functions with the same name - make function overloading possible -Caveat: compiler may not be able to "disambiguate" certain signature difference - e.g.: Calc(int) vs Calc(int&) in call Calc(intVal);
3 contexts for using const keyword
-Declaring symbolic constant. -Specifying pass-by-const-reference. -Specifying accessor (protecting invoking object)
Under which 3 contexts (of interest to a programmer) will C++ call the copy constructor?
-Defining a new object of a class, initializing it to an existing object (of the same class). -Passing an object of a class by value to a function. -Returning an object of a class by value from a function.
Operators that can be used without overloading when creating own data type using class:
-The address-of (&) operator -The sizeof operator -The dot (.) operator -The arrow (->) operator -The assignment (=) operator ---automatic assignment operator that comes for free has the same limitation as the automatic copy constructor ---memberwise (shallow) copying ---may or may not be sufficient - usually insufficient for classes that contain data members that are pointers to dynamically allocated memory (just like automatic copy constructor)
Each component is separated into two files:
-The file for the specification part (which describes the component's interface) is called the interface file. -----The "what" -The file for the implementation part is called, well, the implementation file. -----The "how"
friend function
-a "friend" of a given class -a function that is given the same access as class methods to private and protected data -is declared by the class that is granting access, so they are part of the class interface, like methods
namespace
-a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it ---used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries
ADT (Abstract Data Type)
-a type (or class) for objects whose behavior is defined by a set of value and a set of operations ---It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations
How does one determine the address of a variable?
-attach address of operator (&) immediately in front of the variable name --e.g.: &years (NOTE: no space between & and the variable name)
int *countPtr = 0, count; ... count = 7; countPtr = &count; ... How are count and countPtr related?
-count directly references a variable whose value is 7 -countPtr indirectly references (or points to) a variable whose value is 7
Describe this: int *countPtr = 0, count;
-declares countPtr to be of type int * (i.e., pointer to int) and initializes it to 0 (more on the "zero" address to come) -is read "countPtr is a pointer to int" or "countPtr points to an object of type int" -also declares count to be an int (not a pointer to int!)
A pointer may be initialized to....(2 answers)
-either 0 (the only integer value that can be assigned directly to a pointer and it can be assigned to a pointer to any type)... -or the address of a variable of the same type pointed to by the pointer
4 important attributes associated with every variable
-name -value stored -address -data type
What's happening here: typedef std::size_t size_type;
An alias called size_type is being created for std::size_t
What will happen if a function is executed and the precondition for the function is not met? A)An error message will be printed. B)The program will loop indefinitely. C)The system will crash. D)Any of the above results could happen.
D
When developing a class that contains a data member that is a pointer pointing to dynamically allocated memory, what member functions should be provided for the class? A) Overloaded assignment operator. B) Copy constructor. C) Destructor. D) All of the above.
D
When should you use a const reference parameter? A)Whenever the data type might be many bytes. B)Whenever the data type might be many bytes, the function changes the parameter within its body, and you do not want these changes to alter the actual argument. C)Whenever the data type might be many bytes, the function changes the parameter within its body, and you do want these changes to alter the actual argument. D)Whenever the data type might be many bytes and the function does not change the parameter within its body.
D
Which kind of functions can access private data members of a class? A)friend functions of the class B)private member functions of the class C) public member functions of the class D) All of the above E) None of the above
D
What is this? const int a = 100;
Declaration of a symbolic constant
When overloading a binary operator using member function, the invoking object is the ______ operand.
LHS
When overloading a binary operator using non-member function, the first parameter is the ______ operand.
LHS
A more inclusive term that can refer to either an implementation file or an application file.
Source file
#include directive
Tells the preprocessor to treat the contents of a specified file as if they appear in the source program at the point where the directive appears
What is the left-inclusive metaphor?
The range of your iterator must have the following range: [left, right). Left is the iterator pointing to the first relevant element in the container, while right is an iterator pointing just beyond the last element to be used. The container is empty when left == right
class-level variables
These are also known as static member variables and there's only one copy of that variable that is shared with all instances of that class. If changes are made to that variable, all other instances will see the effect of the changes.
instance-level variables
These variables belong to the instance of a class, thus an object. And every instance of that class (object) has it's own copy of that variable. Changes made to the variable don't reflect in other instances of that class.
How constructors of a class are bound ("contractually") by invariants
They assume "not valid" on entry, but guarantee "valid" on exit
How other mutators ("the rest") of a class are bound ("contractually") by invariants
They expect "valid" on entry, and maintain state of "valid" upon exit
How destructors of a class are bound ("contractually") by invariants
They expect "valid" on entry, but don't care if "valid" on exit
Describe: typedef int value_type;
Using typedef, an alias "value_type" is created as a container for int objects
By convention, should the name of a template parameter be capitalized?
Yes!
Is this legal? int Sum(int a, int b, int c = 0, int d = 0); //prototype
Yes, so long as the function's header does not contain default arguments
helper function
a private member function that is useful when a class requires an operation that should not be part of the public interface
postcondition
a statement describing what will be true (guaranteed) when a function call is completed. If the function is correct and the precondition was true when the function was called, then the function will complete, and the ______ will be true when the function call is completed
precondition
a statement giving the condition that is required to be true (expected) when a function is called. The function is not guaranteed to perform as it should unless the _______ is true.
address
address of first memory location allocated for the variable
Iterator
an object (like a pointer) that points to an element inside the container ---used to move through the contents of the container
The file for the entire program is called the ___________
application file (or client file or driver file)
Modular programming
building application with components that are organized as modules (interface-implementation pairs)
data encapsulation
by packaging data and associated operations into unified entities (objects)
Two functions with identical lists of parameter types but different return types (can,cannot) be overloaded
cannot
Unification
compiler's term for determining how to instantiate a template function
Accessors should have ___________ appearing immediately after closing parenthesis
const
Object lifetime (3 parts):
construction -> utilization -> destruction
A ____________ is a special member function of a class that is used to initialize objects of the class as they are created
constructor
each time a class object is created (comes into existence) A ___________ is automatically called
constructor
The initializer list syntax can only be used in __________.
constructors
A _________ is called when a class object is declared with an existing object of the same class being supplied as the only argument
copy constructor
A ___________ is also called whenever a program generates copies of an object (e.g., when an object is passed by value to a function, when a function returns a copy of an object, or when temporary copies of an object are generated for other reasons)
copy constructor
Indigenous data
data that are completely contained by the structure
Exogenous data
data that reside outside the structure and are accessed through a pointer
Default values are specified in a function's ___________ but are not repeated in the definition
declaration
A _________ is called when a class object is declared without supplying any arguments
default constructor
A _________ is a special member function of a class that is used to perform any cleanup processing that is needed when objects of the class go out of scope (existence)
destructor
name
identifier for referencing variable directly
Principle of least privilege
in a particular abstraction layer of a computing environment, every module (such as a process, a user, or a program, depending on the subject) must be able to access only the information and resources that are necessary for its legitimate purpose
What is this? #ifndef #define ... ... ... #endif
include guard or macro guard
memberwise/shallow copying
initializing a new object by having members of the copied object refer to the same object as the original object. This can lead to stale pointers and memory leaks
set of rules that remain uncompromised as a component object is being put to use
invariant
const appearing immediately after closing parenthesis of a class method only protects the ___________
invoking object
>> and << cannot be overloaded using:
member functions
operator= must be overloaded with:
member functions
A __________ groups together related declarations (of names) for the purpose of avoiding name clashes.
namespace
when there is no other appropriate address for the pointer (perhaps for the time being) Initializing or assigning a pointer with the ________ is a way of preventing the pointer from being improperly used
null address
A pointer with a value 0 (the null address) is called the _______
null pointer
If two or more different functions are given the same name, that name is said to be ____________
overloaded
Default arguments must be the (rightmost/leftmost) arguments in function's parameter list
rightmost
The object-based paradigm overcomes the procedural paradigm's lack of emphasis on data by providing a mechanism that enables programmers to ______________
selectively restrict access to data
Information hiding
separating the interface (what) from the implementation (how)
The ___________ of a function is a list of the types of its parameters, including any const and reference or pointer indicators (& or *)
signature
The name of a function can be overloaded, provided no two definitions of the function have the same __________
signature
When a data member is declared as ______, only one copy of the data is maintained for all objects of the class
static
The expression template <class Item> is called the
template prefix
In addition to its name, a function's signature is determined by
the number, type and order of the function's parameter(s)
Function templates essentially allow us to parameterize:
the values and data types of function arguments
Normal functions only allow us to parameterize:
the values of function arguments
an "interface-implementation" pair of files is often referred to as a ________ or ________
unit, module
data/information hiding
user is shielded from data implementation and allowed to manipulate data only through some well-defined and well-behaved interfaces
A constructor that has parameters but has default value specified for each and every one of the parameters (through function-with-default-arguments feature) (will/won't) cover the default constructor.
will
Automatic default constructor limitations:
will do nothing (besides calling the default constructors of class data members that have default constructors)
Automatic destructor limitations:
will do nothing (besides calling the destructors of class data members)
Automatic assignment operator limitations:
will do only shallow (memberwise, bit-by-bit) copying
Automatic copy constructor limitations:
will do only shallow (memberwise, bit-by-bit) copying
if user decides to write one or more constructors (but doesn't cover the default constructor), compiler (will/won't) provide automatic default constructor
won't
Every C++ class must have the so-called "Big 4":
1. Default constructor ***********//Explicitly defined if class makes use of dynamic memory//****************** 2. Copy constructor 3. Assignment operator 4. Destructor
Answer true or false for this statement: An algorithm with worst case time behavior of 3n takes at least 30 operations for every input of size n=10.
FALSE
Answer true or false for this statement: For all possible inputs, a linear algorithm to solve a problem will perform faster than a quadratic algorithm to solve the same problem.
FALSE
Why should I prefer to use member initialization list?
For class members which are classes, it avoids an unnecessary call to a default constructor
A more exclusive (to C/C++) term for interface file
Header file