23
Win32 Programming Lesson 3: C++ Refresher

Win32 Programming

  • Upload
    drake

  • View
    74

  • Download
    0

Embed Size (px)

DESCRIPTION

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!) - PowerPoint PPT Presentation

Citation preview

Page 1: Win32 Programming

Win32 ProgrammingLesson 3: C++ Refresher

Page 2: Win32 Programming

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

Page 3: Win32 Programming

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?

Page 4: Win32 Programming

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

Page 5: Win32 Programming

Today – everything else First, we’ll begin with “hello world”

#include “stdafx.h”#include <iostream>int _tmain(int argc, _TCHAR* argv[]){

std::cout << "Hello world" << std::endl;return 0;

}

Page 6: Win32 Programming

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 */

Page 7: Win32 Programming

Declarations Also useful to comment And name intelligently Hungarian Notation? See:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs600/html/HungaNotat.asp int iMyCounter; // index for counting through

array…

Page 8: Win32 Programming

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)

Page 9: Win32 Programming

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

Page 10: Win32 Programming

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

Page 11: Win32 Programming

Scope Beware of scope errors… int total;

int count;

int main() {total = 0;count = 0;{

int count;count = 12345;

}++count;return(0);

}

Page 12: Win32 Programming

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?

Page 13: Win32 Programming

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

Page 14: Win32 Programming

Pass by Value #include <iostream>

void inc_counter(int counter) {++counter;

}

int main() {int a_count = 0;inc_counter(a_count);std::cout << a_count << “\n”;return(0);

}

Page 15: Win32 Programming

Pass by Reference #include <iostream>

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

Page 16: Win32 Programming

Pass by Reference – Pt 2 #include <iostream>

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

Page 17: Win32 Programming

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

Page 18: Win32 Programming

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.

Page 19: Win32 Programming

Consider int thing; // Define “thing”

int *piThing; // Create a pointer to an int

thing = 4; // Put the value for in thingpiThing = &thing; // & == Address of… *piThing = 5; // thing now equals five

* is the dereference operator (the thing pointed to)

& is the address operator (the address of)

Page 20: Win32 Programming

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…

Page 21: Win32 Programming

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…

Page 22: Win32 Programming

Dangling Reference Example int *iFunc() {

int x; ... return &x;

} main() {

int a = *iFunc(); }

Page 23: Win32 Programming

Next Class Simple Object Refresher, and Assignment 1