230 likes | 422 Views
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!)
E N D
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 <iostream>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: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs600/html/HungaNotat.asp • intiMyCounter; // 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 • intiData_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 <iostream>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 <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
Pass by Reference – Pt 2 • #include <iostream>void inc_counter(int *counter) {++(*counter);}int main() {inta_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 intthing = 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)
Practical Example • Imagine this struct: • struct foo {std::string name;std:string instrument;std::string city;intskill_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