W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

31
W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: https://cursussen.sharepoint.hu.nl/fnt/8/TCTI- V2CCPP1-10/default.aspx

Transcript of W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

Page 1: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 1

TCTI-V2CCPP1-10C en C++ Programmeren

Daniel TelgenWouter van Ooijen

Site: https://cursussen.sharepoint.hu.nl/fnt/8/TCTI-V2CCPP1-10/default.aspx

Page 2: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 2

Boeken

Boeken:

In zee met CL.v.Moergestel978-9039524794

C++ from the Beginning J. Skansholm (2nd Edition)978-0201721683

These books are highly recommended, but they are not strictly needed.

Page 3: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 3

lesprogram

Week 1: 1. objects, names

2. statements, layout

Week 2: 1. functions, decomposition

2. lists, specifications

Week 3: 1. memory, testing

2. Fagan inspection

Week 4: 1. class, copy-constructor

2. operators1

Week 5: 3. operators2

4. inheritance

Week 6: 5. virtual methods

6. exceptions, trial exam

C

C++

good

programming

practices

Page 4: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 4

C lessons

Lesson Subject Book

Week 1 lesson 1 Objects, names

Week 1 lesson 2 Statements, layout

Week 2 lesson 1 Functions, decomposition

Week 2 lesson 2 Lists, specification

Week 3 lesson 1 Memory, testing

Week 3 lesson 2 Fagan inspection

Page 5: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 5

Assignments

Read the ‘overzicht’ document!

Summary: Individual Fixed deadline: 24:00 before next week’s lesson By email, fixed header, one file attached Each assignment is rated Ratings are averaged No second chance !

Page 6: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 6

Code exist only to be run?

#define P(X)j=write(1,X,1)#define C 39

int M[5000]={2},*u=M,N[5000],R=22,a[4],l[]={0,-1,C-1,-1},m[]={1,-C,-1,C},*b=N,

*d=N,c,e,f,g,i,j,k,s;main(){for(M[i=C*R-1]=24;f|d>=b;){c=M[g=i];i=e;for(s=f=0;

s<4;s++)if((k=m[s]+g)>=0&&k<C*R&&l[s]!=k%C&&(!M[k]||!j&&c>=16!=M[k]>=16))a[f++

]=s;if(f){f=M[e=m[s=a[rand()/(1+2147483647/f)]]+g];j=j<f?f:j;f+=c&-16*!j;M[g]=

c|1<<s;M[*d++=e]=f|1<<(s+2)%4;}else e=d>b++?b[-1]:e;}P(" ");for(s=C;--s;P("_")

)P(" ");for(;P("\n"),R--;P("|"))for(e=C;e--;P("_ "+(*u++/8)%2))P("| "+(*u/4)%2

);}

http://www.de.ioccc.org/main.html

Page 7: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 7

Code is there to be read!

Code is written once, re-written a few times, read many many times.

Reading starts immediately after you type in the first version..

Most of the cost is in reading, not in writing. To lower software cost: make reading easier, event

when that takes more effort (cost) in the writing.

Page 8: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 8

How to make code readable

Names (today) Names must help the reader to understand the code.

Format (layout) The layout must match the statement structure. The layout must be consistent.

Comments Comments must explain what the reader needs to know

but can not easily read from the code. Structure

The code must be structured to be understandable. Known concepts

Use concepts and techniques that are familiar (datastructures, patterns, real-world analogues).

Page 9: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 9

Things in C that have names (identifiers)

Variable Function Typedef Parameter Struct Struct element Macro Enumerate (Label) (File)

Compilation only (unique per file)

Compile and link (can be shared)

Page 10: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 10

C compilation process :separate files, separate objects

#define MESSAGE ”Hello”int main( void){ printf( "m [%s ] @ %X\n", MESSAGE, MESSAGE ); f();}

#define MESSAGE ”Hello”void f( void ){ printf( "f [%s ] @ %X\n", MESSAGE, MESSAGE );}

compilecompile

link

main.c msg.c

msg.omain.o

stdlib.lib

main.exemain.hexmain

Page 11: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 11

C compilation process :use a header file to share text

#include ”msg.h”int main( void){ printf( "m [%s ] @ %X\n", MESSAGE, MESSAGE ); f();}

#include ”msg.h”void f( void ){ printf( "f [%s ] @ %X\n", MESSAGE, MESSAGE );}

compilecompile

link

main.c msg.c

msg.omain.o

stdlib.lib

main.exemain.hexmain

#define MESSAGE "Hello"

msg.h Now only one place to maintain!

Page 12: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 12

The multiple inclusion problem

#include ”message.h”

. . .

#include ”basics.h”

. . .

#include ”basics.h”

#include ”message.h”

. . .

main.c

message.h

message.c

typedef unsigned char byte;

. . .

basics.h

Page 13: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 13

#ifndef basics_h

#define basics_h

typedef unsigned char byte;

. . .

#endif

Avoiding the multiple inclusion problem

#include ”message.h”

. . .

#include ”basics.h”

. . .

#include ”basics.h”

#include ”message.h”

. . .

main.c

message.h

message.c

basics.h

This is an idiom.

Page 14: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 14

Per-compilation named things Typedef Parameter Struct Struct element Macro Enumerate

In each compilation unit, each such thing must be defined once before it is used.

When you want to share a definition between files you put it in a header file (never cut-n-paste!!).

Header files use the #ifdef/#define/#endif idiom to prevent multiple-inclusion problems.

Page 15: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 15

C compilation process:sharing a run-time object

extern char *msg;int main( void ){ printf( ”%s\n”, msg );} char *msg = ”Helllo world”;

compile compile

link

main.c

msg.c

msg.omain.o stdlib.lib

main.exe

main.hex Hello worldmain

Page 16: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 16

C compilation process:sharing a run-time object

extern char *msg;int main( void ){ printf( ”%s\n”, msg );} int msg = 0x1234;

compile compile

link

main.c

msg.c

msg.omain.o stdlib.lib

main.exe

main.hexmain

To the linker a name is just an address.

Page 17: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 17

C run-time named things

Variable Function

Can be Declared : state that it exist

(provide the external properties: type, prototype) Defined : declare and create it

(provide code, room, initial data, etc) Used

Page 18: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 18

C run-time named things

Can be Declared : state that it exist Defined : declare and create it Used

Rules:

1. In each compilation unit, each object must be declared before it is used.

2. In each application, each object that is used must be defined once.

3. Declarations must be in header files, header files must use the #ifndef idiom.

Page 19: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 19

Variable

Variable Global, outside a function, or Local, within a function By default it is a definition, extern makes it a declaration

Function Typedef Parameter Struct Struct element Macro

int saved_x;

extern int x_saved_counter;

void save_x( int a ){

saved_x = a;

x_saved_counter++;

}

unsigned int power_of_two( unsigned int pow

){

unsigned int i, result = 1;

for( i = 0; i < pow; i++ ){

result = 2 * result;

}

return result;

}

Page 20: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 20

Function

Variable Function

With a body it is a definition Header-only is a declaration

Typedef Parameter Struct Struct element Macro

unsigned int strlen(

const char * p

){

. . .

}

unsigned int power_of_two( unsigned int pow

);

void f( void ){

int n, x;

n = strlen( ”Hello world” );

x = power_of_two( n );

}

Page 21: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 21

Reading a small function

void f( int a, int b ){

g( a );

while( b-- ){

(void) h();

}

}

What does this function do? Why does it what it does? Is it correct? When and why should you call it? What parameter values should you pass to it?

Page 22: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 22

Now try again

void ps2_command_send_replies_ignore(

int command

unsigned int n_replies

){

ps2_byte_send( command );

while( n_replies-- ){

(void) ps2_byte_receive();

}

}

What does this function do? Why does it what it does? Is it correct? When and why should you call it? What parameter values should you pass to it?

Page 23: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 23

Choosing names

Old code (and old interfaces!) often used very short names. You know why? (NOT because they liked short names)

Names must help the reader to understand the code. (note: the programer himself is the first reader!)

What's in a name? that which we call a rose

By any other name would smell as sweet;

Page 24: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 24

Guiding principles

Use english. A thing must be a noun. A pure function (no side effects) must be a noun. A function that achieves something (side effects) must be

a verb. Predicates must be constin ‘is’ ( is_even(), is_authorized(),

is_active_alarm(), string_is_valid_integer() ). A name must document its interface (use), not its internals

(implementation). Multi-word names:

The words must be separated. The order of the words is larger-thing-first.

Page 25: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 25

Choosing a name

When applicable and/or needed: Tell what it is or does. (print, count, select) Tell what it returns. (sum, average, first, key, password) Tell what it requires (parameters: width, xy, name, list) Tell what type it returns (char, short, int, long) Tell what types it needs (char, string)

Page 26: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 26

What is wrong?

x = lowerchar( y );

Lower and char are not separated Name does not contain a verb

What does this function do,

1. Test whether y is a lowercase character?

2. Converter y to a lowercase character?

Q: What would be a good name in each of the two cases?

Page 27: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 27

Separating the words in a name

Most programming languages do not allow spaces in identifiers. Justputitalltogetherwithoutseparatorsisnotagoodidea INTHEBEGINNINGPRINTERSHADNOLOWERCASE camelCaseIsNamedAfterTheHumpsOfACamel PascalCaseAlsoCapitalizesTheFirstLetter Adding_Underscores_Makes_Reading_Easier but_with_underscores_who_needs_the_captials Use capitals (only) when needed: TCP_checksum_calculate()

Use lowercase and underscores, uppercase only when needed. Don’t use a leading underscore, this is often used to distinguish a

‘special name’.

http://en.wikipedia.org/wiki/Naming_conventions_(programming)

Page 28: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 28

Libnds keys input functions

Comments please!

http://libnds.devkitpro.org ; select “Keypad and Touch pad”

Page 29: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 29

‘errors’

camelCase. Keys... except for scanKeys (but touchRead does have the verb last!) keysDownRepeat should not contain a verb! keysSetRepeat set should be last

uint32 keys_current( void );

uint32 keys_down( void );

uint32 keys_down_or_repeating( void );

uint32 keys_held( void );

void keys_repeat_set_delay_interval( u8 delay, u8 interval );

uint32 keys_up( void );

void keys_scan( void );

void touch_read( touch_position *position );

Do you see what those int32’s mean?? The explanations are not realy helpful

Page 30: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 30

Read

Book Chapter 4 Chapter 11 Paragraph 17.5

Page 31: W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: .

W 1 L 1 sh 31

Assignment

Go to http://palib-dev.com/manual.html. Make a report (in word) with a three-column table of all function in the following sections. Column one contains the original function name, column two contains the name you would give the functuion, according to the naming conventions exmplaing in the lesson, column three contains your motivation for choosing that name. You might need to check the documentation of a function to find out what it realy does.

Sections:

1. Special Controllers

2. Key input system

3. Sound and MP3 functions