Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating your first C++ program

Similar presentations


Presentation on theme: "Creating your first C++ program"— Presentation transcript:

1 Creating your first C++ program
Introduction to C++ Creating your first C++ program

2 Writing C++ Programs The programmer uses a text editor to create or modify files containing C code. C code is a specific instance of source code. A file containing source code is called a source file. After a source file has been created, the programmer must invoke the C compiler.

3 Using Borland complier
LINUX Using Borland complier

4 The Result : a.out If there are no errors in g++ .cpp, this command produces an executable file, one that can be run or executed. Both the cpp and the gcc compilers name the executable file a.out To execute the program, type a.out at the Unix prompt. Although we call this “compiling a program”, what actually happens is more complicated.

5 3 Stages of Compilation Preprocessor - modifies the source code
Handles preprocessor directives Strips comments and “white space” from the code

6 3 Stages of Compilation Compiler - translates the modified source code into object code Parser - checks for errors Code Generator - makes the object code Optimizer - may change the code to be more efficient

7 3 Stages of Compilation Linker - combines the object code of our program with other object code to produce the executable file. The other object code can come from: The Run-Time Library - a collection of object code with an index so that the linker can find the appropriate code. other object files other libraries

8 Compilation Diagram < Compiler Preprocessor Parser Linker Editor
Source File myprog.cpp Compiler Preprocessor Parser Code Generator Optimizer Object File myprog.obj Other Obj’s Run-time library Other libraries Linker Executable file a.out

9

10 editor Mypgm. cpp Pre Processor Header Library Complier Temp file Object file Linker Object Library .EXE file

11 An algorithm for writing code
Write the algorithm/do flow chart Write the code using emacs (pico, vi) While not yet working… Try to compile the code While there are still syntax errors Fix errors end while still syntax errors Run the program Fix logical errors end while not yet working

12 Beginning the program Draw your “Black Box” Write your algorithm
identify your inputs and outputs Write your algorithm do it in PENCIL! Look at it! Play computer - will this work? Think about it! Now, head to the computer...

13 Incremental Approach to Writing Code
Move your entire algorithm (highest level) into the text editor. Select the first segment of your algorithm. Write your code and test it for this small piece. For instance: For your project Don’t write the whole program at once. Just write enough that you display the prompt to the user on the screen. Get that part working first. Next write the part that gets the value from the user, and then just print it out.

14 Incremental Approach to Writing Code (continued)
Get that working. Next, pick the next piece of code. Write it. Make program modifications: perhaps additional instructions to the user a displayed program description for the user add more comments. Continue piece by piece until your whole top level is working. Repeat this process for each function.

15 A Simple C++ Program //* Charlene Davis Sept. 02. 2008 Hello.cpp
//* This program is used as a template for future assignments //* Input: Hello World //* Output: Hello World #include <iostream> #include <string> using namespace std; int main() { string name,anychar; cout << "Please enter your name: "; cin >>name; cout << "Hello World, this is "<< name<< endl; cin >>anychar; return 0; }

16 Anatomy of a C++ Program
program header comment preprocessor directives main ( ) { statement(s) }

17 The Program Header Comment
All comments must begin with the characters /* and end with the characters */ The program header comment always comes first! The program header comment should include the filename, author, date written and a description of the program

18 Preprocessor Directive
Lines that begin with a # are called preprocessor directives The #include <stdio> directive causes the preprocessor to include a copy of the standard input/output header file stdio at this point in the code. Us e this with C This header file was included because it contains information about the printf ( ) function that’s used in this program.

19 main ( ) Every program has a function called main, where execution begins The parenthesis following main indicate to the compiler that it is a function.

20 Left Brace A left curly brace –
{ -- begins the body of every function. A corresponding right curly brace must end the function. The style is to place these braces on separate lines in column 1.

21 Right Brace This right curly brace -- } --matches the left curly brace above. It ends the function main ( ).

22 Good Programming Practices
C++ programming standards are available on the Web -- see course homepage You will be expected to conform to these standards for all programming projects in this These standards include: Naming conventions Use of white space Use of Braces Comments

23 Examples of Comment Styles
/* a comment */ /*** another comment ***/ /*****/ /*A comment can be written in this * fashion to set it off from the * surrounding code. */

24 More Comments /*******************************************
* If you wish, you can put comments * * in a box. This is typically used for * * program header comments and for * * function header comments * *******************************************/

25 Use of White Space (con’t)
All executable statements are indented one tab stop. How deep should my tabs be ? Typically 3 or 4 spaces. 2 is not enough for good readability, more than four causes indentation to be too deep.


Download ppt "Creating your first C++ program"

Similar presentations


Ads by Google