Win32 ProgrammingLesson 4: Classes and Structures
Last Time… 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 Win32 APIs
Classes 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 Imagine a stack
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 Fairly simple:
class stack {private:
int count; // Number of items on stackint data[STACK_SIZE]; // The items themselves
public:// Initialize the stackvoid init();
// Push an item on the stackvoid push(const int item);
// Pop an item from the stackint pop();
};
Member Variables In our stack, we declare two private member
variables We don’t want direct manipulation Three levels of protection:
Public – anyone can access Private – nobody outside the class Protected – similar to private except allows access
from derived classes
Functions inline void stack::init()
{count = 0; //Zero the stack counter…
}
Zeroes the stack counter, and gets the stack ready for useful work
Similarly… inline void stack::push(const int item) {
data[count] = item;count++;
}inline int stack::pop() {
--count;return(data[count]);
}
Using our stack… 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… There are several…
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 We can create a constructor which is called
automagically… Add:
// Initialize the stackstack();
inline stack::stack() {count = 0;
} No longer need stack.init()!
Destructor Sometimes it is important to know when a
class is being destroyed 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 stack\n”;}
Parameterized Constructors 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 Example: person(const std::string fname, const
std::string lname);
friend Functions 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 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…
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… 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 Win32 APIs
Remember, learning C++ in this class is a means to an end, not an end in itself
Pointers & Classes (OOH!) 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 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:
class item *new_ptr;new_ptr = new item;
delete If new creates an object out of thin air
(actually, the heap, but we’ll get to that later)…
We must delete it Use:
delete pointer; pointer = NULL; // Not needed, but good habit Warning deleting pointers to arrays is different
(look this up)
Example: Linked list 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… class linked_list {
private:class linked_list_element {
public:int data; // Data in this el.
private:// Pointer to next elementlinked_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… 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 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 a0. Read the
comments and fix it! Due Tuesday, before class. It should take you
no more than 30 mins
Assignment Part II Create a program which does the following:
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: “dblprog 20 40” would create a linked list with 20 random values in the range 0-
40. 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 a1 (just like a0 that’s already there).