C++ Study Guide for Test 2

  1. What are the valid data types that can be used in the controlling expression of a switch statement?
    bool, int, char, and enum.
  2. Why is the switch statement good for processing menu choices?
    It presents a list of alternatives on the screen for the user to choose from.
  3. Can the break statement be used in switch, while, do-while, and for-loops?
  4. What does the break statement do in while, do-while, and for-loops?
    It causes the loop statement to end immediately and execution continues with the statement following the loop.
  5. What does the continue statement do in a while, do-while, and for-loops?
    The continue statement transfers control to the update expression.
  6. What are the 3 parts of the for-loop definition?
    • 1. Initialization action.
    • 2. Boolean expression thats used to check for when the loop should end.
    • 3. How the loop control variable is updated after each iteration of the loop body.
    • i.e. (int num =1; num <= 10; num++)
  7. In the initialization action, if the controlling variable is not already defined, can it be defined as a local variable of the for-loop?
  8. Why use files?
    Files provide a way to store data permanently, the file remains after the program has finished running, and there's no need to type data over and over.
  9. What is sequential file processing?
    It is the processing the records in the order they are stored in a file.
  10. What is the definition of an object?
    Objects are variables declared using a class as the data type.
  11. What is the definition of a class?
    A Class is a data type that has data (variables and values) as well as functions associated with it.
  12. When should the close member function be used?
    • When the program is finished getting input from the file or sending output to the file. It disconnects the stream from the file.
    • i.e. inStream.close( ); and outStream.close( );
  13. What is considered the external filename?
    The actual name of the file.
  14. What is considered the internal filename?
    The name of the object.
  15. What is passed to the open function as an argument? The internal or external name?
    • The external name in double quotes enclosed in parentheses.
    • i.e: outStream.open("outfile.dat");
  16. What filename is declared when declaring an ifstream or ofstream object?
    • The internal filename is declared.
    • ifstream inStream;
    • or
    • ofstream outStream;
  17. When should the fail member function be used?
    • To test whether a stream operation has failed.
    • i.e: inStream.fail( );
  18. The eof flag is set when the program attempts to read past the end of file. How should we use the eof member function in a program?
    • To test that we are NOT at the end of a file. Its used with a not in front.
    • if(! fin.eof( ))
    • ...
    • else
    • ...
  19. When calling a member function, the following syntax is used:
    i.e: objectName.memberName(arguments);
  20. In the example function call outFile.open("outfile.dat")
    is the objectName,
    is the memberName, and
    is the argument.
  21. Can the formatting flags (precision, width, etc) be used when writing to a file?
    Yes, it tells the computer to write output to that stream in some specific way.
  22. Can the formatting functions (precision, width, etc) be used when writing to a file?
  23. Which formatting function is needed to right align numeric information?
    i.e: ios::right
  24. What are manipulators?
    • A Manipulator is a function that is called using the insertion operator as if it were a call to be output.
    • i.e: cout << setw(6);
  25. What characters are considered whitespace?
    Blank or space character, tab, and the new-line character.
  26. What to the toupper( ) and tolower( ) functions do?
    Converts uppercase letters to lowercase and vice versa.
  27. What to the isupper( ) and islower( ) functions test for?
    They test if the character expected is true to an uppercase or lowercase letter is provided, otherwise returns false.
  28. What is an array?
    It is a list of variables with a uniform naming mechanism that can be declared in a single line of simple code.
  29. Each array element is referenced by an index, which is sometimes called a subscript.
  30. What is the declared size of an array?
    • It is the size of the array given in braces.
    • i.e. arraySize[10];
  31. If the declared size is stored in a variable like MAX_SIZE, must it have a const modifier?
  32. When an array is declared, is the declared size stored anywhere by C++ automatically?
    • No, the computer reserves enough memory to hold the declared size.
    • The address of the first entry and the base type.
  33. Is the first element in the array referenced with an index of 0 or 1?
    The first element in an array is always referenced with an index of 0.
  34. When does an index become an out of range index?
    When an attempt to reference a nonexistent array index is made.
  35. Who must check for an out of range condition? C++ or the programmer?
    The programmer who wrote the array.
  36. For an array declared as int student[5], how many int elements are declared?
    • 5 with indexes from 0 through 4.
    • i.e. student[0] is the first and student[4] is the last.
  37. In the declared array, int student[5], what index value would reference the last valid array element?
  38. In the declared array, int student[5], what would be the first index value that would be considered out-of-range?
  39. What type of parameter in a function definition is used to pass the address of where the array begins?
    • An array parament.
    • i.e. void displayQty(int qty[], int arraySize);
  40. Can a single array element be passed to a function?
  41. Why is an array parameter more like a call-by-reference than a call-by-value parameter?
    Because the memory address is sent when a call is made to an array parameter.
  42. What modifier can be used with an array parameter to simulate a call-by-value?
    Using the const modifier prohibits functions from changing values in arrays.
  43. Why do we need to pass the declared size to a function?
    It is important for processing so its size must be passed to functions.
  44. Why do we need to keep track of how many items were loaded into a partially filled array?
    So that the array isn't overfilled and does not reference any indexed variable that has not been given a value.
  45. Why do we need to pass the number used or the number of items loaded to a function?
    So the values of the indexed variables of the array argument can be changed by the function.
  46. What are multidimentional arrays?
    Its is an array with more that 1 index, which is actually an array of arrays.
  47. How many subscripts [] are needed to reference a particular element in a multidimensional array?
    2, row by column.
  48. What row and column in a multidimensional array would the element qty[2][2] be located?
    Third row, third column.
  49. What is a cstring variable?
    It is an array of of char (characters), its a string type inherited from the C language.
  50. What is the base type of cstring arrays?
    Non-numeric characters.
  51. What is the Null character and what is its purpose in cstring processing?
    • '\0'
    • Which is represented as an escape character which marks the end of the cstring.
  52. What is the purpose of the cstring functions strlen, strcpy, strcmp, strncpy, strncmp?
    • strlen: returns an integer equal to the length of the string.
    • strcpy: copies the cstring value from 1 string to a target string.
    • strcmp: compares 2 strings, 0 if equal, <0 if less than, >0 if greater.
    • strncpy: copies the cstring value from 1 string to a target string, with limit characters.
    • strncmp: compares 2 strings, 0 if equal, <0 if less than, >0 if greater than, with limit characters compared.
  53. What are the equivalents of strcpy, strlen, and strcmp for the class string?
    • string str is to strcpy
    • str.length( ) is to strlen
    • str1 == str2, str1 != str2, is to strcmp
  54. What is the purpose of a structure?
    struct Name
    double num1;
    double num2;
    int length;
    • To combine data of different types into a single (compound) data value.
  55. What is included in a structure definition?
    • 1. The tag struct followed by the NameOfStruct
    • 2. Open brace, {
    • 3. Member variables
    • 4. Closing brace with semicolon, };
  56. What is a class?
    class NameOfClass
    void input( );
    void output( );
    A Class is a definition of a new data type whose variables are objects?
  57. What is an object?
    A Object is a variable declared using a class definition as the data type. It has member functions as well as the ability to hold data values.
  58. What should be included in a class definition?
    Member variables as well as associated functions.
  59. Where should the actual class function definitions be entered?
    Before the main part of the program.
  60. How are member variables and functions referenced after declaring an object?
    • With the scope resolution operator. ::
    • i.e: void ClassName::functionName(parameter list);
  61. What is the difference between public and private variables?
    • public: can be referenced and altered by functions outside of the class.
    • private: can only be referenced and altered by member functions.
  62. What is the difference between public and private functions?
    • public: can be called by functions outside of the class.
    • private: can only be called by member functions.
  63. What is the purpose of accessor functions?
    It keeps programmers from referencing variables that they shouldn't.
  64. How many values should accessors return?
    The accessor function need not literally return the values of each member variable but they must return something equivalent to those values.
  65. What is a constructor?
    A Constructor is a member function of a class that is called automatically when an object of the class is declared.
  66. What are rules in naming constructors?
    • 1. Same name as the class.
    • 2. Cannot return a value.
    • 3. Used to initialize member variables.
    • 4. Automatically called when an object of that class type is declared.
  67. Should constructors be public or private?
  68. Can constructors be overloaded?
    Yes, with the same function name but with a different number or type of parameters.
  69. Can constructors return a value?
    No value is returned.
  70. When are constructors called?
    When an object of that class type is declared.
  71. Can constructors be called explicitly?
    Yes, after an object has been declared.
  72. Does the compiler define a default constructor if one is not defined by the programmer?
    Yes, a default constructor should always be defined with no parameters.
  73. What is a destructor?
    • A member function that is automatically called when an object goes out of scope.
    • i.e. ~DestructorNameOfClass;
  74. What are rules in naming destructors?
    • 1. Must be public.
    • 2. Same name as the class.
    • 3. Begins with a ~ tilde.
    • 4. Cannot return a value.
    • 5. No return type, not even void.
  75. When are destructors called?
    When an object goes out of scope.
  76. What is an abstract data type (ADT)?
    It is a data type that details are not needed to know about how the values and operations for that type are implemented.
  77. What does the interface tell us about an ADT?
    It tells us how to use the ADT. It consists of the public member functions of the class along with the comments that tell you how to use these public member functions.
  78. What does the implementation tells us about an ADT?
    Its the actual C++ code. It contains private variables and members of the class and the definitions of public and private member functions.
  79. Why are functions named as friend functions of a class?
    It grants access to private members.
  80. Do friend functions gain access to the public or private variables and functions?
    Access is gained to private variables and functions.
  81. What is the definition of inheritance when talking about classes?
    Inheritance is the process by which a new class is created from another class but the new class has additional member variables and/or functions.
  82. What is a base and derived class?
    • Base class: is the highest class and does not inherit from any other class.
    • Derived class: is created from a base class. It has all the member variables and functions from the base class with additional variables and functions.
  83. What is a parent and child class?
    • A parent class would be a base class.
    • A child child class would be a derived class from a parent class.
  84. How are protected class members inherited in the derived class?
    Inherited protected members are inherited in the derived class by being marked protected in the derived class. It then can be accessed by name in the definitions of all descendent classes.
  85. What is stored in the interface file?
    • The interface file includes all of the class definitions (public, private, and protected).
    • It saves contents in a header file and is not compiled.
  86. What is stored in the implementation file?
    • The implementation file #includes the interface file and class function definitions.
    • It saves contents to a .cpp file and must be compiled.
  87. What is stored in the application file?
    • The application file contains main and #include interface file since the object will be declared and members referenced in the app.
    • Main, function declarations, and definitions that make the app are store in the app file in a .cpp file and must be compiled.
  88. What is the purpose of #ifndef, #define, and #endif?
    • #ifndef: checks if some macro is defined or not.
    • #define: tells the preprocessor compiler has been defined.
    • #endif: is the end of the #ifndef condition.
  89. In what order should, #ifndef, #define, and #endif, be entered into a program?
    • #ifndef NAME_H
    • #define NAME_H
    • ...class definition...
    • #endif // End of NAME_H
Card Set
C++ Study Guide for Test 2
C++ Study Guide for Test 2