Full Transcript

C++ Programming 1 Computer Fundamentals & C++ Programming Coverage: 1.1 Programming a Computer 1.2 Program Development Life Cycle 1.3 Machine Languages 1.4 History of C and C++ 1.5 C/C++ St...

C++ Programming 1 Computer Fundamentals & C++ Programming Coverage: 1.1 Programming a Computer 1.2 Program Development Life Cycle 1.3 Machine Languages 1.4 History of C and C++ 1.5 C/C++ Standard Library 1.6 Basics of a Typical C++ Environment 1.7 Creating a C/C++ Program 1.8 Lesson Check-Up 1.9 Introduction to C++ Programming 1.10 An Important C++ Header Files Directive 1.11 Code::Blocks Menu and IDE 1.12 Loading and Compiling a C++ Program 1.13 Hands-on Activities 1.14 C++ Identifiers and Syntactical Rules 1.15 Lesson Check-Up 1.16 Sample C++ Code Using Output Statement and Assignment Statement 1.17 Input Statement 1.18 Laboratory Activities and Programming Project 1.19 Summative Questions 1.20 Review Exercises 1.21 Programming Exercises Objectives:  Learn about the application program and program development.  Learn about machine languages and higher-level programming languages.  Become familiar with the history of C and C++.  Compile and run C++ programs using an open source C++ compiler.  Recognize semantic and syntactical rules in C++.  Define and initialize variables and constants.  Write arithmetic expressions and assignment statements in C++.  Create programs that read and process input, and display the results.  Process strings, using the standard C++ string type. Programming a Computer Most computer users do not write their own programs. Customized or purchased programs are referred to as application software packages. Applications are programs that tell a computer how to accept instructions from the end user and how to produce information in response to those instructions. Even though good application programs can be purchased, people still need to learn programming. Learning a programming language improves logical and critical thinking URSM-COEng F.M.Fernndo C++ Programming 2 skills for computer-related career (especially engineering) and teaches why applications perform as they do. Engineering works need specific software not readily available in the retail market due to its specialized used. Programs need constant maintenance and monitoring. As hardware, networking, and Internet progress and change, people will be needed to meet the challenge of creating new applications. Programming, a combination of engineering and arts, is a highly marketable skill. Program Development Cycle (PDC) Programmers do not sit down and start writing code right away. Instead, they follow an organized plan, or methodology, that breaks the process into a series of tasks. Just as there are many programming languages, there are many application development methodologies; however, those methodologies tend to be variations of what is called the PDLC. The PDLC follows these 6 steps: 1. Analyze the problem: precisely define the problem to be solved, and write program specifications – descriptions of the programs I-P-O (input-process-output) and user interface. 2. Design the program: use algorithmic thinking to develop a detailed logic plan using tools such as pseudo code, flowcharts, and object structure diagram, event diagrams to group the program activities into modules; devise a method of solution or algorithm for each module; and test the solution algorithm. 3. Code the program: translate the design into an application using a programming language or application development tool or IDE (integrated development environment), by creating the user interface and writing code; including interviews, documentation (comments and remarks) within the code that explains the purpose of the code statement. 4. Test the program: find and correct errors (debugging) until it is error-free and contains enough safeguards to ensure the desired results. 5. Formalize the solution: review and, if necessary, revise internal documentation; formalize and complete end-user (external) documentation. Implement the solution at the user level. 6. Maintain the program: provide education and support to end-users; correct any unanticipated errors that emerge and identify user-requested modifications/enhancements. When a program reaches obsolescence, go back to the first step of problem analysis and the whole process is repeated as a cycle. Machine Languages Programmers write instructions in various programming languages, some directly understandable by the computer and others that require intermediate translation steps. Hundreds of computer languages are in use today, some of the most popular are C++, Java, and Python. These maybe divided into three levels: 1. machine language 2. assembly language 3. high-level language 4. very high-level language URSM-COEng F.M.Fernndo C++ Programming 3 Any computer can directly understand only its own machine language – consists of strings of numbers (ultimately reduced to 1s and 0s, or binary numbers) that instruct computers to perform their most elementary operations one at a time. Machine languages are machine-dependent, i.e., a particular machine language can be used on only one type of computer. As computers became more popular, it became apparent that machine language programming was too slow, tedious and error prone. English-like abbreviations known as mnemonics, formed the basis of assembly language. Translator programs called assemblers were developed to convert assembly code to machine code at computer speeds. An example assembly code for adding two values in hexadecimal format follows: MOV AX,00F3 ADD AX,BX NOP HLT Computer usage increased rapidly with the advent of different assemblers, especially on the microchip-level or microcontroller-based applications. To speed the programming process, high-level languages were developed in which single statements accomplish substantial tasks. Translator programs called compilers convert high-level code into machine code. Obviously, high-level languages are much more desirable from programmer’s standpoint than either machine or assembly languages. Also, interpreter programs were developed that can directly execute high-level programs without the need for compiling those programs into machine language. Although compiled programs execute faster than interpreted programs, interpreters are popular in program development environments in which programs are changed frequently as new features are added and errors (or, bugs) are corrected. Once a program is developed, a compiled version can be produced to run most efficiently. History of C and C++ C++ evolved from C, which evolved from two previous programming languages, BCPL and B. BCPL was developed in 1967 by Martin Richards as a language for writing operating systems (o.s.) software and compilers. Ken Thompson modeled many features in his language B after their counterparts in BCPL and used B to create early versions of the UNIX o.s. at Bell Laboratories in 1970 on a DEC PDP-7 computer. Both BCPL and B were “typeless” languages – every data item occupied one “word” in memory 1 word is equivalent to 2 bytes) and the burden of treating a data item as a whole number or a real number, for example, was the responsibility of the programmer. The C language was evolved from B by Dennis M. Ritchie at Bell Laboratories and was originally implemented on a DEC PDP-11 computer in 1972. C uses many important concepts of BCPL and B while adding data typing and other features. C initially became widely known as the development language of the UNIX operating system. Today, most o.s. are written in C and/or C++. C is now available for most computers. C is hardware independent. With careful design, it is possible to write C programs that are portable to most computers. By the late 1970s, C had evolved into what is now referred to as “traditional C,” “classic C,” or “Kernighan and Ritchie (KNR) C.” URSM-COEng F.M.Fernndo C++ Programming 4 ANSI (American National Standards Institute) cooperated with the ISO (International Standards Organization) to standardize C worldwide; the joint standard document was published in 1990 and is referred to as ANSI/ISO 9899:1990. The second edition of KNR, published in 1988, reflects this version called ANSI C, a version of the language now used worldwide. Because C is a standardized, hardware-independent, widely available language, applications written in C can often be run with little or no modifications on a wide range of different computer systems. C++, an extension of C, was developed by Bjarne Stroustrup in the early 1980s at Bell Laboratories. C++ provides a number of featured that “spruce up” the C language, but more importantly, it provides capabilities for object-oriented programming (OOP). Software developers are now aware that using a modular, object-oriented design and implementation approach (or, new-school software development) can make software development groups much more productive than is possible with previous (old-school software development) programming techniques of structured programming. Many other OOP languages have been developed, including Smalltalk, developed at Xerox’s Palo Alto Research Center (PARC). Smalltalk is a pure OOP language – literally everything is an object (an essentially reusable software component. C++ is a hybrid language – it is possible to program in C++ in either a C-like style, an object-oriented style, or both! C/C++ Standard Library Programs consist of pieces called functions in C; classes and functions in C++. You can program each piece you may need to form a C/C++ program. But most C++ programmers take advantage of the rich collections of existing classes and functions in the C++ standard library. When programming in C++ you will typically use the following building blocks: classes and functions -  from the C/C++ standard library;  you create yourself (programmer-defined/user-defined); and,  from various popular third-party libraries (especially, for MCU-based/embedded system development) The advantage of creating your own functions and classes is that you will know exactly how they work. Built-in classes and functions avoid reinventing the wheel-an important characteristic in OOP known as reusability. The disadvantage is the time- consuming and complex effort that goes into designing and developing you own codes. Basics of a Typical C++ Environment C++ systems generally consist of several parts: a program development, the language and the C++ Standard Library. URSM-COEng F.M.Fernndo C++ Programming 5 Figure 1.A typical C++ environment Creating a C/C++ Program C++ programs typically go through 6 phases to be executed (Figure 1). These are edit, preprocess, compile, link, load, and execute. The first phase consists of editing a file (or, source code). This is accomplished with an editor program (i.e., Notepad, or any ASCII text editor). The programmer types a C++ program with the editor and makes corrections if necessary. The source code is then stored on a secondary storage device (i.e., hard drive, thumb drive, CD, and the like). The source code file names often end with the.cpp,.cxx or.C extensions (note that C is in uppercase). C++ software package for personal computers have built-in editors that are smoothly integrated into the programming environment (IDE). Next, the programmer gives the command to compile the program. The compiler translates the source code into machine code (also referred to as object code). In a C++ system, a preprocessor program executes automatically before the compiler’s translation phase begins. The C++ preprocessor obeys special commands called preprocessor directives (i.e., #include ) which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually consist of including other text files in the file to be compiled and performing various text replacements. The preprocessor is invoked by the compiler before the program is converted to machine code. The next phase is called linking. C++ programs typically contain references to functions defined elsewhere, such as in the standard libraries or in the private libraries of groups of programmers working on a particular object. The object code produced by the C++ compiler typically contains “holes” due to these missing parts. A linker links the object code with the code for the missing functions to produce an executable image (with URSM-COEng F.M.Fernndo C++ Programming 6 no missing pieces). On a typical UNIX-based system, the command to compile and link a C++ program is CC. To compile and link a program named hello.C type CC hello.C at the UNIX prompt and press the key (or key). If the program compiles and links correctly, a file called a.out (on the Code::Blocks open source, main.o) is produced. This is the executable image of the hello.C program. The next phase is called loading. Before a program can be executed, the program must first be placed in the RAM. This is done by the loader, which takes the executable image from disk and transfers to RAM. Additional components from shared libraries that support the program are also loaded. Finally, the computer, under the control of its CPU, executes the program one instruction at a time. Most of C++ IDE software like Code::Blocks or MS Visual C++, automatically perform the last four phases discussed, so that the programmer would only need to build and run the source code as part of the IDE (integrated development environment) in addition to editing and debugging, to execute the compiled program. Also, after compilation in Code::Blocks there is a file named hello.exe saved at its bin folder that you only need to type at the command prompt to execute the compiled program. Most programs in C++ input and/or output data. Certain C++ functions take their input from cin (the standard input stream; pronounced “see-in”) which is normally the keyboard, but cin can be connected to another device. Data is often output to cout (the standard output stream; pronounced “see-out”) which is normally the computer screen, but cout can be connected to another device i.e., disks or printers. There is also a standard error stream referred to as cerr. The cerr stream (normally connected to the screen) is used for displaying error messages. It is common for users to route regular output data, i.e., to a device other than the screen while keeping cerr assigned to the screen so the user can be immediately informed of errors. ASSIGNMENT #1.1 INSTRUCTIONS: Write on a pdf format using Arial 11, single space your answers to the following questions. 1. What do you mean by application software? 2. Why is programming a highly marketable skill? 3. Enumerate the steps in PDLC. 4. Give the levels of programming languages. 5. Who, when, and where was C and C++ developed? 6. Enumerate the building blocks where C++ functions and classes are based. 7. What are the phases of C/C++ program development? 8. After a C++ source code, i.e. prog1.cpp, is successfully compiled and linked using Code::Blocks (or, MS Visual C++), give the complete path and filename (at the command prompt) to run the executable version of this program. 9. Differentiate the C++ standard input stream from standard output stream. 10. What is the purpose of cerr? URSM-COEng F.M.Fernndo C++ Programming 7 Rubrics Evaluation Sheet Progressing Meeting Difficulty Meeting Towards Meeting Achieved Criteria Expectations Expectations Expectations Score (7-10 pts.) (0-2 pts.) (3-6 pts.) The submitted The submitted work The submitted work work results or results or outputs results or outputs Correctness and completeness outputs are are between 84% are below 60% between 100% and 60% correct correct or and 85% correct and complete incomplete and complete The logical order The logical order The logical order and neatness of and neatness of the and neatness of the Presentation and the solution for the solution for the solution for the organization of solutions submitted work submitted work submitted work results or outputs results or outputs results or outputs are very are satisfactory are not satisfactory satisfactory The submitted The submitted work The submitted work work results or results or outputs results or outputs outputs manifest manifest mostly poorly manifests the Authenticity or authentic or novel common qualities required qualities novelty qualities which go and no evidence of and are found either beyond the plagiarism incomplete, common standard incorrect, or has strong evidence of plagiarism Timeliness of design Submission before Submission on the Submission after the submission the set deadline exact set deadline set deadline or non- submission Total Score ___ / 40 URSM-COEng F.M.Fernndo C++ Programming 8 Introduction to C++ Programming Visual C++ is a commercially available C compiler for computers one developed by Borland International and another by Microsoft Corporation bundled with MS Visual Studio (combined with Visual Basic, Visual FoxPro, etc.). Two popular open source IDEs (integrated development environment) for C/C++ are known as Code::Blocks and Bloodshed Dev-C++. The following code is a sample C++ program. 1 //My first C++ program to print a line of text on screen 2 #include 3 using std::cout; // line 3 and 4 can be replaced by 4 using std::endl; // using namespace std; 5 6 int main( ) 7 { 8 cout

Use Quizgecko on...
Browser
Browser