Title: CS-341
1CS-341
- Dick Steflik
- Introduction
2C
- General purpose programming language
- A superset of C (except for minor details)
- provides new flexible ways for defining new types
(classes) - provides symbolic constants
- provides inline substitution of functions
- allows default function arguments
- allows function name overloading
- allows operator overloading
- operators for managing free storage
- provides a reference type
3C Usage
- procedural programming - a better version of C
- cleaner handling of arrays as function parameters
- stream based I/O model
- object oriented programming
- encourages reuse of already tested code rather
than inventing a problem solution over and over - encourages data abstraction
- encourages data hiding
4The C Compiler
- Translates C source code into the machine
language for the hardware platform - output of the compiler are object files (.obj)
which are partially ready to run - object files must be linked together using the
system linker to make an executable file (.exe) - .exe files are specific for the hardware platform
- C is portable only at the source level
5C Compilers
- GNU C
- Open Source
- Multi-platform
- Windows
- UNIX (most)
- LINUX
- MAC
- Microsoft Visual C
- includes everything needed to program the Windows
API
6This semester...
- All programs submitted must be able to run on the
Sun Blade workstations in Classroom 2 of the POD. - This can be insured by using the GNU gpp Compiler
- you may develop on the Sun Blade Workstations, on
LINUX (gpp comes with all standard LINUX
installs) or on CYGWIN/XEMACS (a emulated UNIX
environment that runs on Windows, a CD will be
made available or you may download and install
from the internet.) - Those of you that have Windows machines , I
encoutage to install CYGWIN/EMACS - Those of you with Windows machines that are more
daring I encourage you to make the leap to LINUX - You may also telnet into BINGSUNS and use GPP
directly in that environment
7Java C
- Interpreted
- optimized for the internet
- Derived from C
- Good object model
- Widely accepted as the internet programming
language - 4-5 years old
- Compiled
- optimized for workstation usage
- derived from C
- Poorer object model
- Widely accepted as workstation program- ming
language - 10-12 years old
8The Java Compiler
- Translates Java source code into java bytecodes
- java bytecodes are the machine language for the
Java Virtual Machine - the JVM is a hypothetical machine that written
mostly in Java but a little bit in C and is
compiled specifically for various platforms
(Windows, Sun, HP, LINUX) - Bytecodes are portable between platforms
9Java Application Development
classpath
Java Source File
Java Class File
javac
JVM
Compile into bytecodes
base classes
Run the program
10The C Compiler
- Translates C source code into the machine
language for the hardware platform - output of the compiler are object files (.obj)
which are partially ready to run - object files must be linked together using the
system linker to make an executable file (.exe) - .exe files are specific for the hardware platform
- C is portable only at the source level
11C application development
C Source File
Object File
.EXE File
compiler
Linker
C Include Files
System Object Files
12Java import statements
- imports indicate which parts of which java
packages an application will need at run time - also allows the compiler to run to completion and
not indicate that the class isnt part of the
file being compiles - Remember the compiler only works with the file
being compiled
13C include statements
- indicate to the compiler preprocessor which files
(source) to include in the file to be compiled.
Included files are actually copied into the
source file and replace the include statement as
part of precompilation. - include myclass.h
- include ltstdio.hgt
- include fully qualified file name
14Java application
- is defined as a class that is instantiated when
the program is run - main() is the name of the function to be run
initially - constructor is used initialize the class
15C application
- classes are/can be used by a C application but
the application is not itself an instantiated
class (like Java) - main() is the name of the function where the
executable file starts execution - C classes do not have main() functions
- C objects are initialized via class
constructors, C applications are initialized in
the main() function.
16Java - HelloWorld
public class HelloWorld public static void
main(String args) System.out.println(
Hellow World)
17C - HelloWorld
include ltiostream.hgt int main() cout ltlt
Hello World ltlt \n
18Data Types
19Primitive Types
- Integer types
- char 8 bit
- short 16 bit
- nit 32 bit
- long 64 bit
- Real Types
- float - 32 bit real numbers
- double - 64 bit real numbers
20Constants
- In C constants are similar to variables but are
never allowed to be the target of an assignment
(explicit or implicit) - In C constants are defined as
- const int abc 100 const char plus
21Strings
- C has no native built-in String type
- Strings are implemented as null terminated arrays
of characters, last character of the string is a
null character (\0\) - char 3 abc me
- array abc consists of m, e, null
- abc me (shortcut for defining and
initializing a string) - string functions are in ltstring.hgt
22Boolean (false/true)
- C has no boolean type like Java
- the value 0 represents false
- any other value represents true
- the statement if (50) cout ltltt
- will always print t as 50 is not 0 so the
predicate is true - the statement if (5-5) coutltltt else cout ltlt
f - will always print f as (5-5) is 0 (i.e. False)
23Derived Types
- These operators create new types from the basic
types - pointer to
- const constant pointer
- reference to (addressing operator)
- vector of
- () function returning
24Null statements
- the simplest statement is the null statement
-
- useful if the syntax requires a statement
- for instance to introduce a time delay into a
program - for (int j 0 j lt 100 j)
25Function Parameters
pass by value (default)
upon invoking the function the function creates a
local copy of the parameter and copies the value
to the copy.
int foo (int a , int b)
int x 5 int y 7 int b foo(a , b)
This gives the effect of x and y being input
only as the function cant change them.
x5 y7
foo a 5 b 5
26Function Parameters
pass by reference
passes a reference to the parameter to the
function, the function works with the reference.
The function can both read from and write to the
argument. Internal to the function the name in
the definition is used as an alias for the
argument use the operator to indicate pass by
reference
int foo(int a , int b) .
int x5 int y 7 int t foo(x,y)
27Arrays
Array definition is the same
ex. int myarray5
when used with a function the syntax is
different, to pass an array to a function you no
longer need to pass a pointer to the array, just
pass the array name.
int sum(int cnt , int m) int t
0 for (int i0 iltcnt i) t t
mi return t int x
sum(5,myarray)
28Array passing
always pass by reference (default method)