AP Computer Science: The C++ Subset The subset of C++ used in AP Computer Science courses is documented here. This document is only the C++ subset, it is neither a description nor a curriculum for an AP Computer Science course. A detailed topic outline of the AP Computer Science courses is provided in the main portion of this course description on pages 5-9. A more detailed description and rationale for the subset follows the subset. The most current information about the AP C++ Subset and the AP C++ classes is available at www.collegeboard.org/ap in the Computer Science section of “AP Subjects.” In the three-column format used in the subset description below, any feature or topic listed in the second (AB) column is specifically not part of the A course. The third (comment) column explains and elaborates on the topics listed in the first two columns. Four levels of understanding are used in the topics outline: read, use, modify, and imple- ment. For example, the outline says that AP Computer Science A students should be able to “read, use, and modify classes.” This means that they should be able to read class declarations, use the classes in client programs they write, and modify a class implementation by adding a new member function or modifying an existing member function. AP Computer Science A students are not expected to design and implement classes from scratch. When the word “use” modifies a language feature, as in “use const member functions,” students are expected to be able to write programs that use the feature. A special section, “Topics not Covered”, includes features of C++ that are not part of the AP Computer Science C++ subset and therefore will not be tested on the AP Computer Science exam. This section is divided into three subsections. The first subsection lists language features that teachers may want to include in their courses, but that will not be tested on the AP Computer Science exams. The second subsection lists language features that we do not view as useful in the AP Computer Science courses. The third subsection lists language features that we view as dan- gerous—these features should be avoided. Teachers and students are free to use language features in these “not covered” sections, but the features will not be tested using questions that are on the AP Computer Science examinations. A and AB course AB course only Comment Classes Read, use, and Design and implement APCS A students read class modify classes classes. declarations, write client programs, add/modify member functions. Read, use, and Implement copy Constructors should implement constructors and use initializer lists constructors, destructors. as opposed to assignments including to data because sometimes initializer lists. initializers are required, so use them for uniformity. Differentiate between No public data are used in public and private. classes. Since inheritance is not part of APCS, there is no reason to use protected functions/data. 45 A and AB course AB course only Comment Classes Read class definitions Use, modify, and implement that include use of class member functions *this. that include *this. Implement overloaded Implement overloaded Students will NOT be tested functions; use operators, including on recognizing that functions overloaded operators. operator= and differing in return type only operator<< (but friend are overloaded incorrectly. not used). Use and implement Recognize when to const member make a member functions. function const. Use AP string class. The AP string class is a limited, safe subset of the standard string class. Use templated AP Use and reimplement vector and matrix templated AP stack classes. and queue classes. Design and implement templated classes and functions. Differentiate interface and implementation of class (.h and .cpp file). 46 A and AB course AB course only Comment Properties of Language Boolean expressions such as (a && b) are evaluated left-to-right; expression evaluation stops when the value of the entire expression can be determined. This means that constructs such as while(k < n && a[k]!= key) can be used safely. Use built-in types: int, For compilers that do not use char, double, bool. 32 bit integers, long int may be used, otherwise no modifiers Use ++ and -- only as shorthand for +=1 and -=1; do NOT use these operators in expressions like a[k++] = 0. Use break. Used in switch statements, can be used in loops. The continue statement will not be tested. Use function syntax For some types, e.g., long int, for typecasts, e.g., another form of cast is necessary: cout << double(x)/3. (long int) x. When compilers support the static_cast< > operator it will be used. Use pointers: The address of operator & is operators *, ->, used to test for aliasing in new, delete; NULL operator=, but not used preferred to 0; use elsewhere. Pointer arithmetic, == and != with pointers to functions, and pointers. pointer comparison using < and > will NOT be tested. Use [ ][ ] in matrix Reinforce vector of vectors class for indexing. and mimic notation for built-in arrays. 47 A and AB course AB course only Comment Properties of Language Use #include and No other use of #ifndef idiom in preprocessor; const header files. variables used instead of #define. Use escape sequences, \n, \t, \\, \',\" Use value, reference, and constant reference parameters. Recognize that Read and use values returned by reference return operator [ ] for types and string and vector understand the can be assigned to, implications of e.g., s[0] = 'a' returning reference is allowed. to local variable. Use the functions fabs, pow and sqrt from . Use constants INT_MAX, INT_MIN from Stream Properties Use cin, cout, <<, Assume input data is >>,endl. “type-safe”, e.g., when int expected int is read. cerr not tested, but can be useful. Use ifstream, Students will use open, but ofstream. it will not be tested since parameters to open may be compiler specific. Use getline. getline() is a free function in the AP string class, consistent with the standard string class function getline. Use istream &, Students should realize that ostream & as formal different types of streams can parameters. be passed as arguments when formal parameter is istream &, but inheritance hierarchy of streams will not be tested. 48 A and AB course AB course only Comment NOT part of APCS C++ Not tested, but potentially useful assert iomanip (but endl Students and teachers will used) find manipulators useful in formatting output, but iomanip will not be tested. stream member Use while (cin >> x) idiom, functions eof(), or test return value of getline for good(), bad(), end of input. fail(), ignore(), etc. string streams Input string streams are useful for parsing lines of data; outputstring streams facilitate conversion to string values. get(char &) Useful for reading stream input one character at a time. default parameters Function overloading can be used instead. typedef More useful in C than in C++ when classes are used. functions in Many programs require these , e.g., functions, but their use will sin, cos, tan, not be tested. floor, ceil functions in Facilitates system independent , e.g., testing and conversion of isalpha, isdigit, characters, but not tested. islower, ispunct, isspace, isupper, tolower, toupper constants in Can be useful, but , will not be tested. DBL_MIN, DBL_MAX command line Can be useful, but will not arguments, argc, be tested. argv[ ] 49 A and AB course AB course only Comment not tested operators: comma, These operators are not essential. ?:, bitwise, sizeof goto Although there are occasions when goto can be useful, indiscriminate use is dangerous; goto is NOT part of the AP C++ subset. reference variables Reference parameters and reference return types are part of the AP C++ subset, but reference variables are not. inline functions Concerns with efficiency at the level of use of inline functions are not part of the AP course. In general, code will not appear in class declarations (defaulting to inline). friend classes and Use public Display/Print functions member function instead of overloaded operator<< when overloaded operator<< would require use of “friend”. promotion Implicit promotion or conversion, especially in parameter passing, will not be tested. inheritance While inheritance is a central theme of object-oriented programming, this topic and its implementation in C++ are not part of the current AP C++ subset. union Not necessary in programs studied and implemented in AP courses. 50 A and AB course AB course only Comment Troublesome and warned against built-in arrays No need for this given use of templated vector class. The built-in array type is fraught with problems, e.g., range checking, pointer similarity. Recognition of limitations makes built-in arrays an interesting topic for study. C-style (char *) strings No need for this given the apstring class. new[ ]/delete[ ] and No need for [ ] (except with malloc/free built-in arrays) and use new/delete rather than malloc/free. stdio.h scanf/printf, etc. NEVER used. &, the address of Used only in test for operator aliasing in operator=. AP Computer Science C++ Subset: Rationale A Rationale/Elaboration of the AP Computer Science C++ subset Not all aspects of the C++ language are mentioned in the AP C++ subset. In particular, the con- trol constructs below are used in the AP C++ subset. if, if-else, switch while, do-while, for More esoteric language features such as operator —>* or operator .* are not part of the subset. Certainly esoteric means different things depending on oneÕs level of C++ expertise. However, the AP C++ subset is complete enough to implement programs typically encountered in the first two courses in computer science. Overloaded functions and operators Function overloading is a simple form of static (or ad hoc) polymorphism and is included in the C++ subset. Operator overloading is a simple extension of function overloading and is very use- ful. For instance, it allows comparison operators for strings so that comparison of string values has the same syntax as comparison of int and double values. Operator overloading is also used to support assignment and stream I/O, the operators =, <<, and >>. In C++, the return type of a function is not part of a functionÕs signature for the purpose of resolving an overloaded function call; this is considered of minor importance and will not be tested.