Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 204 Advanced Programming Instructor: Albert Levi FENS 1091, 1.

Similar presentations

Presentation on theme: "CS 204 Advanced Programming Instructor: Albert Levi FENS 1091, 1."— Presentation transcript:

1 CS 204 Advanced Programming Instructor: Albert Levi FENS 1091, 1

2 Couple of different items are discussed in these slides: –Course info –Good programming practice – style, commenting –Preprocessor, compiler, linker –Debugging – using preprocessor directives – assert statement 2

3 CS204 in a Nutshell Continuation of CS201 – I will assume only CS201 knowledge CS204 is an integral part of CS curriculum Coverage (Tentative) – Advanced structured and object-oriented programming Templates, templated classes and functions Data representation, bitwise operations Advanced issues on classes and object oriented programming Inheritance, polymorphism and advanced object oriented design Advanced I/O – Introduction to data structures Pointers and dynamic memory allocation Linked lists, stacks and queues – Writing better, more efficient programs Preprocessing and compiler directives Reusable software (using/creating libraries) Exception handling – Visual programming and graphical user interfaces – Programming with threads 3

4 CS204 in a Nutshell Instructor – Albert Levi,, ext.9563, FENS Text Book – Main text is "Ivor Horton's Beginning Visual C++ 2012", by Ivor Horton, ISBN: 978-1-118-36808-4. – We'll also use "A Computer Science Tapestry" (CS201 book) – Reference books are – "Starting out with C++ Early Objects", 7 th edition, by T. Gaddis, J. Walters and G. Muganda – "Objects, Abstraction, Data Structures and Design using C++" by Koffman and Wolfgang. – We may not stick to the textbooks; you are responsible material covered in class too. – No rush to buy the book; wait a couple of weeks to see if you can do without the book. Grading – Midterm1: 23% ; Midterm2: 23% ; Final: 34% ; Homeworks: 20% – There will be 7-9 homeworks; the weights of the homeworks will be determined later – Passing grade and letter grade boundaries will go to grave with me – Overall grade is not the only criterion for the letter grades. I will also consider exam grades to degtermine the passing grade and letter grade boundaries! 4

5 CS204 in a Nutshell Webpage and electronic media – – Visit the web page frequently; lecture notes will be posted there weekly after the lectures – We will use SUCourse to submit/collect homeworks. – For formal announcements I will use course email list and SUCourse announcements – So please check your emails frequently – Also check SUCourse – For discussions you may SUCourse (well nobody used it for the last couple of years!). – We have a Twitter account @CS204SabanciUni - – Follow it for homework, exam, course related discussions. – I will answer all related questions – Informal, so no language restrictions 5

6 Schedule, Labs and TAs Course Schedule: – Monday, 15:40 – 17:30, FASS G062 (auditorium) – Tuesday, 16:40 -17:30, FASS G062 (auditorium) Lab Schedule – Thursday 13:40 - 15:30, (Sections A1, A2 and A3) – see schedule for the places A2 will be closed. Thus, those who are registered to A2 should attend to A3 starting first week. They must change their section to A3 during the add drop period. – Thursday 17:40 - 19:30, (Sections B1, B2 and B3) – see schedule for the places Please note the change in B1's room to FASS G052. TAs – To-Be-Determined and posted on website TAs will go over course material from the past week through some examples and explain that week’s homework in the lab time. So this is half recitation, half lab hour. For scheduling reasons, please attend to the registered lab section 6

7 Homeworks (1/2) Homeworks really help you to understand the topics Submission is through SUCourse only. Anything else will be ignored. Submit all homework related files in a compressed archive – Zip format must be used. Do not use rar or other formats. Label your homework as – – e.g. – Otherwise, we cannot find your homework Write your name inside the files as well The file types must be native ones –For example, do not copy and paste your code to a MS word file Any missing files in your submission will lose you points. – If a critical file is missing (for example, your source code) your grade may be zero. Do not expect too much detailed explanations in the homework specifications – You are now grown-ups – You have to think how to design and program! 7

8 Homeworks (2/2) Late Submissions – Only one day (24 hours) late submission is allowed with a penalty of 10 pts. – After 24 hours, no submissions are allowed. We need such cutoff to start the grading on time. – If you resubmit after the deadline, last submission date and time will be considered. – SUCourse time will be taken into account. Grading – Homeworks must compile and run. – Test cases will be different than the sample cases in the homework specification – Grades will mostly be assigned based on proper functionality Indentation, proper naming, comments, efficiency issues will also be taken in consideration – During grading, no debugging will be done by the graders to understand what is wrong with the code. Please read the related policy at the website – 8

9 Plagiarism Policy for Homeworks Sanctions – -100 for the first case, fail the course in the next one. – Plus, will be reported to the Dean We use automated and intelligent tools We use all legal techniques to track down people who contact freelancers, who make homeworks for money and homework traders – inside and outside of SU – Any incident will be reported to the disciplinary committee with no exceptions – Valid and useful denouncements will be awarded with bonus grades Please read the related policy at the website – – I really mean it, read!!! – In order not to understand which cases are considered as plagiarism and to take actions before you have bad surprises 9

10 Make-up Policy All medical reports must be submitted and all excuses must be reported BEFORE the exam. It is at the discretion of the instructor to give a make- up exam – LETTER FOR SABANCI UNIVERSITY UNDERGRADUATE PROGRAMS, Clause 19/4 "A make-up exam is given to students who cannot take an exam for reasons acceptable to the faculty member." – I mean, do not rely on fake medical reports The make-up exams may be given as oral exams The make-up exams will be harder than regular ones for fairness reasons – because, make-up takers will have more time to study Please read the policy at 10

11 Good Programming practices, Style, Debugging You will be responsible from the style/practice guidelines listed here for all future homeworks/projects. 11

12 Programming Time spent as a software engineer will be spent mostly on maintaining and improving/upgrading code: – 80-90% time – Yours or others # of lines per major application: – May go as much as >1 million! – It has to be written well so that upgrading/maintaining is manageable Some old programs cannot be improved because nobody understands the code! 12

13 Good Programming Habits: Overview Develop and test your code incrementally Code defensively Code clearly Use modular code Include debugging code Include comments 13 Hocanın dediğini yap, yaptığını yapma Do what the teacher says, not what the teacher does

14 Good Programming Habits: Comments How much? – Enough for someone to be able to understand/modify your code in the future – Ideally when the comments are read without looking at the code, everything should be clear Where? – program header: describe purpose, usage, inputs, outputs, known bugs, author, date, version history, external references – each function and major part: purpose, input and output parameters, returning value – each variable: describe the purpose and usage – instructions: not as paraphrasing the code, describe semantically. When? – Comment when first writing then update as necessary – Check the comments before finalizing You must use sufficient comments in each of your homework submissions – penalty: up to 20pts over a 100pts-homework Style – There are different conventions. Consistency is important. – A suggestion: comments and codes should be separable – A sample is in the next slide 14

15 Good Programming Habits: Comments /****************************************************************************** Purpose: This program is a CD title maker. Usage: cdmake songlist.txt Input, output: Author: Berrin Yanikoglu Date: 22/2/2005 Any known bugs: It works fine for txt files but crashes on empty input files References: Any source used besides the ones you used etc. ******************************************************************************/... int TotalTime;//total time of the songs in CD in seconds... /***************************************************************************** This function computes the remaining length in the CD, after writing all the songs in the input file. Input:... Returns:... *****************************************************************************/ int RemainingLength(....) {... } /*****************************************************************************/ /************* Process Input and Format Output *********************/ /*****************************************************************************/ int ctr;//counter for... 15

16 Good Programming Habits: Comments 16 Clear? temp = box1.x; box1.x = box2.x; box2.x = temp; temp = box1.y; box1.y = box2.y; box2.y = temp;

17 Good Programming Habits: Comments What you should do may be the following. /* swap the two corners */ /* swap the x coordinate */ temp = box1.x; box1.x = box2.x; box2.x = temp; /* swap the y coordinate */ temp = box1.y; box1.y = box2.y; box2.y = temp; 17

18 Good Programming Habits: Naming Conventions Naming Style suggestions: balanceOwedToMe //first word lowcase, then capitalized initials //good for normal vars RecordsInFile //capitalized initials, good for global variables MAX_STUDENT_NUM //all caps - good for constants – e.g. const short MAX_STUDENT_NUM = 1000; – See for a very structured convention that is used by many software professional. No dummy names such as myvar, Ali_stat, SabriReyiz, a, b, c, x, y, z Use context specific and self-explanatory names This improves the readability of your code Similarly, do not call your homeworks deneme, test etc. 18

19 Good Programming Habits: Indentation Vertical alignment of the programming blocks – Beginning and end of blocks should be distinguishable Use and location of the curly brackets { } – It is a good idea to always use it even for one line blocks – Two schools. Whatever you choose, adopt and stick to one format for (i=0; i < NUMCLASS; i++) {... } or for (i=0; i < NUMCLASS; i++) {... } 19 I prefer this

20 Good Programming Habits: Clarity Use empty spaces in your code to separate logical blocks As you make paragraphs in essays Do not code for compactness – We are not encoding knowledge – Clarity is very important Do not use obscure notations o = 5; o = --o + o-- ; //Use ++ or -- only, on one line value = 1; res = (value++ * 5) + (value++ * 3); // do not even do this, even though it looks a little more innocent Do not confuse "compactness" with "programming efficiency" Efficient programming is to move out unnecessary code or to avoid unnecessary iterations, so that no extra computation is done. Compactness refers to how short your code is. You don’t want to make your program unnecessarily long. But compactness must not yield cryptic code: rule is to put only as much code on one line as it is still clearly understandable. 20

21 Good Programming Habits: Code development Develop and test incrementally – Rather than writing the whole program first one of the biggest source of frustration when trying to start a project or homework. Code modularly – Think the solution in pieces Without losing their connection to the big picture Code defensively – Robustness – Proactive defense Take precautions to all possible mistakes – Do not trust yourself Anybody can make mistakes – Especially do not trust the user of your program; he/she can do anything! Example: Check if a file is opened successfully or not. Otherwise, you can get a crash or I/O operations do not work out. ifstream input(filename.c_str()); if ( ) { cout << "could not open file " << filename << endl; return 0; } 21

22 Good Programming Habits: Code development Other’s code – You will often have to deal with! – Add comments as you understand the code, for your sake and others in the future. Don’t ignore warnings – Try to fix – or at least understand why they exist. They may be precursors of catastrophes – For example int j – unreferenced variable Maybe you wanted to use j, but instead of j you wrote jj and jj is another variable. – In ideal case, you should see no warnings when you compile – You can change your project’s warning settings (see Warning levels under Project  Properties  C/C++) so that more important warnings do not get overlooked in a flood of warnings Of course, with the risk of missing some potential problems. 22

23 Preprocessor, Compiler, Linker 23

24 Preprocessor A preprocessor is a program that takes your source file and outputs an intermediate file (translation unit) before compilation. It does text substitutions controlled by preprocessor directives, which are lines starting with # character #include " filename " #include Ex: #include "date.h" contents of the include file (date.h in the example) is inserted into the translation unit – The process is recursive such that if a #include file has other #include files inside, they are also inserted. Full path may be given between " and " Difference between " " and – If quotes are used, first folder of the file containing the include statement is checked. Then the standard folders are searched for the included file. – İf angle brackets,, are used then only the standard folders are searched Caution: You have to avoid multiple inclusions of the same file in different include files. – Using if family compiler directives (will see later) 24

25 #define #define identifier token-string – to define a preprocessor identifier – All occurrences of identifier are replaced by token-string before compilation – commonly used to define constants (but const definitions can also be used; it is up to you) #define PI 3.1416 #define MAXSIZE 10000 #define PROMPT "Please enter your move: " – Alternatively you may define constants, but this is not a compiler directive (directly compiled) const double PI = 3.1416; – A #define without a token-string removes occurrences of identifier from the source file (starting from that line). The identifier remains defined. #define MYDEBUGCODE – See testdefine.cpp for an example 25

26 Conditional Preprocessing #ifdef identifier statements #endif – statements are included translation unit if identifier is defined #ifndef identifier statements #endif – statements are included translation unit if identifier is not defined – In both cases, for identifiers defined using #define – regular identifiers do not count – See next slide for an example and testdefine.cpp for another example 26

27 An example 27 #include "dice.h" #define MYDEBUG #ifdef MYDEBUG cout << n; #endif DoSmthg(); Class dice … } cout << n; DoSmthg(); Actual cpp fileTranslation Unit

28 Conditional Preprocessing (Cont'd) More general conditional preprocessing can be performed using #if, #elif, #else, #endif preprocessor directives – Main idea is similar to nested if statements (but this is done at preprocessing stage) #if constant-expression statements #elif constant-expression statements #else statements #endif – Constant expression must include only #defined identifiers and literals Comparison and boolean operators can be used – Do not use regular variables here. In VC++, they are considered as 0 no matter their values are. Thus it causes unexpected results – See testdefine.cpp for an example 28 - Optional, - Can have multiple times - Optional

29 #undef #undef identifier – Removes identifier which is previously created with #define – In other words, it becomes undefined – Only affects subsequent occurrences of identifier not the previous ones – CAUTION FOR Visual Studio: If you use an undefined identifier in an expression, it is considered as zero, rather than a compiler error in MS Visual Studio. 29

30 Macros The #define directive is also used to define macros with parameters: #define MULTIPLY(a, b) a*b #define SQUARE(x) x*x After theses macro definitions, the preprocessor will replace any text in the forms of MULTIPLY (something, anotherthing) with something*anotherthing SQUARE(something) with something*something These macros are supposed to define a squaring/multiplication operation without writing a real C++ function – But this is not so correct, they are not functions! (See next) 30

31 Preprocessor Macros are Dangerous Consider the following code #define SQUARE(x) x*x int x = SQUARE(3+5); int y = 100/SQUARE(5); int k=5; int z = SQUARE(++k); What is the value of of x? – Answer: 23, why? What is the value of of y? – Answer: 100, why? What is the value of z? – Answer: 49, why? More correct way of defining this macro is #define SQUARE(x) ((x)*(x)) This corrects first two cases above. But even this does not solve the third problem above. 31

32 Use of Macros is not much recommended To be on the safe side, do not use macros Use functions instead int Square (int x) { return x*x; } 32

33 Compiler & Linker The input to the compiler is the translation unit generated by the preprocessor (starting from a single source file - e.g. main.cpp). The compiler generates an object file (e.g. main.obj). Sometimes object files have.o extension. If the project has several cpp files, normally they are compiled separately yielding different object files The linker combines all necessary object files and libraries in the project together to create an executable program. – More on the libraries later. 33

34 Debugging 34

35 Debugging: Diagnostic Messages cout is sometimes the best debugger – No kidding Sometimes, to debug your program, you will include some diagnostic messages For example, //See if all values are correct for (i=0; i < 26; i++) cout << counter[i] << " "; cout << endl; 35

36 Debugging When you think you are done debugging, you can do one of the followings to turn debug statements off (so that your program runs fast and without debug messages) 1) remove all this debug code (e.g. cout’s) Too much work, plus you may need them again when some other problem shows up (i.e. if you need debugging later on) 2) make them conditional to a program variable. For instance: if (debug == 1) { for (i=0; i < 26; i++) cout << counter[i]; cout << endl; } Then by setting the variable debug to 1 or 0 on top of the program, you can control whether the statements will be output or not. Tests such as if (debug == 1) are still done (slows the release version)! 36

37 Debugging Best way: Using the preprocessor directive _DEBUG and conditional preprocessing #ifdef _DEBUG for (i=0; i < 26; i++) cout << counter[i]; #endif This code will only be compiled if the _DEBUG is defined – _DEBUG is a standard preprocessor identifier which is defined in the Debug configuration, but not defined in the Release configuration. – This is automatic when you switch between these two configurations – So when the preprocessor parses your code: – If the _DEBUG preprocessing identifier is defined, it will remove the #ifdef and #endif, but include the lines between in your program. – If the _DEBUG prep. identifier is NOT defined, the preprocessor will remove the #ifdef and #endif and the lines between them out of your program. So your program will be as if you have never written those lines (no extra tests done) 37

38 Debugging: Debug/Release You normally develop under Debug mode (a.k.a. Debug configuration) – After testing your program, you may want to submit it in release mode (release configuration) When you are developing with Debug option, the compiler keeps track of symbolic information so that you can watch over variable values, function call hierarchy etc., at every step in your code. Once you are done debugging and you will release the code to the customer (or teacher), you want to remove this information and any other debugging code you may have added so that your executable is cleaner, smaller and faster. 38

39 Debugging: Debug/Release In VS 2012, you can change configuration between debug and release by – selecting from the Build tab, then from Configuration Manager (see the next screenshot) 39

40 40

41 Preprocessor Directives Under Debug Setting 41

42 Preprocessor Directives Under Release Setting 42

43 Preprocessor Output: debug version 43 n = 1; #ifdef _DEBUG cout << n; #endif DoSmthg(); DoSmthgElse(); n = 1; cout << n; DoSmthg(); DoSmthgElse(); _DEBUG is defined as a standard preprocessor identifier under the Debug build of your code.

44 Preprocessor Output: release version 44 n = 1; #ifdef _DEBUG cout << n; #endif DoSmthg(); DoSmthgElse(); n = 1; DoSmthg(); DoSmthgElse(); _DEBUG is not defined as a preprocessor identifier under the release build of your code.

45 Debugging You can also use your own preprocessor definitions: – You can either add them to the preprocessor definitions under Project/Properties/... where _DEBUG was defined – Or you can define them with #define in the beginning of the file //You can define or not define your debug flags: #define SHORTDBG //#define MAJORDBG... #ifdef SHORTDBG cout << " *** Value of num = " << num << endl; #endif #ifdef MAJORDBG cout << " *** Value of num = " << num << endl; cout << " *** Value of info = " << info << endl; cout << " *** Value of t = " << t << endl;... #endif 45

46 Debugging -Previous solutions require to comment out user defined preprocessor definitions for the release config. -Alternatively, you can also make them conditioned on the automatic flag _DEBUG #ifdef _DEBUG #define SHORTDBG//this code won’t be compiled under release config //#define MAJORBG//this code won’t be compiled under release config #endif -This way, if you are running under the release config., you don’t need to turn SHORTDBG or MAJORDBG off. Since their definition won’t even be compiled, your flags won’t be defined. -See debugging.cpp for an example 46

47 Debugging: Assert statements Syntax: assert (boolean-expression) ; If the boolean-expression evaluates to true, execution continues. If the boolean-expression evaluates to false, execution is halted (program aborts) – A message is dumped at this point, something like the following assertion failed: i < j file code.cpp, line 543 You have to have #include at the beginning of the program to use assert Use assert statements to check for conditions that should be true #include... assert (p != NULL);... assert ( i < j ); 47

48 Debugging: Assert statements assert statement should not replace checking and handling errors (e.g. if the file is found or not) provides compact checks for things that should not happen (e.g. would cause the program crash) – Exceptions Other alternative methods? – We will see exception handling in the coming weeks for a neat organization of error handling 48

49 Debugging: Assert statements The assert statement can be "turned off" by defining the preprocessor identifier NDEBUG before including cassert #define NDEBUG // No Debug #include If you use the release configuration, NDEBUG is already defined, so you do not need to define it yourself. In cassert, NDEBUG is checked if it is defined or not: – If it is defined, the assert statement evaluates to an empty statement, which is what we want for the Release configuration, so the tests are not done and do not slow the program. – If it is not defined (Debug mode/configuration), the assert statements stay and do the tests as intended. 49

50 Debugging: VC++ environment Visual Studio has lots of facilities for debugging – Breakpoints F9 – Run until next breakpoint F5 – Step in: F11 – Step out: Shift-F11 – Step over: F10 – Run to cursor: Ctrl-F10 – Inspecting variables: Variables window (Auto, Locals) Watch window see the MSDN library for more details 50

51 Preprocessor, Compiler, Linker ctd. 51

52 Header Guards A very important use of conditional compilation is to prevent multiple inclusion of header files Multiple inclusion would duplicate declarations, resulting in compiler errors. Maybe you can manually check multiple inclusions of the same file in a single.cpp or.h, but when header files include themselves this manual check becomes very hard  so we need an automatic way for that. Automatic way is to assign a preprocessor definition for each header file (convention is _HEADERFILENAME, where the dot is replaced by _ as well) and check if it is defined before including the content of the header file. It is defined for the first time of inclusion, so in the next trials of inclusion, it is not going to be included. In the following example, _DICE_H is defined when "dice.h" is included the first time, hence later inclusions of "dice.h" won’t have any effect on translation unit. #ifndef _DICE_H #define _DICE_H... //dice content is here #endif 52

53 Compilation with Several Files Sometimes we may need to compile several cpp files – dice.cpp, randgen.cpp, prompt.cpp,... – because some functions and class implementations are in those files if you are using them in your program, you have to compile them together with your main cpp file add to the same project – But there must be only one cpp file with main function Sometimes, the class and function implementations are in precompiled libraries – ready-to-use object code – need not to compile, but add the.lib file to the project 53

54 Linker Combines all necessary object files and libraries together to create an executable program. May create errors even if compilation is successful – unresolved external symbol... – undefined Symbol... – Primary reason would be missing libraries or implementation files (like date.cpp, prompt.cpp) header files provide the definition, but body is missing! – If you have such errors, try to find where those symbols are implemented and include those libs or cpps in your project 54

55 Libraries Creating a statically linked library – choose your project as win32 console application but select "static library " as the application type – Add or write the necessary header and cpp files for your class and/or library – No main function – Build – the output is a.lib file – See staticlib.h and staticlib.cpp for an example Using this library – Create your project that will use this library – Add a main cpp file that will do the job by using this library This is the cpp file with main function – Add the library (.lib file) as a resource file to the project – You do not need to add the cpp file that contains the library function/class implementation – Compile, link and run – See testlib.cpp for an example 55

56 Static versus Dynamic (shared) Libraries The libraries you link statically are linked into the final executable by the linker (remember that they are pre- compiled). Dynamic libraries are libraries that have the library name embedded into the executable but the library itself is not linked to the final executable. – Thus the size of executable is smaller When you build a program, the linker verifies that all the symbols (functions, variables …) required by the program, are either linked into the program, or in one of its shared libraries. However, the object files from the dynamic library are not inserted into the executable file. Instead, when your program is started, a program in the system (called dynamic loader) checks out which shared (dynamic) libraries are needed for the program, loads them to memory, and attaches them to the copy of your program in memory. – Thus, launching your program is a bit –insignificantly – slower 56

57 Miscellaneous Info

58 How to Redirect Output – Right click on Project, choose properties, Debugging properties under Configuration Properties. Add >filename to Command Arguments to redirect Standard output that normally goes to the DOS console to filename in Project directory – However, in this case, input remains in console – See next slide for a snapshot of how to redirect output in VS 2012. 58

59 59

60 How to Add a Folder to Include Directories for a project - 1 – Right click on Project, choose properties, click on VC++ Directories under Configuration Properties. – Edit Include Directories line on the right of the window to add the folder that you want to add. – See next slide for a snapshot 60

61 61

62 How to Add a Folder to Include Directories for a project - 2 – However, the previously described method is only for a specific project. – If you want to make this change default to all projects, select "Property Manager" tab on the left pane of VS2012. Expand the project on Property Manager and right-click on "Microsoft.Cpp.Win32.User" under "Debug | Win32" – Choose "Properties" and change the Include folder as in the previous method. – See next slide for a snapshot 62

63 63

Download ppt "CS 204 Advanced Programming Instructor: Albert Levi FENS 1091, 1."

Similar presentations

Ads by Google