1a770b395323b896b7ccd828c1076c3c.ppt
- Количество слайдов: 41
Introduction to C++ Computer Programming DON'T FIGHT WITH YOUR COMPUTER, LEARN HOW TO PROGRAM IT!! Amar Raheja Introduction to C++ 1
Natural Language vs. Programming Language Amar Raheja Introduction to C++ 2
Don’t get Frustrated Amar Raheja Introduction to C++ 3
What learning a Programming language can do Amar Raheja Introduction to C++ 4
Computer Systems Introduction • Before we can dive into programming, we need to fundamentals of computers in general – Define a computer – Possible applications of a computer • components of a computer • how those components interact • how computers store and process information Amar Raheja Introduction to C++ 5
What is a computer ? • A device capable of performing computations and logical operations million and billions times faster than the human brain • Super computer – 100 s of billions of computations per second ~ 1011 computations /sec • Computers process data using an organized set of instructions called computer programs • Computers only understand binary language i. e. 0 s and 1 s • Humans take a long time to understand 0 s and 1 s unlike computers Amar Raheja Introduction to C++ 6
Hardware and Software • Hardware – the physical, tangible parts of a computer – various devices such as keyboard, screen (monitor), disks, memory (RAM), motherboard, CPU, mouse etc. – Cost of hardware has been reducing over the years • Software – programs (and data) that perform certain tasks like Microsoft Word, Excel, Access, Doom, Calculator, Internet Explorer etc. – Software programmers are in great demand the market is growing every year • A computer requires both hardware and software • Each is essentially useless without the other Amar Raheja Introduction to C++ 7
Computer System Hardware • Every computer can be divided into six logical units – Input Unit : receives information from various hardware devices and makes the info available to other devices inside the computer. e. g. keyboard, scanner, microphone, camera etc. – Output Unit : takes info that has been processed by the computer and places it on various output devices. e. g. Monitor(screen), printer, speakers etc. – Main Memory Unit (RAM) : A rapidly accessible unit where the info is placed when it comes in from the input or is to be shipped out to the output device. Amar Raheja Introduction to C++ 8
Computer System Hardware • Arithmetic & logical unit (ALU) : responsible for performing all calculations such as +, , *, / etc. also referred to as the heart of a computer • Central Processing Unit (CPU) : It is the coordinator and supervisor of the whole computer. It executes all program commands and is the brain of a computer. e. g. Intel Pentium chip, Sparc processor etc. • Secondary memory unit: long term high capacity storage unit, also called the hard drive. Programs and data stored on this device stays intact even when the power is turned off unlike the main memory unit which gets erased when power is turned off Amar Raheja Introduction to C++ 9
System Hardware Interaction Hard Disk Keyboard Main Memory RAM Monitor Floppy Disk Amar Raheja CPU & ALU Introduction to C++ 10
Software Categories • Operating system – – controls all machine activities provides the user interface to the computer manages resources such as the CPU and memory Windows ME, Solaris (UNIX), Mac OS • Application Program – generic term for any other kind of software – word processors, databases, games – also provides GUI Amar Raheja Introduction to C++ 11
Memory • Main Memory is mostly referred to as RAM (read only memory) • Main memory is divided into many memory locations • Each memory location has an address which uniquely identifies it • Data is stored in one or more consecutive memory locations • On most computers, each memory location holds 8 bits, or 1 byte Amar Raheja Introduction to C++ 12
Memory address 9278 9279 9280 9281 9282 9283 9284 9285 9286 Amar Raheja large values are stored in consecutive memory locations Introduction to C++ 13
Storage Capacity • Each memory device has a storage capacity, indicating the number of bytes it can hold • Capacities are expressed in various units of binary storage: Unit Symbol Number of Bytes kilobyte KB 210 = 1024 megabyte MB 220 (over 1 million) gigabyte GB 230 (over 1 billion) terabyte TB 240 (over 1 trillion) Amar Raheja Introduction to C++ 14
Memory • Main memory is volatile stored information is lost if the electric power is removed • Secondary memory devices are nonvolatile • Main memory and disks are random access devices, which mean that information can be reached directly • A magnetic tape is a sequential access device since its data is arranged in a linear order you must get by the intervening data in order to access other information Amar Raheja Introduction to C++ 15
Central Processing Unit (CPU) • A CPU is also called a microprocessor • It retrieves, interprets, and executes instructions, one after another, continuously • This process is called the fetch decode execute cycle • The CPU contains: – control unit coordinates processing steps – registers small storage areas – arithmetic / logic unit performs calculations and decisions Amar Raheja Introduction to C++ 16
Central Processing Unit CPU Arithmetic/Logic Unit Control Unit Main Memory Registers Amar Raheja Introduction to C++ 17
Computer Programming • Telling a computer what to do – writing a program in a certain language like C, C++ or Fortran etc. • The program is a set of instructions written in english using certain keywords • Given an input, the computer program produces an output • Input: Typed letters & symbols, saved document (file) • Output: Display, printed document, document file • A program is an implementation of an algorithm in a particular programming language Amar Raheja Introduction to C++ 18
Five Generations of Programming Languages • First Generation Languages 0100011101010011101001010 – machine language = instruction set • Second Generation Languages LDA #10 – assembly language = machine language ADC # 20 – mnemonic / opcode CMP #30 BNE error RTS • Third Generation Languages – Fortran > COBOL > – Algol > C, C++… JAVA if (10+20 !=30) { Algol error(); } return; High-Level Languages Amar Raheja Introduction to C++ 19
Five Generations of Programming Languages • Fourth and Fifth Generation Languages – Not based on other generations. – Fourth = application based – Fifth = declarative paradigm based • What is a Programming Paradigm? Amar Raheja Introduction to C++ 20
Four Programming Paradigms (not a programming language, a method or style of programming) • Imperative Programming HOW to do something? – Sequential – Machine languages, assembly languages, Basic, and C – Large programs are hard to maintain • Object Oriented Programming – Data abstraction. A vehicle is made up of wheels, seats, an engine. – Class > Object > instance (with initialization parameters) – Small Talk, C++, and JAVA Amar Raheja Introduction to C++ 21
Four Programming Paradigms • Functional Programming – Programming with Mathematics – Functions passed as data • Declarative Programming WHAT to do? – Abstract formalism Facts and rules Amar Raheja Introduction to C++ 22
Computer Languages • A language through which abstract instructions are communicated to the computer • Translated to the series of 0's and 1's that do the job • Human readable vs. machine readable • Easier to write , easier to read , easier to edit • A different language, with familiar words – Vocabulary (C has 32 "keywords" and a few symbols) – Syntax (Rules for combinations of words & symbols) – Semantics (What those combinations will do/mean) Amar Raheja Introduction to C++ 23
Syntax vs. Semantics • Syntax Semantics – Grammar of the program Meaning of the program • symbols • reserved words • identifiers role of program statement purpose unambiguos * Just because the syntax is correct doesn’t mean the semantics are correct * Programs will always do what we tell it to do, not what we mean it to do Amar Raheja Introduction to C++ 24
Algorithm • A sequence of precise instructions that leads to a solution • Must be: 1. Clearly and unambiguously defined 2. Effective 3. Finite • It’s a procedure for solving problems in terms of – The action to be executed and – The order in which these actions are to be executed Amar Raheja Introduction to C++ 25
An algorithm to come to work Get out of bed Brush your teeth Take a shower Get dressed Eat breakfast Carpool to work get out of bed brush your teeth get dressed take a shower eat breakfast carpool to work Which set of instructions defines a correct algorithm? Amar Raheja Introduction to C++ 26
Compiling and Linking C++ Programs File. cpp Compiler Object Code Linker Source Code C++ libraries • • Executable file Source code files Compiler machine code files C++ predefined functions Linker combines object code files and produces executable Amar Raheja Introduction to C++ 27
Origins of C++ • BCPL > B > C++ • Why ++? – increment operator • C programming language – Dennis Ritchie (AT&T Bell Laboratories) 1970 s writing and maintaining the UNIX operating system (assembly/B) – Ken Thompson, originator UNIX, and developer of B between high/low level languages, directly manipulate memory like assembly language – C not as easy to understand doesn’t have as many automatic checks as other high level languages – C has both high level and low level features which makes it a unique language Amar Raheja Introduction to C++ 28
C++ Origins • C++ – created by Bjarne Stroustrup (AT&T Bell Laboratories) early 1980 s – designed as a better – C is mostly a subset of C++ now – C with objects – has facilities to do object oriented programming Amar Raheja Introduction to C++ 29
A First C++ Program #include
A First C++ Program the output Press return after entering a number. Enter the number of pods: 10 Enter the number of peas in a pod: 9 If you have 10 pea pods and 9 peas in each pod, then you have 90 peas in all the pods. What does the source code mean? Amar Raheja Introduction to C++ 31
A First C++ Program - basic definitions #include
A First C++ Program - basic definitions • int main() { indicates the beginning of the program called the main method / function • return 0; } indicates the end of the program • { } always indicate the beginning and end of the main part of the program • return 0; is a return statement. It indicates that the program should end here. Amar Raheja Introduction to C++ 33
A First C++ Program - basic definitions • int number_of_pods, peas_per_pod, total_peas; – variable declaration – utilizes primitive data type of int, which is the set of all integers • total_peas = number_of_pods * peas_per_pod; – * is used for multiplication in C++ • cin and cout are input/output statements – – – defined in iostream. h cout << … sends information to the computer monitor cin >> … sends information to a variable TIP cin = input device >> variable cout = output << … n in a cout statement is called an escape sequence. It means that after sending information to the monitor skip to the next line. Amar Raheja Introduction to C++ 34
Basic C++ Program Layout #include
Program Testing • A crucial part of any software development effort • Lack of testing can result in big problems – Computerized stock trading program – Heart rate monitoring equipment • Cannot be an afterthought • Just because a program compiles doesn’t make it correct • Most professional software is tested against a test suite Testing is more than running a few chosen cases, it involves the intelligent selection of test cases that takes into account good knowledge of the application and the software solution. Amar Raheja Introduction to C++ 36
Three General Categories of Errors • Compilation time Errors • Run time Errors • Logical Errors Amar Raheja Introduction to C++ 37
Categories of Errors - continued • Compile Time errors – Syntactic errors • y == 5 // error incorrect equality operator syntactic error – Semantic errors • int j; j++; // error, variable not initialized – forgetting to declare a variable (instance variables for example) – assigning a string to an int (doesn’t make sense) – semantic errors are sometimes ambiguous • int i = “Hello World”; – sometimes receive multiple errors, correct the first one and try again Amar Raheja Introduction to C++ 38
Categories of Errors - continued • Run Time Errors errors that could not be detected at the time of compilation – int w, y; • y = 0; • w = 100 / y; // error, what is w? • Run time errors are harder to detect when compared to syntactic errors – int w, x, y, z; x =. . . y =. . . z =. . . w = 100 / ((x%y) * z); Amar Raheja Introduction to C++ 39
Categories of Errors - continued • Logical Errors errors that result by not correctly implementing algorithm/software requirements – mistakenly use the division sign instead of the multiplication sign – the program runs but gives an incorrect result – hardest of the three to find and correct * The process of finding Run time and Logical errors is called debugging – the less time you spend in debugging, the better programmer you are (not necessarily!!!) Amar Raheja Introduction to C++ 40
Professional Computing Societies • Association for Computing Machinery (ACM) • Computer Society of the IEEE – Institute for Electrical and Electronic Engineers • Model Undergraduate Program in Computer Science – a) Nine subject areas – b) Social, Ethical, and Professional Issues • Student Memberships – www. acm. org www. ieee. org Amar Raheja Introduction to C++ 41