430 likes | 612 Views
Introduction to C++ Computer Programming. DON'T FIGHT WITH YOUR COMPUTER, LEARN HOW TO PROGRAM IT!!. Natural Language vs. Programming Language. Don’t get Frustrated. What learning a Programming language can do. Computer Systems -- Introduction.
E N D
Introduction to C++Computer Programming DON'T FIGHT WITH YOUR COMPUTER, LEARN HOW TO PROGRAM IT!! Introduction to C++
Natural Language vs. Programming Language Introduction to C++
Don’t get Frustrated Introduction to C++
What learning a Programming language can do Introduction to C++
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 Introduction to C++
What is a computer ? • A device capable of performing computations and logical operations million and billions times faster than the human brain • Super computer – 100s 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. 0s and 1s • Humans take a long time to understand 0s and 1s unlike computers Introduction to C++
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 and the market is growing every year • A computer requires both hardware and software • Each is essentially useless without the other Introduction to C++
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. Introduction to C++
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 Introduction to C++
System Hardware Interaction Hard Disk Keyboard Main Memory RAM Monitor CPU & ALU Floppy Disk Introduction to C++
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 Introduction to C++
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 Introduction to C++
Memory address 9278 9279 9280 9281 9282 9283 9284 9285 9286 large values are stored in consecutive memory locations Introduction to C++
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) Introduction to C++
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 Introduction to C++
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 fetchdecodeexecute cycle • The CPU contains: • control unit coordinates processing steps • registers small storage areas • arithmetic / logic unit performs calculations and decisions Introduction to C++
Central Processing Unit CPU Arithmetic/Logic Unit Control Unit Main Memory Registers Introduction to C++
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 Introduction to C++
Five Generations of Programming Languages • First Generation Languages0100011101010011101001010 – machine language = instruction set • Second Generation LanguagesLDA #10 – assembly language = machine language ADC # 20 – mnemonic / opcode CMP #30 BNE error RTS • Third Generation Languagesif (10+20 !=30) { – Fortran -> COBOL -> Algol error(); – Algol --> C, C++… JAVA } return; High-Level Languages Introduction to C++
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? Introduction to C++
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 Introduction to C++
Four Programming Paradigms • Functional Programming • Programming with Mathematics • Functions passed as data • Declarative Programming - WHAT to do? • Abstract formalism - Facts and rules Introduction to C++
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) Introduction to C++
Syntax vs. Semantics • Syntax Semantics • Grammar of the program Meaning of the program • symbols role of program statement • reserved words purpose • identifiers 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 Introduction to C++
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 Introduction to C++
An algorithm to come to work Get out of bed get out of bed Brush your teeth brush your teeth Take a shower get dressed Get dressed take a shower Eat breakfast eat breakfast Carpool to work carpool to work Which set of instructions defines a correct algorithm? Introduction to C++
Object Code File.cpp Compiler Linker Source Code C++ libraries Executable file Compiling and Linking C++ Programs • Source code files • Compiler - machine code files • C++ predefined functions • Linker - combines object code files and produces executable Introduction to C++
Origins of C++ • BCPL --> B --> C --> C++ • Why ++? • increment operator • C programming language • Dennis Ritchie (AT&T Bell Laboratories) 1970s 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 and 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 Introduction to C++
C++ Origins • C++ • created by Bjarne Stroustrup (AT&T Bell Laboratories) early 1980s • designed as a better • C is mostly a subset of C++ now • C with objects • has facilities to do object-oriented programming Introduction to C++
A First C++ Program #include <iostream.h> int main() { int number_of_pods, peas_per_pod, total_peas; cout << “Press return after entering a number.\n”; cout << “Enter the number of pods: \n”; cin >> number_of_pods; cout << “Enter the number of peas in a pod:\n”; cin >> peas_per_pod; total_peas = number_of_pods * peas_per_pod; cout << “If you have \n”; cout << number_of_pods; cout << “ and “; cout << peas_per_pod; cout << “ peas in each pod, then\n”; cout << “ you have “; cout << total_peas; cout << “ peas in all the pods. \n”; return 0; } Introduction to C++
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? Introduction to C++
A First C++ Program - basic definitions #include <iostream> using namespace std; • directive • always begin with # • tells the compiler where to find details about items that are used in • your program (cout << and cin >>) • linker combines the object code associated with the methods in • iostream with the object code corresponding to your source code • PITFALL - do not put any extra spaces between the < >, the compiler will not recognize the name of the file Introduction to C++
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. Introduction to C++
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. Introduction to C++
Basic C++ Program Layout #include <something.h> int main() { Variable_Declarations statement 1 statement 2 … return 0; } Introduction to C++
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. Introduction to C++
Three General Categories of Errors • Compilation-time Errors • Run-time Errors • Logical Errors Introduction to C++
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 Introduction to C++
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); Introduction to C++
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!!!) Introduction to C++
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 Introduction to C++