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
How does inheriting privately from a base class work?
The child class can access base class methods only internally and member fields do not flow through. The child class objects can not access base class methods.
What is the syntax for declaring a namespace?
namespace name_space_name { class Example { }; }
What is the syntax for dumping a namespace into global namespace?
using namespace name_space_name;
Namespaces should be short or long and descriptive?
long and descriptive
How are namespaces shortened to a manageable length?
Aliasing
nsn = name_space_name;
How do you dump an individual name into the namespace. (ex: std::cout)
using std::cout;
namespaces should NEVER be used in what type of file?
header (included) files because the user does not necessarily know what namespace they are dumping into their global namespace and name ambiguities can occur.
A destructor is automatically invoked when ______ ______ _________
A destructor is automatically invoked when a class variable is no longer in scope.
Two syntexes for calling copy constructor
String first("Fred"); String second(first);
String second = first;
If the user does not write a copy constructor, do copy constructors exist?
Yes the compiler automatically creates one however it uses memberwise copying
What is the problem with the constructor provided by the compiler?
Creates only shallow memberwise copying. Problem when copying pointers
What is the syntax of a copy constructor?
String::String(const String& right) {}
What are the three things that an assignment operator must do?
Check for self assignment
Clean up existing resources
Create a copy of values in rhs
What are the 8 instances when a destructor is called?
- Local variables, at end of block
- Parameters, at end of function
- Temporary variables, at end of statement
- Heap values, when delete is called
- Static values, when main terminates
- Member data, when object is deallocated
- Base class object, when derived class object is deallocated
- Local variables, when an exception is thrown and execution
leaves the block
What are the six memory management functions?
1.Copy constructor
2.Assignment operator
3. Destructor
4.Default Constructor
5.Move Constructor (faster than a copy constructor because it
doesn’t allocate memory nor does it copy memory buffers).
6.Move Assignment operator
What is the assignment operator syntax?
&Example Example::operator=(const Example& rhs) {}
What would happen if you supplied a destructor but no constructor? And one shallow copy of an object went out of scope before the original or vise versa?
The destructor would be called to reallocate resources and an error would emerge if the object was used because it referenced resources that are no longer allocated
Each parameter must be preceded by the keyword _____ or ______
Each parameter must be preceded by the keyword class or typename
double e = max(2, 3.13)
Why isn’t this allowed? How can it be fixed?
double e = max(2, 3.13);
What is the template statement above this class definition? class Pair { public: Pair(const F& a, const S& b); F get_first() const; S get_second() const; private: F first; S second; };
template
What are the four conditions for when paint event is called?
- The widget is shown for the first time.
- The widget is resized.
- The widget is obscured by another window, and then is revealed again.
- We force it by a call to repaint() or update().
Write an int main for a qapp
#include #include
int main(argc, *argv[]) { QApplication(argc, argv); MainWindow w; w.show();
return app.exec();
}