1 / 35

Chapter 1: Introduction

Chapter 1: Introduction. C++ Programming. CS 241. Course URL: http://cs241.yolasite.com / Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition or seventh Edition C++ Without Fear A Biggener's Guide That Makes You Feel Smart, Brian Overland. GRADES. Grading

harvey
Download Presentation

Chapter 1: Introduction

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. Chapter 1: Introduction C++ Programming

  2. CS 241 Course URL: • http://cs241.yolasite.com/ Text Book: • C++ How to Program, DETITEL & DEITEL, eighth Edition or seventh Edition • C++ Without Fear A Biggener's Guide That Makes You Feel Smart, Brian Overland

  3. GRADES Grading • First Midterm: 10 • Second Midterm:15 • Lab quize:5 • Homework:5 • Participation and Evaluations:5 • Final: 40 • Final Lab: 20

  4. Course Syllabus

  5. What is a Computer? Computer • is a device that can perform computations and make logical decisions billions of times faster than human beings can. Computer programs • Sets of instructions that control computer’s processing of data Hardware • Various devices comprising computer • Keyboard, screen, mouse, disks, memory, CD-ROM, processing units, Software • Programs that run on computer *Computers (often referred to as hardware) are controlled by software

  6. Programming Languages Computer languages may be divided into three general types: • Machine languages • Assembly languages • High-level languages

  7. Programming Languages 1- Machine language: • Only language computer directly understands • The “natural language” of a computer and as such is defined by its hard-ware de-sign • Too slow (for development), consist of strings of numbers 1s and 0s. • Machine language is often referred to as object code.

  8. Programming Languages 2- Assembly language • English-like abbreviations that represent elementary operations • Clearer to humans • Translator programs called assemblers convert assembly-language programs to machine language. • Programmers still had to use many instructions to accomplish even the simplest tasks.

  9. Programming Languages 3- High-level languages • Similar to everyday English • Single statements accomplish substantial tasks • Translator programs called compilers convert high-level language programs into machine language

  10. Terms Source code • is a program in a form suitable for reading and writing by a human being Executable program (executable) • is a program in a form suitable for running on a computer Compilation • is the process of translating source code into object code Compiler • is a program that performs compilation as defined above The object code file • contains a sequence of instructions that the processor can understand but that is difficult for a human to read or modify.

  11. Typical C++ Development Environment C++ systems generally 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 • execute

  12. Program is created in the editor and stored on disk. Preprocessor program processes the code. Compiler creates object code and stores it on disk. Compiler Linker links the object code with the libraries, creates a.out and stores it on disk Primary Memory Loader Loader puts program in memory. Primary Memory CPU takes each instruction and executes it, possibly storing new data values as the program executes. Preprocessor Linker Editor Disk Disk Disk Disk Disk CPU . . . . . . . . . . . .

  13. More details … Edit • Programmer writes program (and stores source code on disk) Pre-process • Perform certain manipulations before compilation Compile • the compiler translates the C++ program into machine-language code (also referred to as object code).

  14. More details … Link • The object code produced by the C++ compiler typically contains “holes” due to missing parts, such as references to functions from standard libraries. • A linker links the object code with the code for the missing functions to produce an executable program Load • 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 Execute • the computer, under the control of its CPU, executes the program one instruction at a time

  15. First Program in C++: Printing a Line of Text

  16. First Program in C++: Printing a Line of Text (cont.) Comments • Document programs • Improve program readability • Ignored by compiler • Single-line comment • Begin with // • Multiple-line comment • Begin with /* end with */

  17. First Program in C++: Printing a Line of Text (cont.) preprocessor directive • Lines that begin with # are processed by the preprocessor before the program is compiled. • #include <iostream> notifies the preprocessor to include in the program the contents of the input/output stream • Must be included for any program that outputs data to the screen or inputs data from the keyboard

  18. Standard Library Rich collections of existing code that can be reused inyour applications • Common math calculations e.g. sqrt • String manipulations • Character manipulations • Input/output • Error checking • * Provided as part of the C++ development environment

  19. iostreamlibrary • part of the C++ Standard Library • provides a uniform way of handling input from (and output to)predefined sources • based on the concept of a "stream “ which is an object where a program can either insert or extract characters to or from it. • Streams are generally associated to a physical source or destination of characters • a disk file, the keyboard, or the screen

  20. iostream library • Standard Input Stream (cin) - Normally keyboard • Standard Output Stream (cout)- Normally computer screen • Standard Error Stream (cerr) - Display error messages

  21. First Program in C++: Printing a Line of Text (cont.) • main is a part of every C++ program. • The parentheses after main indicate that main is a program building block called a function. • C++ programs typically consist of one or more functions and classes. • Exactly one function in every program must be named main. • C++ programs begin executing at function main, even if main is not the first function in the program. • The keyword int to the left of main indicates that main “returns” an integer value. • A keyword is a word in code that is reserved by C++ for a specific use. • For now, simply include the keyword int to the left of main in each of your programs.

  22. First Program in C++: Printing a Line of Text (cont.) • A left brace, {, must begin the body of every function. • A corresponding right brace, }, must end each function’s body. • A statement normally ends with a semicolon (;), also known as the statement terminator. • Preprocessor directives (like #include) do not end with a semicolon.

  23. First Program in C++: Printing a Line of Text (cont.) • When a cout statement executes, it sends a stream of characters to the standard output stream object—std::cout—which is normally “connected” to the screen. • The std:: before cout is required when we use names that we’ve brought into the program by the preprocessor directive #include <iostream>. • The notation std::cout specifies that we are using a name, in this case cout, that belongs to “namespace” std. • The names cin (the standard input stream) and cerr (the standard error stream) also belong to namespace std. • The << operator is referred to as the stream insertion operator

  24. First Program in C++: Printing a Line of Text (cont.) • The characters \n are not printed on the screen. • The escape sequence \n means newline. • Causes the cursor to move to the beginning of the next line on the screen. • When the return statement is used at the end of main the value 0 indicates that the program has terminated successfully. • According to the C++ standard, if program execution reaches the end of main without encountering a return statement, it’s assumed that the program terminated successfully—exactly as when the last statement in main is a return statement with the value 0.

  25. Modifying Our First C++ Program

  26. Modifying Our First C++ Program

  27. Variables Location in memory where value can be stored Common data types • int - integer numbers • char - characters • double - floating point numbers Declare variables with name and data type before use int integer1; int integer2; int sum; Can declare several variables of same type in one declaration • Comma-separated list int integer1, integer2, sum;

  28. Variables Variable names • Valid identifier • Series of characters (letters, digits, underscores) • Cannot begin with digit • Case sensitive

  29. Another Simple Program:Adding Two Integers

  30. Adding Two Integers

  31. Adding Two Integers Declarations of variables can be placed almost anywhere in a program, but they must appear before their corresponding variables are used in the program • Input stream object • >> (stream extraction operator) • Used with std::cin • Waits for user to input value, then press Enter (Return) key • Stores value in variable to right of operator • Converts value to variable data type • = (assignment operator) • Assigns value to variable • Binary operator (two operands) • Example: sum = variable1 + variable2;

  32. Adding Two Integers • std::endl is a so-called stream manipulator. • The name endl is an abbreviation for “end line” and belongs to namespace std. • The std::endl stream manipulator outputs a newline. • Using multiple stream insertion operators (<<) in a single statement is referred to as concatenating, chaining or cascading stream insertion operations.

  33. Memory Concepts • Variable names such as number1, number2 and sum actually correspond to locations in the computer’s memory. • Every variable has name, type, size and value • When new value placed into variable, overwrites previous value • The process of reading variables from memory is called nondestructive • Placing new value into variable (memory location), overwrites old value- called destructive.

  34. Memory Concepts std::cin >> integer1; • Assume user entered 45 std::cin >> integer2; • Assume user entered 72 sum = integer1 + integer2;

More Related