9/8/14 CS201 RECITATION 1 Part 1: Writing and debugging code with CodeBlocks Consider the following class #include <string> using std::string; class GradeBook{ public: GradeBook(string name){ setCourseName( name ); } void setCourseName(string name){ if(name.length() <=25) courseName = name; else courseName = name.substr(0,25); } string getCourseName(){ return courseName; } private: string courseName; }; 1 9/8/14 Let’s modify this GradeBook class such that It keeps the midterm, final, homework, and quiz grades of a particular student as its data members, and It calculates a letter grade of that student using the computeFinalGrade member function that Takes four input grades from the user Computes the average grade acc. to the following weights Midterm (30%), Final (35%), HW(15%), Quiz(20%) Assigns a letter grade according to 90 ≤ Grade 80 ≤ Grade ≤ 89 70 ≤ Grade ≤ 79 60 ≤ Grade ≤ 69 Grade < 60 Otherwise :A :B :C :D :F : U (unknown) Let’s do it using CodeBlocks CodeBlocks is an integrated development environment. http://sourceforge.net/projects/codeblocks.berlios/ Make sure you downloaded the IDE with its compiler MinGW. 2 9/8/14 NEW C++ PROJECT NEW C++ PROJECT 3 9/8/14 NEW C++ PROJECT NEW C++ PROJECT 4 9/8/14 NEW SOURCE FILE NEW SOURCE FILE 5 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 6 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 7 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 8 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 9 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 10 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 11 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 12 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 13 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 14 9/8/14 GRADEBOOK.CPP GRADEBOOK.CPP 15 9/8/14 CS201 RECITATION 1 Part 2: Porting, compiling and testing in Dijkstra FILEZILLA, OPEN SOURCE FTP CLIENT dijkstra.ug.bcc.bilkent.edu.tr 16 9/8/14 Filezilla, open source ftp client 17 9/8/14 PUTTY SSH CLIENT http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html Putty ssh client 18 9/8/14 Putty ssh client COMPILING C++ PROGRAMS WITH G++ The base command for the Gnu C++ compiler is "g++" Single File Programs To compile a program that is in a single file, the easiest compilation uses the command format: g++ -o <outputName> <inputCodeFilename> Where the inputCodeFilename ends with ".cpp“ Example: g++ -o myExe prog1.cpp 19 9/8/14 MULTIPLE FILE EXAMPLE g++ This command compiles and links the code files "thing.cpp" and "main.cpp" together into the executable program called "myProgram”. g++ -o myProgram thing.cpp main.cpp -o myProgram *.cpp This command compiles and links all the code files with ".cpp" extension. COMPILING C++ PROGRAMS WITH G++ 20 9/8/14 RUNNING YOUR PROGRAM TYPES OF ERRORS Compilation usually syntax errors, undeclared variables and functions, improper function calls. Linker errors usually involve undefined functions or multiplydefined functions or symbols Run-time errors errors -- two varieties: Fatal -- cause program to crash during execution Non-fatal (or logical) -- don't crash the program, but produce erroneous results. 21 9/8/14 DEBUGGING COMPILE STAGE ERRORS Always start at the top of the list of errors. Fix the first error, then recompile and see what is left. If a list of errors is too long, compile and debug one file at a time. When searching for an error, start with the indicated line number, but also look in the vicinity (usually previous lines) for the possible error. Compile portions of programs as you go -don't wait until the program is fully written to do the first compile! CS201 RECITATION 1 Part 3: Using header files 22 9/8/14 BACK TO GRADEBOOK.CPP Let’s try and separate this file into multiple files separating the interface of the class from its implementation as well as separating the user program that uses this code. GRADEBOOK.H 23 9/8/14 GRADEBOOK.H GRADEBOOK.CPP 24 9/8/14 MAIN.CPP NECESSARY FILES 25 9/8/14 UPLOAD WITH FILEZILLA TEST WITH PUTTY 26
© Copyright 2025 ExpyDoc