1 / 23

Win32 Programming

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

drake
Download Presentation

Win32 Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Win32 Programming Lesson 3: C++ Refresher

  2. 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

  3. 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?

  4. 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

  5. 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;}

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

  7. 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…

  8. 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)

  9. 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

  10. 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

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

  12. 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?

  13. 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

  14. 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);}

  15. 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 

  16. 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 

  17. 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

  18. 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.

  19. 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)

  20. 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…

  21. 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…

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

  23. Next Class • Simple Object Refresher, and Assignment 1

More Related