1 / 41

Programming 1

Programming 1. Overview of C++ Language. Objectives. A typical C++ program-development environment. Study variables How to declare How to use Become familiar with the basic components of a C++ program, including identifiers. Typical C++ Development Environment.

rory
Download Presentation

Programming 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming 1 Overview of C++ Language

  2. Objectives • A typical C++ program-development environment. • Study variables • How to declare • How to use • Become familiar with the basic components of a C++ program, including identifiers

  3. Typical C++ Development Environment • C++ systemsgenerally consist of three parts: a program development environment, the language and the C++ Standard Library. • C++ programs typically go through six phases: edit, preprocess, compile, link, load and execute.

  4. Typical C++ Development Environment (Cont.) • Phase 1 consists of editing a file with an editor program, normally known simply as an editor. • Type a C++ program (source code) using the editor. • Make any necessary corrections. • Save the program. • C++ source code filenames often end with the .cpp, .cxx, .cc or .C extensions (note that C is in uppercase) which indicate that a file contains C++ source code.

  5. Typical C++ Development Environment (Cont.) • Linux editors: vi and emacs. • C++ software packages for Microsoft Windows such as Microsoft Visual C++ (microsoft.com/express) have editors integrated into the programming environment. • You can also use a simple text editor, such as Notepad in Windows, to write your C++ code. • integrated development environments (IDEs) • Provide tools that support the software-development process, including editors for writing and editing programs and debuggers for locating logic errors-errors that cause programs to execute incorrectly.

  6. Typical C++ Development Environment (Cont.) • Popular IDEs • Microsoft® Visual Studio 2010 Express Edition • Dev C++ • NetBeans • Eclipse • CodeLite

  7. Typical C++ Development Environment (Cont.) • In phase 2, you give the command to compile the program. • A preprocessor program executes automatically before the compiler’s translation phase begins (so we call preprocessing Phase 2 and compiling Phase 3). • The C++ preprocessor obeys commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. • These manipulations usually include other text files to be compiled, and perform various text replacements. • The most common preprocessor directives are discussed in the early chapters; a detailed discussion of preprocessor features appears in Appendix E, Preprocessor.

  8. Typical C++ Development Environment (Cont.) • In Phase 3, the compiler translates the C++ program into machine-language code-also referred to as object code.

  9. Typical C++ Development Environment (Cont.) • Phase 4 is called linking. • The object code produced by the C++ compiler typically contains “holes” due to these missing parts. • A linker links the object code with the code for the missing functions to produce an executable program. • If the program compiles and links correctly, an executable image is produced.

  10. Typical C++ Development Environment (Cont.) • Phase 5 is called loading. • Before a program can be executed, it must first be placed in memory. • This is done by the loader, which takes the executable image from disk and transfers it to memory. • Additional components from shared libraries that support the program are also loaded.

  11. Typical C++ Development Environment (Cont.) • Phase 6: Execution • Finally, the computer, under the control of its CPU, executes the program one instruction at a time. • Some modern computer architectures can execute several instructions in parallel.

  12. Typical C++ Development Environment (Cont.) • Problems That May Occur at Execution Time • Programs might not work on the first try. • Each of the preceding phases can fail because of various errors that we’ll discuss throughout this book. • If this occurred, you’d have to return to the edit phase, make the necessary corrections and proceed through the remaining phases again to determine that the corrections fixed the problem(s). • Most programs in C++ input or output data.

  13. Typical C++ Development Environment (Cont.) • Certain C++ functions take their input from cin (the standard input stream; pronounced “see-in”), which is normally the keyboard, but cin can be redirected to another device. • Data is often output to cout (the standard output stream; pronounced “see-out”), which is normally the computer screen, but cout can be redirected to another device. • When we say that a program prints a result, we normally mean that the result is displayed on a screen.

  14. Typical C++ Development Environment (Cont.) • Data may be output to other devices, such as disks and hardcopy printers. • There is also a standard error stream referred to as cerr. The cerr stream is used for displaying error messages.

  15. C++ Language Elements • Comments • Compiler directives • Function main • Declaration statements • Executable statements

  16. Comments • // symbols indicate a line comment – apply to just the rest of the line • Block comments start with /* and end with */ - apply to as many lines as you like • Used to describe the code in English or provide non-code information • E.g. to include the name of the program or the author’s name

  17. Converting miles to kilometers

  18. #include <filename> • Compiler directive • Includes previously written code from a library into your program • E.g. #include <iostream> has operators for performing input and output within the program • Libraries allow for code reuse

  19. using namespace std; • Indicates to compiler that this program uses objects defined by a standard namespace called std. • Ends with a semicolon • Follows #include directives in the code • Must appear in all programs

  20. Function main int main ( ) { // function body }

  21. Function main • Exactly one main function per program • A function is a collection of related statements that perform a specific operation • int indicates the return type of the function • ( ) indicates no special information passed to the function by the operating system

  22. Types of Statements • Declaration statements – describe the data the function needs: const float KM_PER_MILE = 1.609; float miles, kms; • Executable statements – specify the actions the program will take: cout << “Enter the distance in miles: ”; cin >> miles;

  23. General Form of a C++ Program

  24. Variables • Give a name to a memory location • Compiler accesses specific memory location when program uses a given variable • Refer to objects in the program for which the value can change • Declaration type variableName; // or type variableName = initializerExpression;

  25. Variables • Variables Declaration • Can be either initialized or uninitialized… • If variable is uninitialized Contents must be considered as “garbage value” • Examples: int age = 18; double GPA = 3.25, credits; char letterGrade = ‘A’; bool ok, done = false;

  26. Data Types • Defines a set of values and operations that can be performed on those values • integers • positive and negative whole numbers, e.g. 5, -52, 343222 • short, int, long • represented internally in binary • predefined constants in INT_MIN and INT_MAX

  27. Data Types (con’t) • Floating point (real) • number has two parts, integral and fractional • e.g. 2.5, 3.66666666, -0.000034, 5.0 • float, double, long double • stored internally in binary as mantissa and exponent • 10.0 and 10 are stored differently in memory

  28. Data Types (con’t) • Boolean • named for George Boole • represent conditional values • Return: true and false

  29. Data Types (con’t) • Characters • represent individual character values E.g. ’A’ ’a’ ’2’ ’*’ ’”’ ’’ • stored in 1 byte of memory • special characters: escape sequences E.g. ’\n’ ’\b’ ’\r’ ’\t’ ‘\’’

  30. string Class • Strings not built-in, but come from library • Classes extend C++ • string literal enclosed in double quotes E.g.: “Enter speed:“ “ABC” “B” “true” “1234” • #include <string> • for using string identifiers, but not needed for literals

  31. Identifiers • Consist of letters, digits, and the underscore character (_) • Must begin with a letter or underscore • C++ is case sensitive • NUMBER is not the same as number • Two predefined identifiers are cout and cin • Unlike reserved words, predefined identifiers may be redefined, but it is not a good idea

  32. Identifiers (continued) • The following are legal identifiers in C++: • first • conversion • payRate

  33. Summary • Identifier consists of letters, digits and underscores, and begins with letter or underscore

  34. Bibliography • Adams, J. & Nyhoff, L. (2003). C++ An Introduction to Computing, 3rd ed. Pearson Education, New Jersey. • Deitel, H.M & Deitel, A.S (2012). C++ How to Program, 8th ed. Pearson Education, London. • Friedman, F.L & Koffman, E.B (2011). Problem Solving, Abstraction, and Design Using C++, 6th International ed. Pearson Education, Massachusetts. • Malik, D.S. (2009). Introduction to C++ Programming, Brief International ed. Course Technology, Massachusetts.

More Related