Programming Tutorial
Programming Tutorial
Programming Tutorial
return 0;
}
Complier directive
#include
Header file
<iostream>: input/output stream header
/***********************************************
* This program converts gallons to liters.
* This is our first C++ program.
* Author:
* Date: 10/07/99
***********************************************/
#include <iostream>
using namespace std;
Function block
int main()
Braces
{ begins the body of a function and }
ends the body
int main()
{
float gallons, liters;
cout << Enter number of gallons: ;
cin >> gallons;
// this inputs from the user
Statement
liters = gallons * 3.7854;
Statement terminator
;
cout << Liters: << liters << \n;
Return
return 0;
}
Programming environment
Pre-Compiled
Library Codes
(I/O, math func.)
Output
Editor
Source
Code
(Hello.cpp)
Compiler
Object
Code
(Hello.o)
Debugger
Linker
Executable
Code
(Hello)
Linux commands
Editor
Hello.cpp
g++ -c Hello.cpp
Compiler
Hello.o
Library
Linker
Hello
./ Hello
Loader
execution result
Control Structures
expression true?
Initialization
True
Statement sequence
increment
statement sequence;
C++:
int total = 0;
for ( int even=2; even<1000; even=even+2 )
total += even;
False
Functions
Functions
C++ standard library (Pre-packaged)
User defined
More manageable program - simplify the problem by
decomposing it into small pieces
Software reusability - using existing functions as
building blocks to create new programs
Avoid repeating code
Information hiding - all variables declared in function
definitions are local variables
Function format
Function prototype
return-type function-name ( parameter types );
Function definition
return-type function-name ( parameter-list )
{
declarations and statements; // function body
}
Function Call
Functions can return values
Returned value MUST match the type specified in
the prototype
Function augments
Variables, constants, expressions
Calling
function
l
cal
n
ctio
n
u
F
Called
function
Function name
augments
Example
// the main source file
// saved as tests.cpp
#include <iostream>
#include tests.h
using namespace std;
int main()
{
int a, b, c;
cout << Enter three integers: ;
cin >> a >> b >> c;
cout << Maximum is: ;
cout << maximum(a, b, c) << endl;
cout << minimum(a, b, c) << endl;
return 0;
}
Header files:
Standard header files are used to provide function prototypes for functions
defined in the standard C++ library.
Access standard header files
#include <header-name>
Access user-defined header files
#include myfunctions.h
// allocate memory
nr = inimg.getRow();
nc = inimg.getCol();
ntype = inimg.getType();
nchan = inimg.getChannel();
// read in image
inimg = readImage(argv[1]);
// test the contrast stretching function
outimg = cs(inimg, m, b);
return outimg;
}
}
Image cs(Image &, float, float);
#endif
Makefile
OBJ = Image.o imageIO.o cs.o
AR = ar
INCLUDE = -I../include
all:
${MAKE} libimage.a
libimage.a: $(OBJ)
$(AR) rvu $@ $(OBJ)
ranlib $@
cs.o: cs.cpp
g++ -c cs.cpp $(INCLUDE)
Image.o: Image.cpp
g++ -c Image.cpp $(INCLUDE)
clean:
-rm *.o *~
EXES = testcs
all:
${MAKE} ${EXES}
INCLUDE = -I../include
LIB = -L../lib
testcs: testcs.o
g++ -o testcs testcs.o $(LIB) -limage
testcs.o: testcs.cpp
g++ -c testcs.cpp $(INCLUDE)
clean:
-rm -rf *.o
SSH-Secure Shell
If you want to use linux system, you
can download and install the "SSH
secure shell" into your computer and
do your projects at home instead of
doing in the lab.
http://www.ece.utk.edu/~mkarakay/courses_files/dip.html