Classes Flashcards
Header
Text file defining class and implementation
Client code needs to know what about a class?
Member functions
Arguments for member functions
Return type of member functions
Interfaces
Define how things interact with eachother
What consists of a class’s interface?
Public member functions
How do you separate a class’s interface from it’s implementation?
Class definition in header and member functions defined in source code file
Function prototypes
Describe class’s public interface without revealing implementation
Why does class header specify class’s private data members?
Compiler must know how much data to reserve for each object of the class
Class header
Provides compiler with info to ensure class’s member functions are called correctly
What is an include guard?
ifndef, #define, #endif
Prevents header from being included multiple times
Scope resolution operator
::
Tells compiler that the member function is within that class’s scope/definition
Why include class header in source code file?
Ensures member function matches prototype and member functions know class data members and functions
What exception can a function throw?
invalid_argument
What is a throw statement?
Creates and throws an object of the type specified
What happens after exception object is created?
throw statement immediately terminates the function and the exception is returned to the calling function
What are objects of ostringstream?
Same functionality as cout but writes their output to string objects in memory
ostringstream’s str member function does what?
Returns the string created
Stream manipulator setfill
Specifies fill character when integer is output in field wider than number of digits in value
Sticky setting
Applies to all subsequent values displayed, ex: setfill
What is a member function defined in a class’s body?
Implicitly declared inline
Defined classes can be used as types in declarations of…
Objects, references, and pointers
Member functions are stored __________ from the objects of the class
Separately
What belongs to a class’s scope?
Data members and member functions
Nonmember functions are defined at what scope?
Global namespace
Within class’s scope, class members are accessible to….
Member non-static functions
Outside of class scope, class members are referenced through….
An object name, a reference to an object, or a pointer to an object
What has block-scope?
Variables declared in a member function
Dot member selection operator (.)
Accesses the object’s, or reference to an object’s, public members
Arrow member selection operator (->)
Used with pointer to access object’s public members
Access functions
Read or display data
Also test truth/falsity (called predicate functions)
Utility functions
Private member function that enables use of public member functions.
Not intended for use by clients
How do you overload?
Provide prototypes and constructors for each overloaded version
How can a constructor call other constructors in the same class and what is it called?
Use a member initializer with the name of the class
Called a delegating constructor
Destructor
Called implicitly when an object is destroyed
Uses ~
Destructor’s termination housekeeping
Before system can reclaim an object’s memory, it’s reused to hold new objects
How many destructors does each class have?
1, if not explicitly, compiler creates empty destructor
What order are destructor calls made?
Reverse of constructor calls
Global and local static destructors are called after all non-static local objects are destroyed
What is function exit?
Terminates program immediately after fatal unrecoverable error occurs
Does not execute destructors
What is function abort?
Terminates program immediately
Does not allow any programmer-defined cleanup
Indicates abnormal termination
What are references?
Alias for name of object
Acceptable lvalue and can receive a value
When can a reference not be used as a modifiable lvalue?
If the function returns a reference to const data
What is memberwise assignment?
= used to assign an object to another object of the same type
Objects can be passed by value or returned by value from functions. How?
Copy constructor creates new object and copies original values in
const keyword
Object is not modifiable
non-const member calls on const member objects
Compiler does not allow
const member function attempt to modify object
Compilation error
Where is member function declared const?
Prototype and definition
const objects must be ________.
Initialized
What cannot be declared const?
Constructors and destructors
What is composition?
Class can have objects of other classes as members
When are member objects constructed in enclosing class objects?
Before
Friend functions
Can access all the class’s members.
Defined outside class’s scope.
Where is friend declaration allowed?
Anywhere in the class
Is friend relationship symmetric or transitive?
Neither
this pointer
Object can access its own address
Not part of object itself
this pointer is passed as an implicit argument to each ______________
Non-static member function
This pointer can be called explicitly to reference
Data members and member functions
Cascaded-member-function calls
When multiple functions are invoked in the same statement. Enabled by this pointer.
Static data members
Class-scope
Can be public, private, or protected
How to access public static class member when no object exists?
Prefix classname with name of data member and scope resolution operator
Static can be applied to definition outside of class definition? T or F
True
When should a member function be declared static?
If it does not access non-static members