introduction to c++ creating your first c++ program

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

Upload: abigail-armstrong

Post on 31-Dec-2015

231 views

Category:

Documents


3 download

TRANSCRIPT

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

Introduction to C++Introduction to C++

Creating your first C++ programCreating your first C++ program

Page 2: Introduction to C++ Creating your first C++ program

Writing C++ ProgramsWriting C++ Programs The programmer uses a The programmer uses a text editor text editor to create or to create or

modify files containing C code.modify files containing C code.

C code is a specific instance of C code is a specific instance of source code.source code.

A file containing source code is called a A file containing source code is called a source source file.file.

After a source file has been created, the After a source file has been created, the programmer must programmer must invoke the C compilerinvoke the C compiler..

Page 3: Introduction to C++ Creating your first C++ program

LINUXLINUX

Using Borland complierUsing Borland complier

Page 4: Introduction to C++ Creating your first C++ program

The Result : a.outThe Result : a.out If there are no errors in g++ .cpp, this If there are no errors in g++ .cpp, this

command produces an command produces an executable fileexecutable file, one , one that can be run or executed.that can be run or executed. Both the cpp and the gcc compilers name the Both the cpp and the gcc compilers name the

executable file executable file a.outa.out To execute the program, type a.out at the Unix To execute the program, type a.out at the Unix

promptprompt..

Although we call this “compiling a program”, Although we call this “compiling a program”, what actually happens is more complicated.what actually happens is more complicated.

Page 5: Introduction to C++ Creating your first C++ program

3 Stages of Compilation3 Stages of Compilation1 Preprocessor Preprocessor - modifies the source - modifies the source

codecode Handles preprocessor directivesHandles preprocessor directives

Strips comments and “white space” from the Strips comments and “white space” from the codecode

Page 6: Introduction to C++ Creating your first C++ program

3 Stages of Compilation3 Stages of Compilation2 CompilerCompiler - translates the modified source - translates the modified source

code into object codecode into object code Parser Parser - checks for errors- checks for errors

Code Generator Code Generator - makes the object code- makes the object code

OptimizerOptimizer - may change the code to be more - may change the code to be more efficientefficient

Page 7: Introduction to C++ Creating your first C++ program

3 Stages of Compilation3 Stages of Compilation3 LinkerLinker - combines the object code of our - combines the object code of our

program with other object code to produce program with other object code to produce the executable file.the executable file.

The other object code can come from:The other object code can come from: The Run-Time Library - a collection of object The Run-Time Library - a collection of object

code with an index so that the linker can find code with an index so that the linker can find the appropriate code.the appropriate code.

other object filesother object files other librariesother libraries

Page 8: Introduction to C++ Creating your first C++ program

Compilation DiagramCompilation Diagram

Compiler

Preprocessor ParserCode Generator Optimizer

Linker

<

Source File myprog.cpp

Object File myprog.objOther Obj’s Run-time library Other libraries

Executable file a.out

Editor

Page 9: Introduction to C++ Creating your first C++ program
Page 10: Introduction to C++ Creating your first C++ program

PreProcessor

Complier

Linker

editor

Mypgm.cpp

Header Library

Temp file

Object file

Object Library .EXE

file

Page 11: Introduction to C++ Creating your first C++ program

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

Page 12: Introduction to C++ Creating your first C++ program

Beginning the programBeginning the program

Draw your “Black Box”Draw your “Black Box” identify your inputs and outputsidentify your inputs and outputs

Write your algorithmWrite your algorithm do it in PENCIL!do it in PENCIL! Look at it!Look at it! Play computer - will this work?Play computer - will this work? Think about it!Think about it!

Now, head to the computer...Now, head to the computer...

Page 13: Introduction to C++ Creating your first C++ program

Incremental Approach Incremental Approach to Writing Codeto Writing Code

Move your entire algorithm (highest level) into the text Move your entire algorithm (highest level) into the text editor.editor.

Select the first segment of your algorithm. Write your code Select the first segment of your algorithm. Write your code and test it for this small piece.and test it for this small piece.

For instance: For your projectFor instance: For your project Don’t write the whole program at once.Don’t write the whole program at once. Just write enough that you display the prompt to the Just write enough that you display the prompt to the

user on the screen.user on the screen. Get that part working first.Get that part working first. Next write the part that gets the value from the user, Next write the part that gets the value from the user,

and then just print it out.and then just print it out.

Page 14: Introduction to C++ Creating your first C++ program

Incremental Approach Incremental Approach to Writing Code (continued)to Writing Code (continued)

Get that working. Get that working. Next, pick the next piece of code. Write it.Next, pick the next piece of code. Write it. Get that working. Get that working. Make program modifications:Make program modifications:

• perhaps additional instructions to the userperhaps additional instructions to the user• a displayed program description for the usera displayed program description for the user• add more comments. add more comments.

Continue piece by piece until your whole top Continue piece by piece until your whole top level is working.level is working.

Repeat this process for each function.Repeat this process for each function.

Page 15: Introduction to C++ Creating your first C++ program

A Simple C++ ProgramA Simple C++ Program//* Charlene Davis Sept. 02. 2008 //* Charlene Davis Sept. 02. 2008 Hello.cpp Hello.cpp

//* This program is used as a template for future assignments //* This program is used as a template for future assignments

//* Input: Hello World//* Input: Hello World

//* Output: Hello World //* Output: Hello World

#include <iostream>#include <iostream>

#include <string>#include <string>

using namespace std;using namespace std;

int main()int main()

{{

string name,anychar;string name,anychar;

ccout << "Please enter your name: ";out << "Please enter your name: ";

cin >>name;cin >>name;

cout << "Hello World, this is "<<cout << "Hello World, this is "<< name<< endl;name<< endl;

cin >>anychar; cin >>anychar;

return 0;return 0;

}}

Page 16: Introduction to C++ Creating your first C++ program

Anatomy of a C++ ProgramAnatomy of a C++ Program program header commentprogram header comment

preprocessor directivespreprocessor directives

main ( )main ( ) {{ statement(s)statement(s) }}

Page 17: Introduction to C++ Creating your first C++ program

The Program Header CommentThe Program Header Comment All comments must begin with the All comments must begin with the

characters /* and end with the characters characters /* and end with the characters */*/

The program header comment always The program header comment always comes first!comes first!

The program header comment should The program header comment should include the filename, author, date written include the filename, author, date written and a description of the programand a description of the program

Page 18: Introduction to C++ Creating your first C++ program

Preprocessor DirectivePreprocessor Directive Lines that begin with a # are called Lines that begin with a # are called

preprocessor directivespreprocessor directives The #include <stdio> directive causes the The #include <stdio> directive causes the

preprocessor to include a copy of the preprocessor to include a copy of the standard input/output header file standard input/output header file stdio stdio at this at this point in the code.point in the code.

Us e this with C This header file was included Us e this with C This header file was included because it contains information about the because it contains information about the printf ( ) function that’s used in this program.printf ( ) function that’s used in this program.

Page 19: Introduction to C++ Creating your first C++ program

main ( )main ( ) Every program has a function called main, Every program has a function called main,

where execution beginswhere execution begins

The parenthesis following main indicate to The parenthesis following main indicate to the compiler that it is a function.the compiler that it is a function.

Page 20: Introduction to C++ Creating your first C++ program

Left BraceLeft Brace A left curly brace – A left curly brace –

{ -- begins the body of every function. A { -- begins the body of every function. A corresponding right curly brace must end corresponding right curly brace must end the function.the function.

The style is to place these braces on The style is to place these braces on separate lines in column 1.separate lines in column 1.

Page 21: Introduction to C++ Creating your first C++ program

Right BraceRight Brace This right curly brace -- } --matches the left This right curly brace -- } --matches the left

curly brace above.curly brace above.

It ends the function main ( ).It ends the function main ( ).

Page 22: Introduction to C++ Creating your first C++ program

Good Programming PracticesGood Programming Practices C++ programming standards are available C++ programming standards are available

on the Web -- see course homepageon the Web -- see course homepage You will be expected to conform to these You will be expected to conform to these

standards for all programming projects in standards for all programming projects in this These standards include:this These standards include: Naming conventionsNaming conventions Use of white spaceUse of white space Use of BracesUse of Braces CommentsComments

Page 23: Introduction to C++ Creating your first C++ program

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

Page 24: Introduction to C++ Creating your first C++ program

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

Page 25: Introduction to C++ Creating your first C++ program

Use of White Space (con’t)Use of White Space (con’t) All executable statements are indented All executable statements are indented

one tab stop. How deep should my tabs be one tab stop. How deep should my tabs be ??

Typically 3 or 4 spaces. 2 is not Typically 3 or 4 spaces. 2 is not enough for good readability, more enough for good readability, more than four causes indentation to be than four causes indentation to be too deep.too deep.