Title: C for Java Programmers
1C for Java Programmers
2That was I/O
- Yesterday we had a reasonably detailed look at
- Streams
- C console I/O revisited
- This is object orientated
- Formatting flags
- File I/O
- Customisation
- Coursework ONE
3Tidying up some loose ends
- Today well be steaming through
- C specifics
- Namespaces
- Classes in C
- How good are you really with pointers?
42 Different Types of C
- C underwent an extensive revolutionary process
during its development and standardisation. - As a result there are actually 2 versions of C
- traditional C
- Standard C
- Standard C was defined by the ANSI/ISO
standardisation committee. - Standard C is essentially a superset of
traditional C.
5Old Compilers
- You have to be careful if you are using an older
compiler. - During the process of standardisation, the
ANSI/ISO committee added many new features to the
language since this occurred over a period of
years an older compiler might not support them. - This is important because 2 recent additions will
affect every program that you write
6Namespaces
- The key differences between old and modern C
style lie in new-style headers and the namespace
statement.
include ltiostream.hgt int main () cout ltlt
this is old style return 0
include ltiostreamgt using namespace std int
main () cout ltlt this is new style return
0
traditional C
modern C
7Headers
- We know that if you want to use a library
function in your code. - you must include its header file, using the
include statement (e.g. include ltstdio.hgt) and
that file literally is included in your program. - This is essentially C-style.
- Standard C created a new kind of header that
doesnt use specific filenames instead they
specify standard identifiers that can be mapped
to files by the compiler.
8Header Comparison
- C headers are an abstraction that simply
guarantees that the appropriate prototypes and
definitions required by the C library have been
declared. - Because these new-style headers are not filenames
they dont have the .h extension. For example - C still supports the old C-style but be aware
that the comparision isnt always direct - iostream.h ltiostreamgt
- math.h ltcmathgt
- string.h ltcstringgt
9Namespace Analysis
- Contents of new-style headers are contained in
the std namespace. - The purpose of namespaces is to localise the
names of identifiers to avoid name collisions
elements declared in one namespace are completely
separate from elements declared in another. - Originally this wasnt used an all C library
functions were in the global namespace now they
are in std. - So we use the following statement to bring the
std namespace into visibility - using namespace std
10Why do all of this?
- C saw an explosion of variable, function and
class names prior to the namespace statement
all of his competed for slots in the global
namespace. - If you wrote a function called abs() for example
it too would be stored in the global namespace,
and hence there would be a conflict with the one
already there. - This got even worse as there was increased use of
3rd Party libraries, which might use the same
names for classes.
11Classes in C
BJARNEY
- C began as an expanded version of C Started
in 1979 it was originally called C-with-classes
(taking OOP from Simula67). - C is an excellent language but it has its
pragmatic limits once code exceeds from 25,000
to 100,000 lines it can become so complex that it
is impossible to grasp in its totality. - C was created to allow this barrier to be
broken. - I believe classes should be there for a purpose
to make your code easier to grasp, to speed up
your coding, to allow other people to use it and
so on.
12Structs and Classes
- It is easier to understand classes in general if
you view them as an extension of structs - Structs encapsulate data to make your program
easier to use. You can define it once then use it
throughout your code. - To access its fields all you need to do is to use
the dot operator. - So essentially Classes are Structures with member
functions. In fact structs and classes in C
have evolved to be almost identical.
13Anatomy of a Class
- Classes have the following features
- Declaration
- Access Control
- Constructor
- Destructor
- Variables
- Methods
- A Special Pointer called this.
14Class Structure
Header
Constructor
Destructor
Member Functions
Main Program
15Class Header
A Valid C Name
class Keyword
- class ClassName
- public
- //Prototypes for constructors
- //and public member functions
- //and declarations for public
- //data attributes go here
- ...
- ...
- private
- //Prototypes for private data
- //members and declarations for
- //private data attributes go here
- ...
- ...
public Keyword
private Keyword
Never forget this semi-colon at the end of the
header
16Example Header
- class rectangle
- public
- int height
- int width
- void initialise(int w, int h)
- int getArea()
- void display()
- private
- void drawRow()
- void drawColumn()
Instance Variables
Public methods
Only available to functions inside the class in
this case display will use them
17Public and Private
- Set something as private and it can only be
accessed by functions within the class itself. - This allows you to protect everything inside your
class from outside interference. - You can set public first or private first its up
to you I recommend public section first as this
provides the interface to the class. - There is also protected section well look at
these in more detail later on when we cover
inheritance.
18Class Member Functions
- Unlike Java class member functions are not
contained within the class block of code but are
written after it. - Because of this you need to indicate which class
a function is associated with. - type classNamefunctionName
- //function body
-
- For example
- int rectanglegetArea()
- return heightwidth
-
General Form
19Constructor
- It is very common for some part of an object to
require initialisation before it is used. Hence
there is a special function that allow us to do
this the constructor. - The constructor class is a function that has the
same name as the class - classNameclassName(parameters)
- //Initialise Variables by copying
- //the parameters given to our
- //classes instance variables
-
20Full Example
- class rectangle
- public
- int height
- int width
- rectangle()
- rectangle()
- void initialise(int w, int h)
- int getArea()
- void display()
- private
- void drawRow()
- void drawColumn()
21Full example - Constructor
rectanglerectangle() height 5 width
5 cout ltlt rectangle has been
constructed\n rectanglerectangle() cout
ltlt rectangle has been destroyed\n void
rectangleinitialise(int h, int w) height
h width w
Constructor
Destructor
Initialise Function
22Full Example Member Functions
int rectanglegetArea() return heightwidth
void rectangledisplay() drawRow() for
(int i0 iltheight-2 i) drawColumn()
drawRow() void rectangledrawRow() for
(int i0 iltwidth i) cout ltlt cout ltlt
endl void rectangledrawColumnRow() cout
ltlt for (int i0 iltwidth-2 i) cout ltlt
cout ltlt \n
Private Functions
23What can C classes do
- that java cant?
- Function Overloading
- Operator Overloading
- Destructors
- Separate Class Header from its methods
- Classes dont need the new statement
24Combining Everything
- Were soon going to be using pointers, classes,
DMA all combined together, so its important to
know at this point - How much do you really know
- about pointers?
..Surprise 30 minute Test on Pointers ?
25Exercise of the day (really)
- What does the program do?
include ltiostreamgt using namespace std int
main() int x 10 int px x cout
ltlt px ltlt endl cout ltlt x ltlt ' ' ltlt (px)
ltlt ' ' ltlt x ltlt endl return 0
26Exercise of the day (really)
- What does the program do?
include ltiostreamgt using namespace std int
main() int x 1,2,3,4,5,6,7,8,9 int
px int i cout ltlt x0 for (i0, px
xi lt sizeof(x)/sizeof(int)-1 px,i) cout
ltlt ", " ltlt px ltlt "-" ltlt px1 cout ltlt endl
return 0
27Exercise of the day (really)
- What does the program do?
int main(int argc, char argv) for (char
pc argvpc lt argv argcpc) cout ltlt
pc ltlt ", " cout ltlt "\b\b " ltlt endl
return 0
28Exercise of the day (really)
- What does the program do?
int main() int x 2,4,5,9,2,0,3,9,4,38,2
82,77 const int dimx sizeof(x)/sizeof(int)
int px, qx int endx x dimx for
(pxxpxltendx-1px) for (qxxqxltendx-1qx
) if (qx0 lt qx1) int h qxqx
(qx1)(qx1) h for (px xpx lt
endxpx) cout ltlt px ltlt " " cout ltlt
endl return 0