Win32 Programming Lesson 3: C++ Refresher. Before We Begin  You can call the Win32 APIs in a lot of different languages (Visual J++, Visual Basic.NET,

Slides:



Advertisements
Similar presentations
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Advertisements

Win32 Programming Lesson 4: Classes and Structures.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
C Programming - Lecture 5
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
System Programming Practical Session 7 C++ Memory Handling.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Lecture 20-something CIS 208 Wednesday, April 27 th, 2005.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
C++ Functions A bit of review (things we’ve covered so far)
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Lecture 3: Getting Started & Input / Output (I/O)
Pointers What is the data type of pointer variables?
Overview 4 major memory segments Key differences from Java stack
Motivation and Overview
Command Line Arguments
Pointers and Pointer-Based Strings
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Pointers Psst… over there.
void Pointers Lesson xx
Pointers Psst… over there.
Overview 4 major memory segments Key differences from Java stack
Pointers & Functions.
Code::Block vs Visual C++
CISC/CMPE320 - Prof. McLeod
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
Pointers and dynamic objects
Pointers & Functions.
C Programming - Lecture 5
C++, Sorting, Convex Hull
Presentation transcript:

Win32 Programming Lesson 3: C++ Refresher

Before We Begin  You can call the Win32 APIs in a lot of different languages (Visual J++, Visual Basic.NET, C#) but we’ll be using C++  Lower-level language, closer to the metal  Don’t mix up Managed C++ with Unmanaged (at least for now!)  Don’t be upset if this all feels basic to you; the class will pick up speed next week

C++ History  Based on the name, we might want to remember C; developed by K&R.  C had one goal: to write operating systems  Downside: procedure-oriented  Then came the OO “revolution”  Added classes (and a number of other features) to C and called it C++ Quiz: shouldn’t it be ++C (and what’s the difference?

Classes  Perhaps the most important difference in C++ is the ability to create Classes, just like in Java (only faster ;)  We’ll cover that on Thursday

Today – everything else  First, we’ll begin with “hello world” #include “stdafx.h” #include int _tmain(int argc, _TCHAR* argv[]) { std::cout << "Hello world" << std::endl; return 0; }

Style  You’ve either got it or you’re going to get it  Style starts with commenting code  Single line: /* Comment */ Or: // Comment  Multi-line: /* * Multi-line comment */

Declarations  Also useful to comment  And name intelligently  Hungarian Notation? See: url=/library/en- us/dnvs600/html/HungaNotat.asp url=/library/en- us/dnvs600/html/HungaNotat.asp int iMyCounter; // index for counting through array…

Layout  C++ doesn’t care about indentations…  But I do.  Indenting code makes it readable: if (total <0) { std::cout << “You owe nothing\n”; } else { std::cout << “You owe ” << total << endl; } Clarity (story)

Writing Good Code  You start by deciding what it is you want to write  So many programmers here at FIT start by opening up Visual Studio  I start programming on a whiteboard

Common C++ gotcha  Variable assignment: Variable = Expression  Variable comparison: Variable == Expression  int iData_list[3] Has valid members 0-2 Remember, real programmers count from zero

Scope  Beware of scope errors…  int total; int count; int main() { total = 0; count = 0; { int count; count = 12345; } ++count; return(0); }

Namespaces  Remember I keep using std::cout to print?  That is because the cout variable is part of the std namespace  There’s lots to know about namespaces… for this class, recognize the using keyword using namespace std; What is the downside of this?

References  Ahh, approaching the dreaded pointer  But I promise that pointers really are very simple – once you understand them  Let’s look at some code

Pass by Value  #include void inc_counter(int counter) { ++counter; } int main() { int a_count = 0; inc_counter(a_count); std::cout << a_count << “\n”; return(0); }

Pass by Reference  #include void inc_counter(int& counter) { ++counter; } int main() { int a_count = 0; inc_counter(a_count); std::cout << a_count << “\n”; return(0); }  Beware the “dangling reference”… you’ll see

Pass by Reference – Pt 2  #include void inc_counter(int *counter) { ++(*counter); } int main() { int a_count = 0; inc_counter(&a_count); std::cout << a_count << “\n”; return(0); }  Beware the “dangling reference”… you’ll see

Structures and Unions  A simple way of grouping things together  Used in the Win32 APIs a great deal, so you better get comfortable with them!  struct structure-name { member-type member-name; // Comment member-type member-name; // Comment … };  A union is similar, but members overlap

Pointers  “There are things, and there are pointers to things…”  A thing, we already know about… like an int… This consists of a chunk of memory of particular size  However, we can also create a pointer to something, like a pointer to an int This consists of a value which “points to” (stores the memory location of) a thing in memory. It doesn’t create the thing in memory.

Consider  int thing; // Define “thing” int *piThing; // Create a pointer to an int thing = 4; // Put the value for in thing piThing = &thing; // & == Address of… *piThing = 5; // thing now equals five  * is the dereference operator (the thing pointed to)  & is the address operator (the address of)

Practical Example  Imagine this struct: struct foo { std::string name; std:string instrument; std::string city; int skill_level; } mylist[LENGTH];  How can we sort mylist most effectively? Write me some p-code…

Classic Example  Command line arguments… int main(int argc, char *argv[]) argc is the number of arguments on the command line argv contains the arguments; it’s a pointer to an array…

Dangling Reference Example  int *iFunc() { int x;... return &x; } main() { int a = *iFunc(); }

Next Class  Simple Object Refresher, and Assignment 1