CSE131 – Computer Programming Dr. Yomna Safaa El-Din Book Big C++ Cay S. Horstmann, Timothy A. Budd, 2nd Edition, Wiley, 2009 Chapter 1 Chapter Goals • To understand the activity of programming • To learn about machine languages and higher-level programming languages • To become familiar with your compiler • To compile and run your first C++ program • To recognize syntax and logic errors What is Programming? • A program tells the computer the sequence of steps needed to fulfill a task. • A programmer designs and implements these programs. • Most computer users are not computer programmers. The Anatomy of a Computer (Schematic) • The motherboard holds the CPU, memory, and bus, as well as card slots which connect peripherals to the computer. • Peripherals allow the user and computer to interact: • Printer • Mouse • Keyboard • Modem • Some computers are connected to one another through networks. The Anatomy of a Computer (CPU, Memory, and bus) • CPU (Central Processing Unit): • Plastic, metal and mostly silicon. • Composed of several million transistors. • Enormously complicated wiring. • Performs program control, arithmetic, and data movement. • Locates and executes program instructions. • Memory/Storage: • RAM (Random Access Memory): read-write memory. • ROM (Read Only Memory): contains certain programs that must always be present. • Secondary storage (e.g. a hard drive) provides persistent storage. • The bus is a set of electrical lines that connect the CPU, RAM, and other devices. Translating Human-Readable Programs to Machine Code • Machine instructions: beda2y • Extremely primitive • Encoded as numbers: 161 40000 45 100 127 11280 • Tedious and error prone to look up numeric codes and enter them manually. • Each processor has its own set of machine instructions. that generates machine code • Assembler: does it make the object code? • Assigns short names to commands • mov 40000, %eax • sub 100, %eax • jg 11280 • Makes reading easier for humans. • Translated into machine instructions by another program. • Still processor dependent High-level Languages • Independent of the underlying hardware. • Easiest for humans to read and write. if (int_rate > 100) message_box("Interest rate error"); • Translated by compilers into machine instructions. • (a) Special-purpose languages: • created with a specific purpose, ex: database processing. • are not used much beyond their area of specialization. • (b) General purpose languages: • can be used for any task General purpose languages Can be used for any task. • Pascal: designed as a teaching language (purposefully kept simple). • C: developed to be translated efficiently into fast machine code, with a minimum of housekeeping. • C++: adds "object oriented programming" aspects to C. • C and C++ are portable languages • programs written in C and C++ can run on many different computers First C++ Code #include <iostream> // or: #include <iostream.h> using namespace std; int main() { cout << "Hello, World!\n"; return 0; } • C++ is case sensitive • C++ has freeform layout. Syntax (C++ has freeform layout) • C++ has freeform layout: • You can write the entire program on a single line, int main(){cout<<"Hello, World!\n";return 0;} • or write every keyword on a separate line int main() { cout << "Hello, World!\n" ; return 0; } • Good taste dictates that you use readable program layout. First C++ Code #include <iostream> // or: #include <iostream.h> using namespace std; int main() { cout << "Hello, World!\n"; return 0; } • C++ is case sensitive • C++ has freeform layout. First C++ Code Header files Comment #include <iostream> // or: #include <iostream.h> using namespace std; read the file iostream that contains the definition for the stream input/output package. all names in the program belong to the "standard namespace" int main() { cout << "Hello, World!\n"; return 0; } Statements ( executed one by one.) End with a semicolon newline String in “ ” denotes the end of the main function. The zero value is a signal that the program ran successf 1.19 A Simple Program: Printing a Line of Text • std::cout • standard output stream object • “connected” to the screen • we need the std:: to specify what "namespace" cout belongs to • we shall remove this prefix with using statements • << • stream insertion operator • value to the right of the operator (right operand) inserted into output stream (which is connected to the screen) std::cout << " Welcome to C++!\n" •\ • escape character • indicates that a “special” character is to be output 1.19 A Simple Program: Printing a Line of Text (II) There are multiple ways to print text • Following are more examples Escape Sequence Description \n Newline. Position the screen cursor to the beginning of the next line. \t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. \\ Backslash. Used to print a backslash character. \" Double quote. Used to print a double quote character. 1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements Outline 3 #include <iostream> 4 1. Load <iostream> 5 int main() 6 { 7 std::cout << "Welcome "; 8 std::cout << "to C++!\n"; 9 10 return 0; 2. main 2.1 Print "Welcome" // indicate that program ended successfully 2.2 Print "to C++!" 11 } 2.3 newline Welcome to C++! Unless new line '\n' is specified, the text continues on the same line. © 2000 Deitel & Associates, Inc. All rights 2.4 exit (return 0) 1 // Fig. 1.5: fig01_05.cpp Outline 2 // Printing multiple lines with a single statement 3 #include <iostream> 1. Load <iostream> 4 5 int main() 6 { 7 2. main std::cout << "Welcome\nto\n\nC++!\n"; 2.1 Print "Welcome" 8 9 return 0; // indicate that program ended successfully 10 } 2.2 newline 2.3 Print "to" 2.4 newline Welcome to 2.5 newline C++! Multiple lines can be printed with one statement 2.6 Print "C++!" 2.7 newline 2.8 exit (return 0) © 2000 Deitel & Associates, Inc. All rights Details of syntax • main() defines a function called main. • A function is a collection of programming instructions that carry out a particular task. Every C++ program must have a main function. • Most C++ programs contain other functions besides main, • The instructions or statements in the body of the main function—that is, the statements inside the curly braces {}—are executed one by one. • each statement ends in a semicolon. • A sequence of characters enclosed in quotation marks • "Hello, World!\n" • is called a string. You must enclose the contents of the string inside quotation marks so that the compiler knows you literally mean "Hello, World!\n". Errors • Syntax error or compile-time error: • Something violates the language rules. • Compiler finds the error. • cot << "Hello, World!\n"; • cout << "Hello, World!\"; • Logic error or run-time error: • The program is syntactically correct and does something, but it doesn't do what it's supposed to do. • Program author must test and find the error. • cout << "Hell, World\n"; Errors (Contd.) ely hwa eshta8al yabne w e3ml ignore lel errors delw2ty • Structuring programs so that an error in place does not trigger a disastrous response is called defensive programming. • A program error is commonly called a bug; special software tools called debuggers help you locate bugs. • Overflow and roundoff errors can occur when the result of a calculation is outside a computer's numeric range (i.e. either very big or very small). overflow error: when chosen wrong datatype for a certain number, example : long long x = 10^18; , will only work when using long long datatype, otherwise will give overflow error The Compilation Process (Diagram) it's what i write a form of code, hard to read but can be read, it's not as complex as machine code and not as simple as source code which is written by high level language links object code and libraries' header files like iostream, and other header files exe. file The Compilation Process (Diagram) a program that translate your C++ code into object code. The C++ source code is your program. consists of machine instructions and information on how to load the program into memory. and are not zereos and ones program takes the object code from your program and the code from the various libraries and builds them into an executable file. Libraries contain the (already translated) code used by your program (such as iostream). The Compilation Process