-
More C++ Notes…
More C++ Notes… Step 1–Define the problem to solve. Step 2–Define a solution. Step 3–Write a program that implements the solution. Step 4–Compile the program. Step 5–Link object files. Step 6–Test program. Step 7–Debug. C++ files should end in ‘.cpp‘ extension to indicate a C++ source file. Ex: name.cpp Use a C++ compiler to compile a C++ (.cpp) program. The C++ compiler sequentially goes through each source code (.cpp) file in your program & does 2 important tasks: First, it checks the code to make sure it follows the rules of the C++ language. If it does not, the compiler will give you an error to help pinpoint what needs…
-
More Programming Notes…
Value–the representation of some entity that can be manipulated by a program. The members of a ‘type‘ are the values of that type. Data are characteristics, or information, usually numerical, that are collected through observation. In a more technical sense, data are a set of values of qualitative or quantitative variables about one or more persons or objects, while a datum (singular of data) is a single value of a single variable. In academic treatments, data are simply units of information. Data re employed in virtually every form of human organizational activity. Data are measured, collected & reported, and analyzed. Raw data (“unprocessed data” is a collection of numbers or…
-
Classes: A Deeper Look, Part 2–STRUCTURED PROGRAMMING Course Notes
Classes: A Deeper Look, Part 2 ‘const’ (Constant) Objects and ‘const’ Member Functions The keyword ‘const‘ can be used to specify that an object is not modifiable and that any attempt to modify the object should result in a compilation error. C++ compilers disallow non-const member function calls on const objects. An attempt by a const member function to modify an object of its class is a compilation error. A member function (behavior) is specified as const both in its prototype and in its definition. A const object must be initialized. Constructors & destructors cannot be declared const. const data member & reference data members must be initialized using member…
-
Classes: A Deeper Look, Part 1–STRUCTURED PROGRAMMING Course Notes
Classes: A Deeper Look, Part 1 Time Class Case Study Preprocessor directives (a.k.a. “preprocessor wrappers”)–Use preprocessor directives to form preprocessor wrappers. Ex: //prevent multiple inclusions of header file #ifndef TIME_H #define TIME_H … #endif Preprocessor directives ‘#ifndef‘ (“if not defined”) and #endif are used to prevent multiple inclusions of a header file. (If the code between these directives has not previously been included in an application, #define defines a name that can be used to prevent future inclusions, and the code is included in the source code file. Tip: Use the name of the header file in upper case with the period replaced by an underscore in the #ifndef and…
-
Pointers–STRUCTURED PROGRAMMING Course Notes
Pointers Pointer Variable Declarations & Initialization Pointers are variables that contain as their values memory addresses of other variables. The declaration: int *ptr; declares ptr to be a pointer to a variable of type int and is read, “ptr is a pointer to int.“ The *(asterisk) as used here in a declaration indicates that the variable is a pointer. There are 3 values that can be used to initialize a pointer: 0, NULL, or, an address of an object of the same type. The only integer that can be assigned to a pointer without casting is zero. Normally, a variable directly contains a specific value. A pointer contains the memory…
-
Arrays & Vectors–Arrays–STRUCTURED PROGRAMMING Course Notes
Arrays & Vectors Intro Data Structures are collections of related items. Arrays are data structures consisting of related data items of the same type. Arrays are “static” entities in that they remain the same size throughout program execution. (They may, of course, be of automatic storage class, and hence be created and destroyed each time the blocks in which they’re defined are entered & exited.) An array is a consecutive group of memory locations that share the same type. To refer to a particular location or element in an array, we specify the name of the array and the position number of the particular element in the array. A program…
-
Recursion–Functions–STRUCTURED PROGRAMMING Course Notes
Recursion A recursive function calls itself, either directly or indirectly. A recursive function knows how to solve only the simplest case(s), or so-called base case(s). If the function is called with a base case, the function simply returns a result. If the function is called with a more complex problem, the function typically divides the problem into two conceptual pieces–a piece that the function knows how to do, and a piece that it does not know how to do. To make recursion feasible, the latter piece must resemble the original problem, but be a slightly simpler or slightly smaller version of it. For recursion, to terminate, the sequence of recursive…
-
Function Templates–Functions–STRUCTURED PROGRAMMING Course Notes
Function Templates Overloaded functions typically perform similar operations that involve different program logic on different data types. If the program logic & operations are identical for each data type, overloading may be performed more compactly & conveniently using function templates. Given the argument types provided in calls to a function template, C++ automatically generates separate function template specializations to handle each type of call appropriately. All function template definitions begin with the ‘template‘ keyword followed by a template parameter list to the function template enclosed in angle brackets ( <> ). The formal type parameters are placeholders for fundamental types or user-defined types. These placeholders are used to specify the…
-
Function Overloading–Functions–STRUCTURED PROGRAMMING Course Notes
Function Overloading C++ enables several functions fo the same name to be defined, as long as these functions have different sets of parameters (a.k.a.–different signatures). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types, and order of the arguments in the call. Overloaded functions are distinguished by their signatures (function name & parameter types). The compiler encodes each function identifier with the number & types of its parameters to enable type-safe linkage. Type-safe linage ensures that the proper overloaded function is called and that the types of the arguments conform to the types of the…
-
Unary Scope Resolution Operator–Functions–STRUCTURED PROGRAMMING Course Notes
Unary Scope Resolution Operator C++ provides the unary scope resolution operator ( :: ) to access a global variable when a local variable of the same name is in scope. This makes it possible to declare local & global variables of the same name. The unary scope resolution operator cannot be used to access a local variable of the same name in an outer block. A global variable can be accessed directly without the unary scope resolution operator if the name of the global variable is NOT the same as that of a local variable in scope. Tip: Always using the unary scope resolution operator ( :: ) to refer…