Win32 Programming Lesson 4: Classes and Structures.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
1 Chapter 11 Introducing the Class Pages ( )
Structure.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Programming Language Concepts Lecture 19 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming (Part.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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,
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Call Stacks John Keyser. Stacks A very basic data structure. – Data structure: a container for holding data in a program. – Classes, structs can be thought.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Class Byteline Ustyugov Dmitry MDSP November, 2009.
1 CSC241: Object Oriented Programming Lecture No 13.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C++ Memory Overview 4 major memory segments Key differences from Java
11 Introduction to Object Oriented Programming (Continued) Cats.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Module 8: more on methods #1 2000/01Scientific Computing in OOCourse code 3C59 Module 8: More on methods: In this module we will cover: Overloading of.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Overview of C++ Polymorphism
11 Introduction to Object Oriented Programming (Continued) Cats.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
1 Introduction to Object Oriented Programming Chapter 10.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
C++ Functions A bit of review (things we’ve covered so far)
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
1 C++ Classes and Data Structures Course link…..
Structs versus Classes
Chapter 5 Classes.
Pointers and Linked Lists
Lecture 4-7 Classes and Objects
Stack Memory 1 (also called Call Stack)
Pointers & Functions.
Introduction to Programming
Visit for more Learning Resources
Overview of C++ Polymorphism
Object-Oriented Programming (Part 2)
Pointers & Functions.
CS31 Discussion 1D Winter18: week 6
Presentation transcript:

Win32 Programming Lesson 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 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  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 stack stack(); 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 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…  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:  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 Prints the list out forwards and then in reverse Demonstrates inserting a value 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 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).