CPSC 121
syntax for .h files
#ifndef ClassName_h #define ClassName_h //class body goes here //must put all includes that are used in the file #endif
The value of a default argument must be a _______.
A literal value or a named constant
If you were writing the definitions for the Canine class member functions and wanted to place these in their own file, what should you name the file?
Canine.cpp
If you were writing a class declaration for a class named Canine and wanted to place it in its own file, what should you name the file?
Canine.h
_______ arguments are passed to parameters automatically if no argument is provided in the function call.
Default
______ variables are defined outside all functions and are accessible to any function within their scope.
Global (outside)
What is the advantage of breaking your application's code into several small functions?
It makes the code more manageable Makes it easier to find and pinpoint where errors occur in the code Minimize repetitive code
What does it mean to overload a function?
Overloading a function is when you write multiple functions with the same name. The functions are differentiated because they have different arguments.
_____ local variables retain their value between function calls.
Static
Static local variables
Static local variables are not destroyed when a function returns. The y exist for the entire lifetime of the program, even though their scope is only the function in which they are defined.
Find the error void getValue (int value&) { cout << "Enter a value: "; cin >> value&; }
The ampersand on value& should go in front of the of the variable name. (&value) As it is written, this is not a legal variable name, because the ampersand is considered a part of the variable name and special characters are not allowed. The cin statement should read cin>> value, as the ampersand does not need to be included when the variable is called.
Each of the following functions has errors. Locate as many errors as you can. void total(int value1, value2, value3) { return value1 + value2 + value3; }
The function has specified a return type of void in the header. Therefore, a return statement cannot be used in this function. You could change the header to return an int, or you could cout the values.
Find the error double average(int value1, int value2, int value3) { double average; average= value1 + value2 + value3 / 3; }
The function header specifies that the function will return a double, but there is no return statement.
True / False: you can use a mixture of parameters with and without default arguments
True • in the following function, only the last parameter has a default argument: // Function prototype void calcPay (int empNum, double payRate, double hours= 40.0); When calling this function, arguments must always be specified for the first two parameters (empNum and payRate) because they have no default arguments . Here are examples of valid calls: calcPay ( 769 , 15.75) ; // Uses default argument for hours calcPay(142, 12.00, 20}; // Specifies number of hours
When a function accepts multiple arguments, does it matter what order the arguments are passed in?
Yes, the arguments must be passed in the order that they appear in the function header parameter list.
If you are writing a function that accepts an argument and you want to make sure the function cannot change the value of the argument, what should you do?
You should pass the argument by value. The function will create a copy of the value and will not be able to alter the actual value in memory.
prototyping
allows you to call the function before its definition header with empty body returnType functionName(args); placed before the function is called
Reference variables are defined like regular variables, except there is an _________ in front of the name.
ampersand (&)
Variables that are sent into a function are called _______.
arguments
c++ primitive data types
bool char int float double void
declaring a 2x2 array
dataType arrayName[row][column];
A _________ constructor is one that requires no arguments.
default
A program contains the following function. void display(int arg1, double arg2, char arg3) { cout << "Here are the values: " << arg1 << • • << arg2 << • • << arg3 << endl ; } Write a statement that calls the function and passes the following variables to it: int age; double income; char initial;
display(age, income, initial);
syntax for printing out the contents of a 3x3 array called arr
for(int rows=0 ; rows < 3; rows++) { for(int columns = 0; columns <3; columns++) { cout<< arr[rows][columns]<<" "; } cout<< endl; }
An object's members can be declared public or private. Public members can be accessed by __________. Private members can be accessed by ________.
functions outside class, member functions of same class
The _______ is the part of a function definition that shows the function name, return type, and parameter list.
header
When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be defined ______.
last
A _____ variable is defined inside a function and is not accessible outside the function.
local
If a function has a local variable with the same name as a global variable, only the ______ variable can be seen by the function.
local (vs global)
A class may have more than one constructor, as long as each has a different _________________.
parameter list
Two or more functions may have the same name, as long as their ______________ are different.
parameter lists
Special variables that hold copies of function arguments are called _________.
parameters
A _________ eliminates the need to place a function definition before all calls to the function.
prototype
Either a function's ______ or its ______ must precede all calls to the function.
prototype, definition
Arrays are always passed by __________
reference
When used as parameters, ________ variables allow a function to access the parameter's original argument.
reference
passing by reference
refers to the process of passing a variable's address to a procedure so that the value in the variable can be changed indicated by the ampersand & syntax for header that passes a variable by reference returnType functionName (&variableName);
A program contains the following function. int cube(int num) { return num * num * num; } Write a statement that passes the value 4 to this function and assigns its return value to the variable result.
result = cube(4);
Constructors cannot have a __________ type.
return
The _____ statement causes a function to end immediately.
return
If function showValue has the following header: void showValue (int quantity), you would use the statement ______ to call it with the argument 5.
showValue (5)
passing by value
the process of passing a copy of a variables value to a procedure
Class objects passed to function are passed by _____ by default.
value
When only a copy of an argument is passed to a function, it is said to be passed by ________.
value
If a function doesn't return a value the word _____ will appear as its return type.
void
default arguments
• A default argument is passed to the parameter when the actual argument is left out of the function call. • The default arguments are usually listed in the function prototype. • Example: void showArea (double length = 20.0, double width= 10.0); the default arguments must be literal values or constants and have an = operator in front of them. • A function's default arguments should be assigned in the earliest occurrence of the function name. usually the function prototype. However, if a function does not have a prototype, default arguments may be specified in the function header.