Midterm Flashcards
_______ declared _____ are meaningful only within
the file they are declared in
Globals declared static are meaningful only within
the file they are declared in
Globals visible in other files by declaring it as _____
extern
Global variables are in scope from moment of declaration until ___ ___ ___
end of file (not in-scope earlier in file)
a variable defined inside a _____ is
meaningful only inside that function
• It is said to have _____ scope
a variable defined inside a function is
meaningful only inside that function
• It is said to have local scope
int count = 0; // Create a global variable
int count_items() { int orig_count = count;
int count = 3; for (int i = 0; i < 4; ++i) count++; return count + orig_count; }
what is the output? int main() { cout << count << endl; cout << count_items(); }
0
7
When an inner variable has the same name as an outer variable, the outer variable is _______ by the inner variable
When an inner variable has the same name as an outer variable, the outer variable is shadowed by the inner variable
double balance; // Balance has global scope class BankAccount { public: BankAccount(double balance); // Parameter variable balance has local scope private: double balance; // Data field balance has class scope };
BankAccount::BankAccount(double balance) { this->balance = balance; // Assign \_\_\_\_ \_\_\_\_\_\_ \_\_\_\_\_\_ using parameter value \::balance = balance; // Assign \_\_\_\_\_\_ \_\_\_\_\_\_ using \_\_\_\_\_\_ value }
double balance; // Balance has global scope class BankAccount { public: BankAccount(double balance); // Parameter variable balance has local scope private: double balance; // Data field balance has class scope };
BankAccount::BankAccount(double balance) { this->balance = balance; // Assign data field balance using parameter value \::balance = balance; // Assign global variable using parameter value }
What is definition of variable scope?
Where a name is visible
What is definition of variable lifetime?
How long a variable exists during program execution
• A static local variable is allocated and initialized when
the program loads, and exists until _____ _____
• Only visible to __ _______
• A static local variable is allocated and initialized when
the program loads, and exists until program exits.
Only visible to the function
What functions can access protected sections of a class?
functions within that class and children of that class
How should the protected designation be used?
Same caution as public. Only accessors
• It is common to grant friendship to the _____ operators
• It is common to grant friendship to the stream operators
Friend functions and classes can access what data of the class friendship is defined in?
Can access all protected and private data
Substitution principle: If B inherits from A, then ______
_____________________
Substitution principle: If B inherits from A, then B can be used wherever A is expected