Upload
ellery
View
49
Download
0
Tags:
Embed Size (px)
DESCRIPTION
CMPT 128: Introduction to Computing Science for Engineering Students. Introduction to the basic components of C and C++ programs. Introduction. Computer program : an ordered sequence of instructions whose objective is to accomplish a task . - PowerPoint PPT Presentation
Citation preview
© Janice Regan, CMPT 128, Jan 2007 1
CMPT 128: Introduction to Computing Science for Engineering Students
Introduction to the basic components ofC and C++ programs
Janice Regan CMPT 128 2007-2012 2
Introduction Computer program: an ordered sequence of
instructions whose objective is to accomplish a task.
Programming: process of planning and creating a program
Programming Language: a set of symbols, special words (semantics), and rules for using those symbols and special words (syntax) to create statements to implement the ordered sequence of instructions to accomplish a task
Janice Regan CMPT 128 2007-2012 3
Problem solving When we try to solve a problem using
programming we should always begin by breaking the problem into parts. Each part describes a process that solves
part of the problem (or an object that is part of the problem)
Each part can be implemented as a function within a program to solve the problem (or a class)
Janice Regan CMPT 128 2007-2012 4
Programs and Libraries Programs are divided into smaller
modules called functions. Functions can
be separately tested and reused by many programs
Use other functions Be used by other functions Become part of libraries
Janice Regan CMPT 128 2007-2012 5
Programs Every program has a main function.
The main function controls the highest level of abstraction of the program.
The main function may call other functions to complete lower level tasks (like evaluating an exponential)
Many commonly needed functions are supplied in libraries as part of the C and or C++ language, some less commonly needed functions are available as additional add on libraries
C and C++ C is a subset of C++
C codes will run inside a C++ application All C libraries are available in C++
C++ contains many extensions of C C++ is object oriented and allows classes C++ libraries that are based on classes
cannot be used in C
© Janice Regan, CMPT 128, Sept 2007 6
Janice Regan CMPT 128 2007-2012 7
The Components of a Program Each C or C++ program includes a series of
functions or program units. Every C or C++ application program includes a main
function, sometimes called a main program There may be other functions used by the main
program. Functions from C libraries and C++ libraries (for C++ programs only)
Functions written by the user
For your first programs we will illustrate only a main function and some functions from libraries
Janice Regan CMPT 128 2007-2012 8
Hello World: your first C++ program
// My first C++ program // make the computer print Hello world
// connect to any necessary libraries or objects#include <iostream>
// required, will be explained later in the courseusing namespace std;
// the main function, implements the algorithm to solve the problem// the main function may use functions // main uses input an output functions from the iostream class (library). cout is one of these functions. int main ( ) { cout << "Hello, world!“ << endl;
return 0; }
Janice Regan CMPT 128 2007-2012 9
Hello World: your first C program/* My first C program */ /* make the computer print Hello world */
/* connect to any necessary libraries */#include <stdio.h>
/* the main function, implements the algorithm to solve the problem the main function may use functions input an output functions from the stdio library. printf is one of these functions. */
int main ( ) { printf( “Hello World \n” );
return 0; }
Janice Regan CMPT 128 2007-2012 10
Comments Comments are essential to clearly explain the
purpose of your code and the methods of implementation used When you use another programmer’s code When another programmer tries to use your code When you try to use your own code after months or
years Comments make it is much easier to figure out
how things work in existing code. Comments are not “executed”
Janice Regan CMPT 128 2007-2012 11
Good Comments Give ADDITIONAL information not
obvious from the code// This program calculates the first 20
primes…
Do not translate code into English// Add A to B (BAD COMMENT)C = A+B
Janice Regan CMPT 128 2007-2012 12
C++ Comments: syntax Comments begin // and end at the end of
the line Comments can appear on their own line
// This is a comment Comments can appear on the same line as
an expression A = b + c; // b and c are numbers of people
C and C++, self documenting? A = b + c; // b and c are numbers of people
Better to writeTotalStudents = FullTimeStudents + OtherStudents;
Code is more readable when variables used have names that describe what they contain (much more later)
© Janice Regan, CMPT 128, Sept 2007 13
Janice Regan CMPT 128 2007-2012 14
Hello World: your first program
// My first C++ program // make the computer print the string “Hello world”
// connect to any necessary libraries #include <iostream> using namespace std;
// the main function, implements the algorithm to solve the problem// the main function may use functions from included libraries int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world
return 0; }
Janice Regan CMPT 128 2007-2012 15
C Comments: syntax Comments begin /* and end */ Comments can appear on their own line or
on their own block of lines /* This is a C comment
it continues until it is closed */ Comments can appear on the same line as
an expression A = b + c; /* add b to c, put the result in A */
Janice Regan CMPT 128 2007-2012 16
Hello World: your first C program/* My first C program */ /* make the computer print Hello world */
/* connect to any necessary libraries */#include <stdio.h>
/* the main function, implements the algorithm to solve the problem the main function may use functions input an output functions from the stdio library. printf is one of these functions. */
int main ( ) { printf( “Hello World \n” );
return 0; }
Janice Regan CMPT 128 2007-2012 17
Libraries / C++ objects A main function, or any other function may
wish to use functions in the built in libraries of the C or C++ language or in user developed or application libraries
To use such functions our program must indicate that the libraries are being used and provide a way to access those functions
Janice Regan CMPT 128 2007-2012 18
Hello World: C++// My first C++ program // make the computer print the string “Hello world”
// connect to any necessary libraries or objects#include <iostream> // iostream is a stream object used to read/write using namespace std;
// the main function, implements the algorithm to solve the problem// the main function may use functions from included libraries // the iostream stream object includes read and write functions like cout int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world
return 0; }
Important In our first C++ program we are using the
iostream stream object (library). This object is used to read from the keyboard
and write to the screen Any C++ program that reads or writes
information should include the iostream stream object
Later we will look at the details of what we can do with iostream. (much more)
Janice Regan CMPT 128 2007-2012 19
Janice Regan CMPT 128 2007-2012 20
Hello World: your first C program/* My first C program */ /* make the computer print Hello world */
/* connect to any necessary libraries */#include <stdio.h>
/* the main function, implements the algorithm to solve the problem the main function may use functions input an output functions from the stdio library. printf is one of these functions. */
int main ( ) { printf( “Hello World \n” );
return 0; }
Important In our first C program we are using the stdio
library. This library is used to read from the keyboard
and write to the screen Any C program must use the stdio library Any C++ program may use either the stdio
library or the iostream class
Janice Regan CMPT 128 2007-2012 21
Janice Regan CMPT 128 2007-2012 22
Standard C Libraries #include <stdio.h>
The # at the beginning of the line indicates that the command is for the C preprocessor (a preprocessor directive)
The C preprocessor puts the contents of the file stdio.h into your program (function descriptions) instead of the line of code above then passes the resulting code to the compiler
If the preprocessor directive to include the library is not part of your program, your program will not be able to use the functions in the library
Janice Regan CMPT 128 2007-2012 23
Standard C++ Libraries and objects #include <iostream>
The # at the beginning of the line indicates that the command is for the C preprocessor (a preprocessor directive)
The C preprocessor puts the prototypes (function descriptions) for the functions in the library or object into your program instead of the line of code above then passes the resulting code to the compiler
If the preprocessor directive to include the library is not part of your program, your program will not be able to use the functions in the library
Janice Regan CMPT 128 2007-2012 24
Using Libraries in C and C++ A C or C++ program that wants to use libraries
starts with one or more include directives #include <Library_Reference> #include “Library_Reference” //(details later)
Here Library_Reference indicates the name of a file containing a list of all the functions in the library.
Janice Regan CMPT 128 2007-2012 25
Syntax of include directive#include <Library_Reference>#include “Library_Reference”
“ “ indicates that the files location should be completely specified by the programmer.
< > indicates that the file is part of the C or C++ language itself and can be located by the preprocessor, compiler and linker without programmer intervention.
Janice Regan CMPT 128 2007-2012 26
Hello World: C++// My first C++ program // make the computer print the string “Hello world”
// connect to any necessary libraries and objects#include <iostream> // iostream is a stream object used to read and write using namespace std; // not needed for C
// the main function, implements the algorithm to solve the problem// the main function may use functions from included libraries // the iostream object includes read and write functions like cout int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world }
Name Spaces, C++using namespace std Indicates you wish to use the standard
namespace In large advanced programs it is possible that
you want to use additional namespaces to prevent naming conflicts.
For this course you will always use the standard namespace and you will always have this line in your C++ programs
Janice Regan CMPT 128 2007-2012 27
Janice Regan CMPT 128 2007-2012 28
Hello World: C++// My first C++ program // make the computer print the string “Hello world”
// connect to any necessary libraries and objects#include <iostream> // iostream is a object used to read and write using namespace std;
// the main function, implements the algorithm to solve the problem// the main function may use functions from included libraries // the iostream object includes read and write functions like cout int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world
return 0; }
Janice Regan CMPT 128 2007-2012 29
Hello World: your first C program/* My first C program */ /* make the computer print Hello world */
/* connect to any necessary libraries */#include <stdio.h>
/* the main function, implements the algorithm to solve the problem the main function may use functions input an output functions from the stdio library. printf is one of these functions. */
int main ( ) { printf( “Hello World \n” );
return 0; }
Janice Regan CMPT 128 2007-2012 30
Creating a C or C++ main function
int main ( ) { /* Body of the main function */
return 0; }
The start of the main method is indicated by the statementint main ( )
The body of the main method (the code that accomplishes the task of the main function) is enclosed in { }.
The convention used in this course in to put each of the { } on a separate line.
Janice Regan CMPT 128 2007-2012 31
Hello World: C++// My first C++ program // make the computer print the string “Hello world”
// connect to any necessary libraries and objects#include <iostream> // iostream is an object used to read and write using namespace std;
// the main function, implements the algorithm to solve the problem// the main function may use functions from included libraries // the iostream object includes read and write functions like cout int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world return 0; }
Body of the C++ program For our first program the body consists of
a single line of codecout << "Hello, world!“ << endl;
cout prints to the screen Everything inside the “ “ is printed endl means move to the start of the next
line before printing anything else
Janice Regan CMPT 128 2007-2012 32
Simple examples of C++ cout Print the word OUCH to the screen
cout << “OUCH”; Move to the beginning of a new line then
print the sum of 6.5 and 3.5 to the screen cout << endl << 6.5 + 3.5; Prints lines below to the screen
OUCH10
Janice Regan CMPT 128 2007-2012 33
Simple examples of C++ cout Print the word OUCH to the screen
cout << “OUCH”; Move to the beginning of a new line then
print the sum of 6.5 and 3.5 to the screen float result; // create a variable result
…result = 6.5 + 3.5;cout << endl << result;
Janice Regan CMPT 128 2007-2012 34
What is printedcout << “OUCH”;cout << endl << 6.5 + 3.5;
ANDint result = 0;cout << “OUCH”;;result = 6.5 + 3.5;cout << endl << result;
BOTH Print the lines below to the screenOUCH10
© Janice Regan, CMPT 128, Sept 2007 35
Janice Regan CMPT 128 2007-2012 36
Hello World: your first C program/* My first C program */ /* make the computer print Hello world */
/* connect to any necessary libraries */#include <stdio.h>
/* the main function, implements the algorithm to solve the problem the main function may use functions input an output functions from the stdio library. printf is one of these functions. */
int main ( ) { printf( “Hello World \n” );
return 0; }
Body of the C program For our first program the body consists of
a single line of codeprintf( “Hello World \n” );
printf prints to the screen Everything inside the “ “ is printed except
control characters preceded by \ \n means move to the start of the next
line before printing anything elseJanice Regan CMPT 128 2007-2012 37
Simple examples of C printf Print the word OUCH to the screen
printf( “OUCH”); Move to the beginning of a new line then
print the sum of 6.5 and 3.5 to the screen printf( “\n %lf”, 6.5 + 3.5);
Janice Regan CMPT 128 2007-2012 38
Simple examples of C printf Print the word OUCH to the screen
printf( “OUCH”); Move to the beginning of a new line then
print the sum of 6.5 and 3.5 to the screen float result; // create a variable result
…result = 6.5+3.5;printf( “\n %f”, result);
Janice Regan CMPT 128 2007-2012 39