Programming exam Flashcards
Declaration/ header (.h)
In the header file, a developer provides the declaration of a class’ interface. In essence, this gives the “shape” of the class
Definition/Implementation (.cpp)
In the definition file, a developer provides the code that is run each time a function member is called.
Use/ Driver (.cpp)
In a driver file, an external program references the header file using a #include statement and uses the class members (via objects or instances of that class) to solve a problem.
Class Declaration
A class declaration takes this general form
class <class>{
private:
//a series of private data and function member declarations
public:
//a series of public data and function member declaration</class>
Class Definition
The definition of a class provides definitions for functions that are declared in the class declaration. This linking is performed using the scope resolution operator “::” between the class name and the member name. An example definition would look like this:
<return> <class>::<function> (<parameter>) {
//code goes here
}
</parameter></function></class></return>
Using a Class
In a driver program, one would first create instances of a class by declaring a variable of the appropriate type:
Employee e;
After the variable is declared, the programmer now has access to all of the public data and function members using the class member access operator, also known as the “dot” operator:
e.Inactivate()
Compiling a Class and Driver
While one could use a single command approach to compilation, it is better to use a multi-step approach that separates the header file from compiled source code. An example compilation of a class and driver with the three files outlined above would be:
Step 1: g++ -c <class>.cpp
Step 2: g++ -c <driver>.cpp
Step 3: g++ <class>.o <driver>.o -o <desired>.exe
In English:</desired></driver></class></driver></class>
Step 1: compile the class source code into consumable object code
Step 2: compile the driver class into object code
Step 3: link the class object code with the driver object code to create an executable file
Private class members
can only be accessed within the definition of a class. That is, any attempt to reference a private data or function member from a function or code location that is not associated with the same class using the scope resolution operator (::) is a syntax error.
Public class members
can be accessed from anywhere an object of the appropriate type is available. The most common example would be a driver program using public function members from an object of a class declared as a local variable within that driver program.
Constructors
Constructors are special function members that are used to create objects or instances of a class. These functions contain behavior that is run when a driver program creates a variable with the type of your class.
Initialization: Initialize the object’s attributes when it is created.
Same Name as Class: It has the exact same name as the class.
No Return Type: Constructors do not have a return type, not even void.
Automatically Called: It gets called automatically when an object is created.
Accessors
are public function members used to access the private value
Mutators
are public function members used to change the private value.
Getters
are accessors that only provide access to a private data member.
Setters
are mutators that set a private data member to a value specified as a passed in parameter
A(n) ___________ function has access to both public and private data members of a class but is NOT a member function:
friend
Classes encapsulate two types of members: function members that define business logic and ______ members that define the state of a class.
data
_______ is a keyword used to ensure that conversion constructors are NOT used implicitly.
explicit
The _______ operator is used to link a member function to a class.
::
The _____ design methodology is used when creating a C++ program or class.
DDU
When creating an operator override, the keyword _______ is used to name the implementation method:
operator
The name of a destructor must begin with the special character:
~
A(n) ________ is used to set constant data members of a class:
Initialization list
++ is a(n) _______ operator.
Unary
The keyword “friend” is used in a functions’s:
Declaration
Conversion constructors can accept multiple paramters (T/F)
False (can only have one parameter, its the type its converting from)
Destructors accept only one parameter (T/F)
False
A class can have only one implementation of an override for a given operator (T/F)
True
A driver program cannot access private data or function members. (T/F)
True
Constant member functions can call other non-constant functions. (T/F)
False
A class can have an arbitrary number of constructors. (T/F)
True
The -c flag is used to create object code from a source cpp file with g++. (T/F)
True
Classes are instances of objects. (T/F)
False
A driver program can create an arbitrary number of objects from a class. (T/F)
True
A setter should always be marked const. (T/F)
False
A ________ member can be accessed outside of the class implementation but a ______ member cannot.
public, private
Getters are a special type of ______ that provide access to _____ state.
accessor, private
A _________ is a special function member that specifies defaulting logic for a class’ data members.
Constructor
Which of the following is TRUE about constructors?
They are a function with the same name as the class