cs201 recitation 1 - bilkent university · 2017-10-05 · let’s modifythe gradebookclass such...

74
CS201 RECITATION 1 Introduction to C++

Upload: others

Post on 27-Jan-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

CS201 RECITATION 1Introduction to C++

Page 2: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Outline

Part 1 : Writing and debugging code with CodeBlocks

Part 2 : Porting, compiling and testing in Dijkstra

Part 3 : Using and understanding header files

Page 3: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 1: Writing and debugging code with

CodeBlocks• Consider the following class:

Page 4: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Let’s modify the GradeBook class such that

• it keeps the midterm, final, homework, and quiz grades

of 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)

Page 5: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

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.

Page 6: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New C++ Project

Page 7: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New C++ Project

Page 8: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New C++ Project

Page 9: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New C++ Project

Page 10: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New C++ Project

Page 11: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New C++ Project

Page 12: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New C++ Project

Page 13: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New Source File

Page 14: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New Source File

Page 15: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New Source File

Page 16: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New Source File

Page 17: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

New Source File

Page 18: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 19: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 20: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 21: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Build & Run

Page 22: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 23: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 24: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 25: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 26: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 27: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 28: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Alternative:

Successfully Compiled!

Page 29: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 30: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 31: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 32: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 33: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 34: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp

Page 35: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Breakpoint

Page 36: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Page 37: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Step into

Page 38: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Page 39: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Page 40: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Next line

Page 41: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Page 42: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Step out

Page 43: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Page 44: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

GradeBook.cpp (Debugging)

Debug/Continue

Page 45: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

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

Page 46: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

At Break Point

Step Into

Next Line

Step Out

Page 47: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

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

Page 48: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: FileZilla

dijkstra.ug.bcc.bilkent.edu.tr

Page 49: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: FileZilla

Page 50: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: FileZilla

Page 51: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: PuTTY

Page 52: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: PuTTY

Page 53: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: PuTTY

Page 54: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: PuTTY

Page 55: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: PuTTY

Page 56: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: PuTTY

Page 57: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

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.

Page 58: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: SSH Secure Shell

Page 59: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: SSH Secure Shell

dijkstra.ug.bcc.bilkent.edu.tr

Page 60: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: SSH Secure Shell

Page 61: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: SSH Secure Shell

New File Transfer Window

Page 62: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: SSH Secure Shell

Page 63: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 2: SSH Secure Shell

Page 64: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

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 :

Page 65: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

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

Page 66: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

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

Page 67: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

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.

Page 68: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 3: Using header files

Page 69: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 3: Using header files

Page 70: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 3: Using header files

Interface (Header, .h) File

Page 71: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 3: Using header files

Implementation (.cpp) File

Page 72: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 3: Using header files

Main (.cpp) File

Page 73: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 3: Using header files

Page 74: CS201 RECITATION 1 - Bilkent University · 2017-10-05 · Let’s modifythe GradeBookclass such that •it keeps the midterm, final, homework, and quiz grades of a particular student

Part 3: Using header files