-
Random Number Generation–Functions–STRUCTURED PROGRAMMING Course Notes
Random Number Generation rand(); a C++ Standard Library function that generates an unsigned integer between 0 and RAND_MAX (a symbolic constant defined in the <cstdlib> header file). The function prototype for the ‘rand’ function is in <cstdlib>. rand()%6 to produce integers specifically in the range 0 to 5, for a dice-rolling game, for example, we use the modulus operator(%). This is called scaling. The number 6 is called the “scaling factor”. We would then shift the range of numbers produced by adding 1 to our previous result. ex: (1 + rand() % 6 ) srand() the function ‘srand‘ takes an unsigned integer argument and seeds the ‘rand‘ function to produce…
-
Functions–STRUCTURED PROGRAMMING Course Notes
Functions Experience has shown that the best way to develop & maintain a program is to construct it from small, simple pieces or components. This technique is called “divide & conquer”. Declare and use functions to facilitate the design, implementation, operation, and maintenance of large programs. Functions (aka methods, procedures, sub-routines) allow us to modularize a program by separating its tasks into self-contained units. We can use a combination of provided library functions or create out own functions, known as user-defined, or programmer-defined functions. The statements in function bodies are written only once but can be reused from perhaps several locations in a program and are hidden from other functions.…
-
Control Statements–STRUCTURED PROGRAMMING Course Notes
CONTROL STATEMENTS Theory & Principles of Structured Programming. These concepts presented here are crucial to building effective classes & manipulating objects. BEFORE writing a program to solve a problem, we must have a thorough understanding of the problem & a carefully planned approach to solving it. WHEN writing a program, we must also understand the types of building blocks that are available, & employ proven program construction techniques. An ALGORITHM is a procedure for solving a problem in terms of the actions to execute and the order in which to execute them. Specifying the order in which statements execute in a program is called PROGRAM CONTROL. Note: In addition to…
-
Data Members, ‘set’ Functions, & ‘get’ Functions–C++ Notes (C Plus Plus Notes)
Variables declared in a functions body are local variables, and can be used only from the point of their declaration in the function to the immediately following closing right brace (}). When a function terminates, the values of its local variables are lost. A local variable must be declared before it can be used in a function. A local variable cannot be accessed outside the function in which it’s declared. Data members normally are private. Variables or functions declared private are accessible only to member functions of the class in which they’re declared, or to friends of the class. When a program creates (instantiates) an object of a class, its…
-
Defining a Member Function With a Parameter-–C++ Notes (C Plus Plus Notes)
Defining a Member Function With a Parameter A member function can require one or more parameters that represent additional data it needs to perform its task. A function call will supply arguments for each parameter. A member function is called by following the object name with a dot operator (.), the function name, and a set of parenthesis containing the function’s arguments. A parameter list may contain any number of parameters, including none at all (represented by empty parentheses) to indicate that a function does not require any parameters. The number and types of arguments in a function call must match the number & types of the parameter list.
-
Declarations, Prototypes, Definitions, & Implementations–C++ Notes (C Plus Plus Notes)
Declarations–a declaration introduces a name into a scope. Generally speaking, a scope is either an entire ‘.cpp’ file (source file) or anything in code delimited by {} (braces), be it a function, a loop within a function, or even an arbitrarily placed block of {} within a function. A name introduced, is visible within the scope from the point at which it is declared to the end of that scope. A declaration merely tells the compiler how to use something, it does not actually create anything. Prototypes–a prototype is just another name for a declaration of a function. Definitions–a definition fully specifies an entity. Definitions are where the actual creation…
-
Header Files, Source Files, & Extension Names–C++ Notes (C Plus Plus Notes)
What’s the difference between Header files & Source files? Basically, header files are #included and not compiled, whereas source files are compiled and not #included. So do not compile ‘.h‘ files, and do not #include ‘.cpp‘ Source files. Also, do not #include the same .h file twice. This will lead to an error. Extensions All files are fundamentally the same in that they’re all text file, however different kinds of files should have different extensions: Header files should use a .h extension (.h/.hpp/.hxx). Which particular one of these you use doesn’t matter. C source files should use .c (.c only). (But that’s for ‘C’ language.)
-
Class Methods & Separating Interface From Implementation–C++ Notes (C Plus Plus Notes)
Class Methods Methods, or member functions, are functions that belong to the class. There are two ways to define functions that belong to a class, or member functions, or methods. Inside the class definition Outside the class definition (including from a different file like a source file) To define a member function outside the class definition, you have to declare it inside the class (with a function prototype), and then define it outside of the class. This is done by specifying the name of the class, followed by the scope resolution operator ( :: ), followed by the name of the function. Separating Interface from Implementation *Note: It’s better software…
-
Classes & Objects–C++ Notes (C Plus Plus Notes)
C++ Classes & Objects Classes and Objects are the two main aspects of OOP. Ex: Class Objects Apple Fruit Banana Mango or: Class Object Volvo Car Audi Toyota A class is a template for objects, and an object is an instance of a class. When the individual objects are created they inherit all the variables and functions from the class. Everything in C++ is associated with classes & objects, along with its attributes (data members–such as “weight” & “color”), and methods (member functions–such as “drive” & “brake” for a “car”). Attributes & methods are basically variables & functions that belong to the class. These are often referred to as “class…
-
Computer Organization–Notes
Computer Organization Input Unit–the “receiving” section. It obtains info from input devices & places it at the disposal of the other units for processing. Output Unit–the “shipping” section takes information that the computer has processed & places it on various output devices to make it available for use outside the computer. (i.e.–displayed on screens, printed on paper, played on audio devices, or used to control other devices. Can also be output to networks, such as the Internet. Memory Unit–this rapid-access, relatively low-capacity “warehouse” section retains info that has been entered through the input unit, making it immediately available for processing when needed. Also retains processed info until it can be…