Multimedia-Programmierung Übung 3

Similar documents
CS 376b Computer Vision

Tutorial-2a: First steps with C++ programming

Cours de C++ Introduction

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 303: Concepts and Tools for Software Development

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

ENERGY 211 / CME 211. Evolution

Separate Compilation Model

C++ Code Structure. Cooperating with the Compiler

6.096 Introduction to C++ January (IAP) 2009

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Introduction to C++ Introduction to C++ 1

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CS 251 Intermediate Programming Java Basics

AN OVERVIEW OF C++ 1

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

QUIZ. What is wrong with this code that uses default arguments?

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

PHY4321 Summary Notes

Variables and numeric types

C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

Console input 26/09/2018. Background. Background

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

CS

CE221 Programming in C++ Part 1 Introduction

Your first C and C++ programs

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Programmazione. Prof. Marco Bertini

CS11 Intro C++ Spring 2018 Lecture 1

These are notes for the third lecture; if statements and loops.

C, C++, Fortran: Basics

EP241 Computer Programming

Short Notes of CS201

Computer Components. Software{ User Programs. Operating System. Hardware

Lab 1: First Steps in C++ - Eclipse

Starting to Program in C++ (Basics & I/O)

CS201 - Introduction to Programming Glossary By

Streams. Ali Malik

Multimedia-Programmierung Übung 1

Separate Compilation of Multi-File Programs

Introduction to Programming Using Java (98-388)

UEE1303(1070) S 12 Object-Oriented Programming in C++

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

CS102 C++ Exception Handling & Namespaces

CS93SI Handout 04 Spring 2006 Apr Review Answers

MA400: Financial Mathematics

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

CS302 - Data Structures using C++

Introduction to C++ Systems Programming

Motivation was to facilitate development of systems software, especially OS development.

Chapter 1 Introduction to Computers and C++ Programming

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

CSCI 1061U Programming Workshop 2. C++ Basics

CSE 374 Programming Concepts & Tools

Your first C++ program

C++ Modern and Lucid C++ for Professional Programmers

Fast Introduction to Object Oriented Programming and C++

CS242 COMPUTER PROGRAMMING

CS11 Introduction to C++ Fall Lecture 1

Compiling and Running a C Program in Unix

Introduction to C++: Part 1 tutorial version 0.2. Brian Gregor Research Computing Services

Introducing C++ to Java Programmers

Introductory Seminar

Data Structures and Programming with C++

September 10,

Part X. Advanced C ++

A brief introduction to C++

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

SCIRun: Module Development Basics

Intermediate Programming, Spring 2017*

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

COMP322 - Introduction to C++ Lecture 01 - Introduction

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Multimedia-Programmierung Übung 3

Pointers and References

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Introduction To C#.NET

Computer Programming : C++

Object-Oriented Programming

Lab # 02. Basic Elements of C++ _ Part1

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

G52CPP Lab Exercise: Hangman Requirements (v1.0)

Common Misunderstandings from Exam 1 Material

Chapter 15 - C++ As A "Better C"

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

y

Introduction to C++ IT 1033: Fundamentals of Programming

QUIZ. What are 3 differences between C and C++ const variables?

Introduction to Programming (Java) 2/12

Transcription:

Multimedia-Programmierung Übung 3 Ludwig-Maximilians-Universität München Sommersemester 2016 Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-1

Today Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-2

A, B, C, C++ procedural programming language with additional features C with Classes object oriented extension of C Strong typing Provides facilities for low-level memory manipulation Influence for Java, C#, etc. Bjarne Stroustrup (born 30. December 1950 in Århus, Denmark) is a Danish computer scientist, most notable for the creation and development of the widely used C++ programming language. Quelle: Wikipedia http://www.cs.tamu.edu/ news/items?id=1797 Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-3

It s just like Java, but... Java vs. C++ Similar Syntax if, else; for, while; etc. Easy to learn Compilation to Virtual Machine Automatic Garbage Collection One file per class More complex, but highperformance, efficient, flexible Native Compilation DIY Separate declaration and implementation of classes two files per class (header files) Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-4

Installation Code::Blocks as IDE and GNU Compiler Linux/CIP Rechner Code::Blocks Windows Code::Blocks + GNU or Virtual Studio Mac do not use Code::Blocks, instead use Xcode http://www.codeblocks.org/ Download the 'codeblocks-16.01mingw-setup.exe' version Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-5

Get your Compiler Working Code::Blocks needs to find the GNU Compiler. If you get an error message saying that it can not find it follow these steps: Go In It to Settings -> Compiler the Global Compiler Settings press the Reset defaults button and confirm the dialogs twice should find the compiler automatically, if not check if you have downloaded the version from the last slide, that has the compiler included. In the Global compiler Settings you can also choose the C++ standard. Newer versions of C++ have more features, that make programming easier and more efficient. For this tutorial choose the C++11 Iso C++ language standard. Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-6

Learning by Example: Hangman For this tutorial we will program a small Hangman game in order to learn the basics of C++. Hangman: One player chooses a secret word. The other player has to guess letters that might be part of the word. If the letter is included in the word its position gets revealed. If the whole word is revealed the guessing player wins, but if the player guesses wrong 7 times the game is lost. Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-7

Step 1: Setup a new Project 1. Open Code::Blocks 2. Go to File New Project 3. Choose Console Application 4. Click Next and choose C++ as a language 5. Give the Project a title, for example Hangman 6. Choose the GNU GCC Compiler 7. Finish 8. Build and Run 9. Add the files from the website to project Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-8

Knowledge 0: Compiling?! Compiler Collection A collection of programs that are needed to create a program Preprocessor Replaces Parts of Code in the Sourcefiles bevor the actual compile step Compiler: Translates Code fragments from C++ to machine code Linker: Combines compiled code fragments into executable program Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-9

Hello World C++ Style #include <iostream> int main() { std::cout << Hello World! << std::endl; return 0; } - Like java the main method is called first, but no need for class - Has to return something 0 means no errors - std is short for standard library has a lot of useful functions and classes to make everything easier Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-10

Knowledge 1: Datatypes and Libraries Primitive Datatypes: int Bool char [] aka C String char void Important modules from the standard library: <iostream> std::string std::cout and std::cin <vector> std::vector<t> <thread> <math> <exception> <algorithms> for_each all_of Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-11

Step 2: The Puzzle Class Puzzle class will model actual Game Let s start by preparing the class: Task: Define attributes and methods in header file and later implement them in source file. Snippets 1-0, 1-1, 1-2: GameState Snippets 1-3: Define game setup and gameloop methods. Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-12

Knowledge 2: Classes and Header Files Header Files(.hpp) only contain definitions and declarations the actual implementation in in the corresponding source file (.cpp) Other modules can then just #include the.hpp file and have access to all functions, classes, structs and enums of the module Well, to be truthful, you don t have to use header files But you should!!! But why???? L Because: The code in x.cpp needs to be compiled only the first time or if it is changed; the rest of the time, the linker will link x s code into the final executable without needing to recompile it. This makes rebuilding your program way faster. Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-13

Step 3: Instantiating and Calling Puzzle Our main module (file with main-method) uses and controls the Puzzle class Task: Create an instance of the class and save it to memory Snippet 2-0: include Puzzle Class Snippet 2-1: Puzzle Pointer, Constructor Call and game loop Snippet 2-2: Constructor implementation Snippet 2-3: Get GameState Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-14

Knowledge 3: Fun with Pointers Constructor Call with new creates a new object somewhere in the computers memory. This location (address) can be assigned to Puzzle* puzzle pointer. Pointer points at beginning of object memory. (Stack vs. Heap) *point* Puzzle Instance &pointer: Gives us the address of the object it is pointing to *pointer: Gives us the value of the same. puzzle->startgame(): with pointer we need intstead of. in order to call a method. Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-15

Step 4: Prepare and Start the Game Task: Add attributes to Puzzle and implement the startgame method Snippet 3-0 Add attributes Snippet 3-1: startgame implementation set the attributes to starting values secret word input Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-16

Step 5: Game Logic Task: Implement the actual game logic Snippet 4-0 The gameloop method is implemented Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-17

But wait what is happening??? Let s look at an example: The player enters a puzzle word: puzzle word = "hello" The program now creates a list of boolean values of the same size as the string: guessed = [false, false, false, false, false] Now another player enters a letter to guess: char input = 'l' Now the program looks for the letter in the string and finds it twice, setting the corresponding boolean values to true: guessed = [false, false, true, true, false] Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-18

What is happening??? (contin.) If the program can t find the letter it subtracts a try from the amount of tries the player has. If after subtracting the amount of tries is 0, the game is over. Now the program looks through the boolean array. If the element is 'false' it prints an '_', if it is true it prints the letter from the puzzle word, that is at the current index. Word = soap guessed = [false, false, true, false] Console: a _ Finally the programm looks through the guessed list. If all values are true the game is over and the guessing player has won. Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-19

Step 6: Console Graphics Task: Draw hangman on the console Snippet 5-0 The draw method is implemented Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-20

Want to know more? API: http://www.cplusplus.com/reference/ The BOOK: https://upload.wikimedia.org/wikibooks/de/5/59/cplusplus.pdf Tutorials: http://www.cplusplus.com/doc/tutorial/ Recommended: http://www.learncpp.com/ Preview on Cocos: https://www.youtube.com/watch? v=qxqgsnuf9cc&list=plrtjmdoyxlf4od_bokn3wjapr7snpxzoe http://wizardfu.com/book/cocos2d-x/ Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-21