CS201 RECITATION 1 Introduction to C++ Outline Part 1 : Writing and - - PowerPoint PPT Presentation

cs201 recitation 1
SMART_READER_LITE
LIVE PREVIEW

CS201 RECITATION 1 Introduction to C++ Outline Part 1 : Writing and - - PowerPoint PPT Presentation

CS201 RECITATION 1 Introduction to C++ Outline Part 1 : Writing and debugging code with CodeBlocks Part 2 : Porting, compiling and testing in Dijkstra Part 3 : Using and understanding header files Part 1: Writing and debugging code with


slide-1
SLIDE 1

CS201 RECITATION 1

Introduction to C++

slide-2
SLIDE 2

Outline

Part 1 : Writing and debugging code with CodeBlocks Part 2 : Porting, compiling and testing in Dijkstra Part 3 : Using and understanding header files

slide-3
SLIDE 3

Part 1: Writing and debugging code with CodeBlocks

  • Consider the following class:
slide-4
SLIDE 4

Let’s modify the GradeBook class such that

  • it keeps the midterm, final, homework, and quiz grades
  • f a particular student as its data members
  • it calculates a letter grade of the 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%), homework (15%), quiz (20%)
  • assigns a letter grade according to the table

90 ≤ Grade A 80 ≤ Grade ≤ 89 B 70 ≤ Grade ≤ 79 C 60 ≤ Grade ≤ 69 D Grade < 60 F Otherwise U (unkown)

slide-5
SLIDE 5

Let’s do it using CodeBlocks

  • CodeBlocks is an integrated development environment.
  • http://www.codeblocks.org/downloads/26
  • Make sure you download the IDE with its MinGW compiler.
slide-6
SLIDE 6

New C++ Project

slide-7
SLIDE 7

New C++ Project

slide-8
SLIDE 8

New C++ Project

slide-9
SLIDE 9

New C++ Project

slide-10
SLIDE 10

New C++ Project

slide-11
SLIDE 11

New C++ Project

slide-12
SLIDE 12

New C++ Project

slide-13
SLIDE 13

New Source File

slide-14
SLIDE 14

New Source File

slide-15
SLIDE 15

New Source File

slide-16
SLIDE 16

New Source File

slide-17
SLIDE 17

New Source File

slide-18
SLIDE 18

GradeBook.cpp

slide-19
SLIDE 19

GradeBook.cpp

slide-20
SLIDE 20

GradeBook.cpp

slide-21
SLIDE 21

GradeBook.cpp

Build & Run

slide-22
SLIDE 22

GradeBook.cpp

slide-23
SLIDE 23

GradeBook.cpp

slide-24
SLIDE 24

GradeBook.cpp

slide-25
SLIDE 25

GradeBook.cpp

slide-26
SLIDE 26

GradeBook.cpp

slide-27
SLIDE 27

GradeBook.cpp

slide-28
SLIDE 28

GradeBook.cpp

Alternative:

Successfully Compiled!

slide-29
SLIDE 29

GradeBook.cpp

slide-30
SLIDE 30

GradeBook.cpp

slide-31
SLIDE 31

GradeBook.cpp

slide-32
SLIDE 32

GradeBook.cpp

slide-33
SLIDE 33

GradeBook.cpp

slide-34
SLIDE 34

GradeBook.cpp

slide-35
SLIDE 35

GradeBook.cpp (Debugging)

Breakpoint

slide-36
SLIDE 36

GradeBook.cpp (Debugging)

slide-37
SLIDE 37

GradeBook.cpp (Debugging)

Step into

slide-38
SLIDE 38

GradeBook.cpp (Debugging)

slide-39
SLIDE 39

GradeBook.cpp (Debugging)

slide-40
SLIDE 40

GradeBook.cpp (Debugging)

Next line

slide-41
SLIDE 41

GradeBook.cpp (Debugging)

slide-42
SLIDE 42

GradeBook.cpp (Debugging)

Step out

slide-43
SLIDE 43

GradeBook.cpp (Debugging)

slide-44
SLIDE 44

GradeBook.cpp (Debugging)

Debug/Continue

slide-45
SLIDE 45

GradeBook.cpp (Debugging)

  • Step Into :
  • Runs the program until the next instruction is reached.
  • Next Line :
  • Runs the program until the next line of code is reached.
  • Step Out :
  • Runs the program until the current procedure is completed.

Step Out ≥ Next Line ≥ Step Into

slide-46
SLIDE 46

At Break Point Step Into Next Line Step Out

slide-47
SLIDE 47

Part 2: Porting, compiling and testing in Dijkstra

  • FileZilla (FTP client) + PuTTY (SSH Client)
  • FileZilla
  • https://filezilla-project.org/download.php?type=client
  • PuTTY
  • http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
  • SSH Secure Shell
slide-48
SLIDE 48

Part 2: FileZilla

dijkstra.ug.bcc.bilkent.edu.tr

slide-49
SLIDE 49

Part 2: FileZilla

slide-50
SLIDE 50

Part 2: FileZilla

slide-51
SLIDE 51

Part 2: PuTTY

slide-52
SLIDE 52

Part 2: PuTTY

slide-53
SLIDE 53

Part 2: PuTTY

slide-54
SLIDE 54

Part 2: PuTTY

slide-55
SLIDE 55

Part 2: PuTTY

slide-56
SLIDE 56

Part 2: PuTTY

slide-57
SLIDE 57

Part 2: PuTTY

  • The base command for the Gnu C++ compiler is g++
  • Single File Programs
  • The easiest compilation uses the command format:
  • g++ -o <outputName> <cppFile>
  • Example:
  • g++ -o myExe prog1.cpp
  • Multiple File Programs
  • g++ -o <outputName> <cppFile1> <cppFile2> ...
  • Example:
  • g++ -o myProgram thing.cpp main.cpp
  • This command compiles and links the code files "thing.cpp" and "main.cpp"

together into the executable program called "myProgram”.

  • g++ -o myProgram *.cpp
  • This command compiles and links all the code files with ".cpp" extension.
slide-58
SLIDE 58

Part 2: SSH Secure Shell

slide-59
SLIDE 59

Part 2: SSH Secure Shell

dijkstra.ug.bcc.bilkent.edu.tr

slide-60
SLIDE 60

Part 2: SSH Secure Shell

slide-61
SLIDE 61

Part 2: SSH Secure Shell

New File Transfer Window

slide-62
SLIDE 62

Part 2: SSH Secure Shell

slide-63
SLIDE 63

Part 2: SSH Secure Shell

slide-64
SLIDE 64

Part 2: Types of error

  • Compile Time errors
  • Syntax errors
  • Undeclared variables and functions, improper function calls etc.
  • e.g. Forgetting to put semicolon(;) at the end of an instruction.
  • Result :
  • Linker errors
  • Undefined functions or multiply defined functions or symbols
  • e.g. Not including correct header files →
  • Not using the correct namespace →
  • Result :
slide-65
SLIDE 65
  • Run-time errors
  • Fatal Errors
  • Typically cause the program to crash during execution
  • e.g. Trying to access a non-existent memory location.
  • Non-Fatal(Logical) Errors
  • Does not crash the program but produce erroneous results
  • Typically hardest to detect
  • Result : Incorrect program behaviour

Result :

slide-66
SLIDE 66

Part 3: Using header files

Why do we need header files?

  • 1. Speeds up compilation time
  • Upon the change of a single line of code;

Without headers : All of the code needs to be recompiled With headers : Only changing parts need to be recompiled

  • 2. Keeps the code organized
  • Necessary for big projects
  • Allows multiple people to work on the same project

For more info : Headers and Includes: Why and How

slide-67
SLIDE 67

Part 3: Using header files

  • 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.

slide-68
SLIDE 68

Part 3: Using header files

slide-69
SLIDE 69

Part 3: Using header files

slide-70
SLIDE 70

Part 3: Using header files

Interface (Header, .h) File

slide-71
SLIDE 71

Part 3: Using header files

Implementation (.cpp) File

slide-72
SLIDE 72

Part 3: Using header files

Main (.cpp) File

slide-73
SLIDE 73

Part 3: Using header files

slide-74
SLIDE 74

Part 3: Using header files