Win 32 Programming Lesson 4 Classes and Structures

  • Slides: 24
Download presentation
Win 32 Programming Lesson 4: Classes and Structures

Win 32 Programming Lesson 4: Classes and Structures

Last Time… o o o We covered a quick overview of C++ Now, we

Last Time… o o o We covered a quick overview of C++ Now, we look in more detail at the way classes work This is important, as you’ll need a working knowledge in order to interoperate with Win 32 APIs

Classes o o o Remember Structures? Well, it’s pretty handy to be able to

Classes o o o Remember Structures? Well, it’s pretty handy to be able to combine the functions that use or manipulate the structure and the data itself That’s basically a class

Stacks o Imagine a stack n o o o Remind me… Now, for a

Stacks o Imagine a stack n o o o Remind me… Now, for a stack we want to be able to push and pop things off it Could implement in a struct… but can’t protect from direct manipulation Solution: A Class

Simple Class stack o Fairly simple: class stack { private: int count; // Number

Simple Class stack o Fairly simple: class stack { private: int count; // Number of items on stack int data[STACK_SIZE]; // The items themselves public: // Initialize the stack void init(); // Push an item on the stack void push(const int item); // Pop an item from the stack int pop(); };

Member Variables o In our stack, we declare two private member variables n n

Member Variables o In our stack, we declare two private member variables n n We don’t want direct manipulation Three levels of protection: o o o Public – anyone can access Private – nobody outside the class Protected – similar to private except allows access from derived classes

Functions o inline void stack: : init() { count = 0; //Zero the stack

Functions o inline void stack: : init() { count = 0; //Zero the stack counter… } o Zeroes the stack counter, and gets the stack ready for useful work

Similarly… o inline void stack: : push(const int item) { data[count] = item; count++;

Similarly… o inline void stack: : push(const int item) { data[count] = item; count++; } inline int stack: : pop() { --count; return(data[count]); }

Using our stack… o stack a_stack; // Stack we want to use a_stack. init();

Using our stack… o stack a_stack; // Stack we want to use a_stack. init(); // Initialize the count… a_stack. push(1); a_stack. push(2); std: : cout << “Stack top: ” << a_stack. pop() << “n”;

Our stack shortcomings… o There are several… n n Let’s focus on just one:

Our stack shortcomings… o There are several… n n Let’s focus on just one: initialization Before we use the stack, it would be nice if we didn’t rely on the programmer to initialize it!

Constructor o o We can create a constructor which is called automagically… Add: n

Constructor o o We can create a constructor which is called automagically… Add: n n n // Initialize the stack(); inline stack: : stack() { count = 0; } No longer need stack. init()!

Destructor o Sometimes it is important to know when a class is being destroyed

Destructor o Sometimes it is important to know when a class is being destroyed n n n For example, a class which internally allocates memory must free the memory when it exits Thus, there are destructors… inline stack: : ~stack() { if (count !=0) std: : cerr << “Warning: non-empty stackn”; }

Parameterized Constructors o o Sometimes, it’s handy to force a constructor to take parameters

Parameterized Constructors o o Sometimes, it’s handy to force a constructor to take parameters that initialize the class For example, a class which holds firstnames and lastnames might require these on instantiation n Example: person(const std: : string fname, const std: : string lname);

friend Functions o o o Suppose we wished to compare two of our stacks…

friend Functions o o o Suppose we wished to compare two of our stacks… The member variables are private! Solution: make everything public (doh!) Better solution: the friend keyword Can define a friend function or a friend class which has access to the private members of the class

static variables o o o Suppose you want to keep a running total of

static variables o o o Suppose you want to keep a running total of how many stacks you have at one time Could declare a global variable outside the class, but this is pretty ugly Instead, declare a static inside of private… n n private: static int stack_count; Tells the system that there is only ever one stack_count variable, no matter how many stacks we create

Classes… o o o There’s a lot we haven’t covered in classes and C++

Classes… o o o There’s a lot we haven’t covered in classes and C++ right now – inheritance, overloading, all that good stuff But what we have is enough to get you started with the Win 32 APIs Remember, learning C++ in this class is a means to an end, not an end in itself

Pointers & Classes (OOH!) o o o Just to mess with your heads… and

Pointers & Classes (OOH!) o o o Just to mess with your heads… and because it’s useful Classes can also contain pointers This provides a really neato™ way of constructing linked lists

Consider o o o class item { public: int value; item *next_ptr; } next_ptr

Consider o o o class item { public: int value; item *next_ptr; } next_ptr is a pointer to another item We can create an object with the new syntax: n class item *new_ptr; new_ptr = new item;

delete o o o If new creates an object out of thin air (actually,

delete o o o If new creates an object out of thin air (actually, the heap, but we’ll get to that later)… We must delete it Use: n n n delete pointer; pointer = NULL; // Not needed, but good habit Warning deleting pointers to arrays is different (look this up)

Example: Linked list o o You can create a linked list quite easily, by

Example: Linked list o o You can create a linked list quite easily, by creating two different classes: a link_list container and a class which holds the data elements Hint: you’ll need to use the friend notation to make sure you can manipulate the pointers…

Here… o class linked_list { private: class linked_list_element { public: int data; // Data

Here… o class linked_list { private: class linked_list_element { public: int data; // Data in this el. private: // Pointer to next element linked_list_element *next_ptr; friend class linked_list; }; public: linked_list_element *first_ptr; // First element // Initialize the linked list… linked_list() {first_ptr = NULL; } // Other member functions… };

Adding to the list… o void linked_list: : add_list(int item) { linked_list_element *new_ptr; new_ptr

Adding to the list… o void linked_list: : add_list(int item) { linked_list_element *new_ptr; new_ptr = new linked_list_element; (*new_ptr). data = item; (*new_ptr). next_ptr = first_ptr; first_ptr = new_ptr; }

Assignment Part I o o Just a quick test of SVN You should have

Assignment Part I o o Just a quick test of SVN You should have an SVN repository at: https: //cs. fit. edu/smsvn/<tracks-username> In it, you will find a project a 0. Read the comments and fix it! Due Tuesday, before class. It should take you no more than 30 mins

Assignment Part II o Create a program which does the following: n n Implements

Assignment Part II o Create a program which does the following: n n Implements a doubly-linked list – that’s a linked list which is linked backward and forward – in a class Populates it with X random integer values, between 0 and MAX in value, where X and MAX are taken from the command line: o n n n n “dblprog 20 40” would create a linked list with 20 random values in the range 040. Prints the list out forwards and then in reverse Demonstrates inserting a value 31337 in position 7, and printing the list again You must use a class for this! Program should be a command line program in Visual C++ 2013 Think carefully about what to check in. Make sure when you check it out elsewhere it still builds! Please name your project “dblprog” – that way, we can grade just by cutting and pasting Due: 7 days from now before class starts. Make a new project in your repository called a 1 (just like a 0 that’s already there).