Title: Object Orientated Systems
1Object Orientated Systems
(A new beginning)
- James Goulding
- A10 jog_at_cs.nott.ac.uk
- Chris Coleman
- A09 cqc_at_cs.nott.ac.uk
2Your new C Obsessed Life
- Lectures
- Monday 12.00 (LT1)
- Friday 11.00 (C33)
- Labs
- Monday 2.00 4.00 (A31)
- Thursday 9.00 11.00 (A31) (starting 3rd Feb)
- Surgery Sessions - (1 hour drop in slots)
- Times to be announced
- Theres more
- Catch up classes if required.
- Discussion board on course website
3Assessment
- 2 Courseworks (50)
- 1) 20 - 21st Feb ?17th March
- 2) 30 - 14th March ? 5th May
- Exam (50)
- Similar to ICP in style, more details to follow.
4Web Page Book List
- Main Course Web Page is at
- www.cs.nott.ac.uk/cqc/OOS
- The recommended texts for CFJ are
- C The Complete Reference 25
- (Herbert Schildt McGraw Hill)
- Or
- The C Programming Language50
- (Bjarne Stroustrup - Addison-Wesley)
5Handy Website
- The C FAQ Lite
- Translated into many languages, including
- French, Russian, Chinese and even English.
- http//www.parashift.com/c-faq-lite/
- VAST amounts of knowledge and tips in here
- Links will appear to sections of it in the notes
from time to time for extra reading.
6Here we go.
- Course is split up into 5 sections
- Java Bashing (In a minute) - 1 Lecture
- Basic C - 4 Lectures
- OO and C - 5 Lectures
- Advanced C and the STL 7 Lectures
- Algorithms and Data Structures 4 Lectures
- Revision Lecture
7What is C?
- Low Level Languages
- - Machine Code, Assembler
- Mid Level Languages
- - Java, C, Pascal, C
- High Level Languages
- - Visual Basic, Symantec Cafe, Delphi
8A really brief C history
- (1958) Algol Created The first ever high-level
structured language with a systematic syntax. - (1969) UNIX created using BCPL (Basic Combined
Programming Language) - B created by Ken Thompson, as a replacement for
BCPL. - (1970) Pascal, a very formal and well defined
language established as the successor to Algol - (1973) C completed, and released as the successor
to B, giving the user control of data types. - (1979) Bjarne Stroustrup begins work on
C-with-Classes, an Object Orientated version of
C.
9Where does C come from?
BJARNEY
10Progression of C
- (1983) C-with-Classes redesigned and released as
C - (1985) First mass release of C compilers.
- (1988) Lots of versions of C begin to emerge.
- (1989) ANSI/ISO standardisation of C begins
- (1999) and a decade later finally approved.
- (1995) Java goes public, viewed by many as being
the successor to C. - (2004) Java, the language of the web, has taken
over the world, and C is becoming a dead
language..
11Progression of C
- .only it hasnt.
- (else youd not be learning about it now)
12What is C used for?
- Word Processors
- Operating systems
- Web Browsers
- Databases
- Spreadsheets, Mail Software.
- Games, Music Software.
- .basically everything
- Almost everything you see is written using C.
Even new C compilers are written using the old
ones.
13So why all this Java stuff then?
- Java is an important language.
- It's easy to learn.
- Its portable.
- It provides meaningful errors (usually).
- It removed complications like templates and
pointers. - Added garbage collection (aka Memory management)
- Added multithreading
- Added GUI libraries
- Protected the user from making many errors.
14So why all this Java stuff then?
- Some see the Recipe for Java as
- Take C.
- Subtract functionality, speed and power.
- Box stuff in classes that take up 10 times the
space that they have to. - Add bugs to every implementation
15Compilers vs Interpreters
Storage
COMPILED
INTERPRETED
CPU/Memory
16Compilers vs Interpreters
- Compiled (eg C)
- Runs faster
- Typically has more functionality
- Easier to Optimise
- More instructions available
- Best choice for complex programs that need to be
fast - Interpreted (e.g. PHP, Perl)
- Slower, but often easier to develop
- Allows runtime Flexibility
- More appropriate for use on the web
17The Java Model
Storage
CPU/Memory
18The Java Model
- Java is unique because it is both compiled and
interpreted. - Java is compiled into byte code, and then
interpreted by the virtual machine. - Great advantage of Java is that the code can be
compiled once and then run on any other type of
machine. - Except it cant necessarily, because of the
variability among java interpreters, and their
implementations on different platforms.
19C Disadvantages
- It does not protect you from making mistakes.
- The power it offers the programmer makes it very
easy to crash your PC. - C with its many concepts and possibilities has
a steep learning curve. - extensive use of some C concepts can very
quickly make C programs very complicated to
read initially. - Shortcuts that it offers can often make it
completely unreadable, eg - if (hrhr!12)(hrlt12)?(P "d ",hr))(P "d
",hr-12)) - Dont panic!!!
20C Advantages
- C is very powerful.
- It is very efficient.
- It is extremely fast.
- Everything can be written using C.
- As you learn C, you learn how computers work on
the inside. - Once you learn C well, you will not want to
program in Java.
21C for Java Programmers
(The Real Beginning)
22C files
- Traditionally C programs use the file extension
.c and C programs the extension .cpp - C is essentially a subset of C, so you could
use a C compiler to run a C program. The two
languages are extremely similar BUT NOT THE
SAME! We are learning C NOT C.
23C Compilers
- DO NOT USE MS VISUAL C at home or in the labs.
It isnt really C. - In the labs, and for the coursework we will be
using a UNIX based compiler called gcc. - Open source standard C compiler
- Produced by the Free Software Foundation
- Just because its free doesnt mean its bad
- Strictly ANSI standards compliant
- Command Line based
- Very versatile
24C Compilers In the Lab
- There are 2 versions of gcc installed on scarlet.
2.95 and 3.4. - 2.95 is old and past it but the default on
scarlet. - 3.4.3 is shiny and new
- To make 3.4 your default
- logon to scarlet and type
- scarlet wget http//www.cs.nott.ac.uk/cqc/OOS/gc
c_setup-0.1.sh - scarlet chmod ux gcc_setup-0.1.sh
- scarlet ./gcc_setup-0.1.sh
- scarlet g --version
- Check version is 3.4.3
-
25C Compilers At home
- Linux You almost certainly have gcc somewhere
on your system. - Win32 You almost certainly dont have it.
- MinGW (Minimalist GNU)
- A port of GCC for windows.
- 2 good ways to use MinGW DevC or with MSYS
(both front ends) - DevC is a full IDE with gcc as the compiler
backend - http//www.bloodshed.net/dev/index.html
- MSYS is a UNIX like shell that runs on windows
- http//www.mingw.org/
26C from home
- Bit of a pain setting up on windows at home.
- You should always be testing on the university
C version. - (This is what we mark courseworks on)
- The answer is to ssh or telnet in over the
internet - in windows select run from the windows menu, and
type - telnet scarlet.cs.nott.ac.uk
- Editing your files is very difficult over telnet
or ssh so better to use ftp (ftp//scarlet.cs.nott
.ac.uk) to transfer your files. This will be
explained again in an exercise sheet.
27Anatomy of a Program
- The project source for a C program
- include ltiostreamgt
- using namespace std
- int main()
-
- cout ltlt No more Big Brother. Please. ltlt endl
- return EXIT_SUCCESS
28Very Little to Learn
- include is the preprocessor command used, to
allow the program access to more functions, in
this case the cout command, from the C Standard
Library iostream. - There is a little more to include, but thatll
do for now. - include is almost always used because the core
C language has hardly any reserved words
29Initial Similarities (C vs Java)
- C is strongly typed.
- C can be Object Orientated.
- C uses almost the same variable types as Java.
- C and Java have the same rules for Identifier
Types - The Good The Bad
- count 1count
- ronnie_size Eric Prydz
- twentyFour 13
30Comments in Code
- Comments are also identical to Java.
- // this is a comment on one line
- / this is a comment
- and it is on more than
- one line /
- No special javadoc style commenting as part of
the language, but external utilities exist. - Eg. doxygen
31C Techniques
- Variables
- Operators
- Decisions
- Loops
321. C Variables Scope
- Variables, can be declared anywhere in the
program. - Within methods, classes, or outside them both in
the global scope - However they are only visible to the program
within the block of code in which they are
defined. - int main()
-
- int x 4
- if (x gt 3)
-
- int y 3
- cout ltlt x is ltlt x ltlt endl
- cout ltlt y is ltlt y ltlt endl
-
- cout ltlt y is ltlt y ltlt endl
- return EXIT_SUCCESS
ERROR
33C Variable Types
- There are various variable types available, some
are shown below - Unlike Java, their range is compiler dependant,
so the values above are minimum values set by the
ANSI standard. - Each compiler defines the range in the ltclimitsgt
header file
342. C Operators
- Operators are similar to in Java
- Addition x y z
- Subtraction x y z
- Multiplication x y z
- Real number division x y / 3.14
- Integer division x y / 10
- Logical AND if (x1 y2)
- Logical OR if (x1 y2)
- Logical NOT if (!x)
35C Operators
- More Identical operators
- Equal to if (x10)
- Not equal to if (x!10)
- Less than if (xlt10)
- Greater than if (xgt10)
- Less than / equal to if (xlt10)
- Greater than / equal to if (xgt10)
36C Operators
- The increment and decrement operators
- Increment x or x
- Decrement x- or -x
- Shorthand Assignments x3 (multiply x by
3) x5 (add 5 to x) - x-10 (subtract 10 from 6)
- x/2 (halve x)
-
37C Operators - prefix / postfix
- There is a subtle difference between x and x
when used in an expression. - x will increment the variable before it does
anything else with it, x will increment after
the evaluation of the expression. - Only in the 1st case is y set to 11.
- Prefix is preferable if simply incrementing a
value. -
x 10 y x
x 10 y x
y 10 y y
Here y is set to 11
Now y is set to 10
But here y is still 10
38Simple Type Conversions
- When variables of one type are mixed with another
Type Conversion occurs. This is not impossible in
Java (where casting is needed), and would cause
an error. - int main()
-
- int x
- char ch
- float f
-
- ch x
- x f
- f x
-
If x is between 0 and 255, ch and x would get the
exact same values. Other wise ch would only
reflect the first 8 bits of x.
Here x will be given the bits that represent the
non fractional part of f.
Here f will be a decimal form of x
39Different Outlooks
- This highlights the differing outlooks of the
languages -
- C assumes youre Clever. It gives the
programmer masses of power. And as Spiderman
says, with power comes great responsibility. - Whereas, Java assumes youre a desk monkey.
-
40Casting in C - Basics
- You can force an expression to be of a specific
type by using casting. The general form of a cast
is - (type) expression
- Where type is a valid data type. For example to
make sure that the expression x/2 evaluates to
type float, write - (float)(x/2)
- Without the cast only an integer division would
have been performed.
413. Decisions
- The structure of an if statement in C (again
stolen by Java) is as follows
42Nested If Statements
-
- int main()
-
- int winner -1
- cout ltlt and the Celebrity Big Freak is
- if (winner1) cout ltlt Bez ltlt endl
- else if (winner2) cout ltlt Brigitte ltlt
endl - else if (winner3) cout ltlt Kenzie ltlt
endl - else cout ltlt Does anyone what this rubbish?
ltlt endl -
- return EXIT_SUCCESS
43Switch Statements
- Switch statements look like this
- switch (expression)
-
- value_1 statements_1 break
- value_2 statements_2 break
- ...
- value_n statements_n break
- default
-
-
-
-
445. Loops
- The syntax of the For loop in C is identical
to Java - for (initialisation condition increment)
-
- statements
-
- For example
- int x
- for(x1 xlt100 x)
-
- cout ltlt Loop Number ltlt x ltlt endl
-
455. Special Cases
- Loops with no bodies, such as a time delay
- for (time0 time lt10000 time)
- You can have as many control variables as you
want in loops. The following is fine -
- for (x0, y0 xylt10 x, y)
- Infinite Loops
- for ( )
-
- cout ltlt Reality TV, no more, please ltlt
endl -
46The While Loop
- An example while loop looks like this
- include ltcstdiogt
- int main()
-
- char ch
- while (ch ! Q)
- ch getchar()
-
- getchar() reads a character from the standard
input (usually the keyboard). - To use it you will need to add include ltcstdiogt
to the top of your programs.
47The Do-while Loop
- The do-while loop repeatedly executes a block of
code indicated by statements as long as the
conditional expression cond_expr is true. - do
-
- someBigBoringCalculation
- while (cond_expr)
48Continue Break
- The continue statement is used to force program
execution to the bottom of the loop, skipping any
statements that come after it, but to continue
doing the loop on the next itteration. - The break statement is used to halt execution of
a loop immediately prior to the loops normal
test condition being met.
49Continue Break
- The exit() statement causes the whole program to
terminate. Much like System.exit(int x) in Java.
The number passed to the function is returned to
the OS. -
- if(ch Q)
-
- exit(0)
-
50Closing Remarks
- A lot is obviously similar to Java so far. This
will not remain the case! - C is way too big to teach in 20-ish lectures.
You will be required to do plenty of
self-learning and practicing outside of lectures
and labs to keep up. - C is hard work initially, but the most valuable
language you can learn. - It is essential not to fall behind! This course
builds on previous material quite rapidly. - Lots and Lots of help and resources available to
you Use Them!
51Thats all for now
- Next Lecture
- What makes up a C program?
- gcc/g tutorial how to compile and run your
C programs - Simple IO Stream usage
- Basic keyboard IO
- Basic file IO