1 / 88

Introduction to High level Language Programming Chapter 7

Introduction to High level Language Programming Chapter 7. Why Do We Need Different Types of Automobiles?. Different people need to use their cars for different purposes. Some people need to transport things. Others need to transport people. Some are used on roads and some off road.

wanda
Download Presentation

Introduction to High level Language Programming Chapter 7

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. Introduction to High level Language ProgrammingChapter 7

  2. Why Do We Need Different Types of Automobiles? • Different people need to use their cars for different purposes. • Some people need to transport things. • Others need to transport people. • Some are used on roads and some off road. • Others need to impress members of the opposite sex.

  3. Different Programming Languages Provide for Different Programming Needs? • FORTRAN is designed for writing computationally-intensive programs (engineering applications) • COBOL (COmmon Business Orientated Language) is designed for handling transaction-processing applications which require extensive use of report generation (business applications) • C is a good language for writing operating system programs (UNIX) because of its ability to directly access and manipulate data in main memory

  4. Background • BCPL was developed by Martin Richards for writing OS software and compilers. • Ken Thompson developed B language to create early version of UNIX OS. • The C language was evolved from B and BCPL (1967) by Dennis Ritchie at Bell Lab.

  5. Background • Most operating systems are in written in C or C++. • Wide spread use of C for various types of computers created many variation of C.

  6. C • Many developers liked the C language but they developed their own variation of the language. • Programs written in traditional C were often not portable. That is, programs were hardware dependent: programs that ran on a mainframe wouldn’t run on a PC. (need for standards)

  7. ANSI • The ANSI (American National Standards Institute) developed guidelines, which was published in 1990, for how C compilers should translate code into machine language so that C programs would be hardware independent (portable).

  8. C++ • C++ is an extension of the C language; it was developed by Bjarne Stroustrup in the early 1980’s at Bell Labs. • C++ provides object-orientated capabilities to the language

  9. Objects • Objects in the real world: • People • Animals • Plants • Buildings • Computers

  10. Objects • Human think in terms of objects: • View screen images as objects( people, plants, mountains rather than pixels). • We can think in terms of beaches rather than grains of sands. • We can think in terms of houses rather than bricks.

  11. Objects • Objects can be categorized into: • animated • unanimated • View screen images as objects( people, plants, mountains rather than pixels). • We can think in terms of beaches rather than grains of sands. • We can think in terms of houses rather than bricks. • We can think in terms of airplane rather than aluminum sheet.

  12. Objects Common Attribute • Objects can be divided into different classes but they can have certain common attributes. • Shape • Color • Size • Weight

  13. Objects • We learn about objects by studying their attributes and and their behaviors. • Objects with similar attributes can exhibit similar behaviors. • Examples: • trucks and cars (class of automobiles) • dolphins and whales (class of sea animals) • cats and lions (class of wild animals) • babies and adults ( class of human being)

  14. Object-Oriented Programming(OOP) • Models real wold objects with software counterparts. • It takes advantages of class relationships where objects of the same class have the same characteristics. • It takes advantage of inheritance relationships. ( an object of class convertible has characteristics of an automobile as well as its own unique characteristics)

  15. Object-Oriented Programming(OOP) • Models real-wold objects, their attributes , and their behaviors. • OOP models communication between objects

  16. Object-Oriented Programming(OOP) • OOP encapsulates data (attributes) and functions (behavior) into packages called Objects.

  17. Object-Oriented Programming(OOP) • Let’s look at the major objects in the word processing system.

  18. Word processor Editor Spell checker Printer operations File operations Thesaurus Viewing a word processor as a collection of objects

  19. Editor Commands Documents Screen Identifying additional objects within word processor

  20. Spell checker dictionary Documents Commands Identifying additional objects within word processor

  21. Printer operations Printer Documents Commands Identifying additional objects within word processor

  22. File operations File Documents Commands Identifying additional objects within word processor

  23. File COPY DELETE RENAME Object name Object member functions Name size protection date stamp Identify the operations that an object can perform or the operations that can be performed on an object. (Object’s member functions) Identify the the information you must know about the object (Object’s member variables)

  24. Program Object name COPY DELETE RENAME Object member functions Name size protection date stamp Operating Sys. Identify the operations that an object can perform or the operations that can be performed on an object. (Object’s member functions) Identify the the information you must know about the object (Object’s member variables)

  25. File Object name Document COPY DELETE RENAME display print Program Name size protection date stamp form COPY DELETE RUN Name size protection date stamp Operating Sys. COPY DELETE RENAME Object member functions (Object’s member variables) Name size protection date stamp Stand-alone File, Document, and Program Classes

  26. File Object name COPY DELETE RENAME Object member functions Name size protection date stamp (Object’s member variables) Document Program display print RUN New Classes are based on existing file class Operating Sys. format

  27. C++ Uses Objects • An object is a reusable software component. • C++ stresses: • Reusability of code • More productive programmers • Code-writing is more structured, no more “spaghetti code”

  28. The Concepts Behind the Structured Programming • Writing clear, concise code (no “go to” statements) • Taking a modular approach (another benefit: reusable code) • Thorough documentation • Think first, program second

  29. The Overall Form of a Typical C++ PROGRAM Prologue comment (optional) Include directives (optional) Functions (optional) Main function { declarations (optional) main function body }

  30. C++ PROGRAMS GO THROUGH 6 PHASES TO BE EXECUTED:

  31. PHASE 1: Create Source Code • Use any text editor program to write the source code. • Save the source code in “plain text” . • The source code file must have the extension .cpp. • We use a Unix-based text editor program called “pico”

  32. PHASE 1: Create Source Code (continued) • Save the source code file • Save the .cpp file, and exits pico. • Ctrl-O  Ctrl-X • At the unix-prompt, type: g++ -o <output file name> <input file name> Phases 2, 3, and 4 are carried out

  33. PHASE 2: Execute Preprocessor Directives (code libraries) • Aany preprocessor directives (PPDs) that appear in the programmer’s source code gets executed. • Example: #include <iostream.h> requests that the pre-processor should “fetch” the file iostream.h from /usr/include and import its entire contents into your program’s source code. • Example: #define PI 3.14159 requests that the pre-processor scan through your code and replace all occurrence of PI with the constant value 3.14159.

  34. PHASE 3: Compile • The compiler translates each line of the source code file into one or more machine language instructions. • The machine language version is called the object file.

  35. PHASE 4: Link • The linker scans through your object code looking for calls to externally-defined functions (sub-routines) whose code (definitions) reside in external libraries. • The linker will either embed the external code into your program OR it may create a link (i.e. pointer) to the external code. (Linking eliminates the need for duplication but embedding makes the finished program more portable.)

  36. The End Result: • The end result is an executable file. (In DOS or Windows, that would be called an “.exe file”)

  37. PHASE 5: Load The .Exe File Into RAM (Main) Memory • When the user types the output file name at the UNIX command-prompt, the executable file is loaded into memory by the loader.

  38. PHASE 6: Execute • The program is executed, or carried out, one instruction at a time.

  39. Getting started 1. Login to your odin account using your user id and password. 2. Create the source code file using a text editor named “pico”. At the odin-prompt, type: pico <filename.cpp>  Example: pico first.cpp  3. After typing the program, save it and exit by selecting: ctrl-o, , ctrl-x 4. Compile it by entering the following command at the odin-prompt: g++ -o <executable file name> <source code file name>  Example: g++ -o first first.cpp 

  40. Error Types • Syntax error • your program doesn’t follow the rules of the language • Your program will not compile • Logical error • your program doesn’t do what it is supposed to do • Your program will compile but it will not provide the desired output

  41. Fixing Syntax errors: Use the text editor program to revise your source code: pico first.cpp  Re-save your program: Ctrl-o,  ctrl-x Re-compile: g++ -o first first.cpp  Run your program, type the executable file name at the odin-prompt: first 

  42. Some Conventions Used in C++ and Suggestions • Identifiers are names in a programming language. • Identifiers can be any combination of letters, digits and ( _ ) as long as it does not begin with a digit. • Identifiers can not be any of keywords. • Use lower case letters (not a requirement)for identifiers. • C++ is case sensitive, ITM250 is different from iTM250 • Identifiers should be descriptive of what they represent (31 characters).

  43. Types of DATA • Constants • Variables • ( int ) a positive or negative quantity • double a real number • character • A declaration statement: • Can specifies whether the data item is a constant or variable. • Tells the identifiers that will be used to name that item. • Tells the data type for that item.

  44. SAMPLE PROGRAM #1 // first.cpp - Your name - Current Date #include<iostream.h> int main(void) { cout << “Welcome to C++! \n"; return 0; }

  45. EXPLANATION . . .

  46. COMMENTS: • To add comments: • proceed the comment with 2 slashes • // comment • OR • /* This is a comment! */ • The compiler will ignore anything that is a comment.

  47. #include<iostream.h> • Statements at the beginning of a program that are preceded by a # are called preprocessor instructions. • “#include” has the effect of saying "include the entire contents of the file iostream.h right here". • Any program that uses “cout” or “cin” must #include <iostream.h> so the compiler will “know” that cout and cin are valid “tokens”.

  48. int main(void) • C++-programs are made up of one or more functions. • One of theses function must be main. • ”int" means that the function "returns" an integer; “main” is the function’s name; "(void)" means that there will be no arguments passed to this function. • Immediately following this declaration is a { to indicate the beginning of the main function. • A } indicates the end of the main function.

  49. cout << “Welcome to C++”; • COUT, used to display text on the screen. • A cout statement is used to send a stream of characters to the standard output stream object (who, in turn, passes the string on to the display device.) • << is called the “stream insertion operator”. • In this example, cout gets the character string “Welcome to C++” • All statements in C++ must end with a semi-colon.

  50. \n • \n is an “escape sequence”which means “press enter” • escape sequences may be embedded in character streams to indicate non-printing characters such as “tab”, “return” and “enter”. • When you want to display an “a” on the screen, you program: • cout<< “a”; • when you want the cursor to move to the start of the next line, program: • cout<< “\n”; • or • cout<< endl;

More Related