1 / 32

CS 240: Data Structures

CS 240: Data Structures. Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation. Decomposition.

althear
Télécharger la présentation

CS 240: Data Structures

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. CS 240: Data Structures Thursday, June 7th Programming Semantics, Software Life Cycle, Memory Allocation

  2. Decomposition • When you create a decomposition (and particularly when you create a specification) it should be readable to the point that if someone else was looking at it they could code it.

  3. Concerns • Using classes: We will start discussing this today and using them in the next lab and successive classes. • There is no class on Tuesday, June 12th. I will not have office hours on that day either. • Exam: We will start talking about this on the 19th.

  4. Concerns • Functions: • We use these to solve our problems • Functions are created from our decomposition and specifications. • Functions have an input and an output • These inputs and outputs may be constrained • What goes into a blender? What comes out? • What are the inputs into makepizza(…)? • There may also be side effects: • Blenders use electricity • makepizza(…) destroys my supplies!

  5. Concerns • File Layout: • A program may consist of many, many files. • .cpp, .h, .hpp, .rc, .ico, .jpg, .wav, .dll and so on. • These represent the code as well as other resources needed by the program • Our programs will be more limited in scope: • Our main() will have its own .cpp file. • This .cpp file will use #include to put together a larger project • Each other file is broken up into two parts: • .h file for the function prototypes and #ifndef code • .cpp file the function definitions

  6. blackjack.cpp Contains our main() Contains: #include”card.h” #include”deck.h” #include”player.h” #include”game.h” The following files organize our code: Card -> card.h and card.cpp Deck -> deck.h and deck.cpp Player -> player.h and player.cpp Game -> game.h and game.cpp File Layout Example

  7. More on pointers • Pointers allow us to give data to functions • They also allow us to obtain additional memory resources while a program is running.

  8. Value model: A copy of the data is given. This is like burning a copy of a CD to give to a friend because they don’t have clean hands. This is the model that Java uses. int myfunction(int first, int last, double appliedvalue, float secondvalue); Passing data:

  9. Passing data: • Reference model: • This model allows us to share/give data to a function.

  10. #include<iostream> #include”swap.h” using namespace std; int main() { int number = 5; int secondnum = 10; swap(&number,&secondnum); cout << number << endl; cout << secondnum << endl; return 0; } Output: 10 5 Reference passing

  11. swap.h #ifndef SWAP_H #define SWAP_H //Swaps the values pointed to by the parameters. //input: two int pointers //output: none //side effects: the values pointed to by the two parameters are swapped. void swap(int *a, int *b); #endif swap.cpp #include”swap.h” void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } Reference passing

  12. Using arrays • Arrays are a representation of multiple data of the same type: • Either: int numbers[1000]; or • int number1,number2,number3….. • An array is not just a cleaner scheme but much more modifiable. • Also, easier to access (try setting each of the numbers to 15).

  13. Arrays • An array is implicitly a pointer (a static pointer). • Because it is static, we do not use it as we do with pointers. • Generally, we use dynamic arrays which are created differently.

  14. Dynamic Arrays #include<iostream> using namespace std; int main() { int *numbers; int userinput; cin >> userinput; numbers = new int[userinput]; cout << “Array created of size: “ << userinput <<endl; delete [] numbers; }

  15. Dynamic Arrays • If the array the user asked for isn’t large enough? • Lets say the user inputs: 10 • And we change the code to take in int inputs until the user inputs -1.

  16. Dynamic Arrays #include<iostream> using namespace std; int main() { int *numbers; int userinput; cin >> userinput; numbers = new int[userinput]; cout << “Array created of size: “ << userinput <<endl; int location = 0; userinput = 0; while(userinput != -1) { cin >> userinput; numbers[location] = userinput; location++; } delete [] numbers; }

  17. Dynamic Arrays • When the user inputs the 11th value, it uses memory that isn’t ours. • It may not cause an error right away. • In fact, on larger programs, we might just overwrite our own data for awhile. • Dynamic arrays allows us to resize the array if we need to.

  18. #include<iostream> using namespace std; int main() { int *numbers; int userinput; cin >> userinput; numbers = new int[userinput]; cout << “Array created of size: “ << userinput <<endl; int location = 0; int maxsize = userinput; userinput = 0; while(userinput != -1) { cin >> userinput; numbers[location] = userinput; location++; if(location>=maxsize) { ….. } } delete [] numbers; } int *temp = new int[maxsize*2]; for(int i=0;i<maxsize;i++) { temp[i]=numbers[i]; } maxsize *= 2; delete [] numbers; numbers = temp;

  19. Abstract Data Types • An abstract data type (ADT) is a collection of data. • It provides a representation for some more abstract object. class computer { string name,processor_type,brand,video_card; int processor_speed,ram_speed,ram_amount; …. };

  20. ADTs • This allows you to use the ADT instead of defining each field in your code

  21. ADTs #include<iostream> #include”computer.h” using namespace std; int main() { computer mycomputer; mycomputer.ram_amount = 1024; computer *mycomputer_ptr = new computer; mycomputer_ptr->ram_amount = 1024; cout << mycomputer << endl; //This doesn’t work! return 0; }

  22. ADTs • You could print out each field/value/variable within computer. • However, that would be excessive. • Also, what if you want to change what you decided to print? You’d have to change it in every location. • computer can be given functions/methods that generate a printable output.

  23. ADTs - Output • Option 1: • Create a function that uses cout directly: • This is not preferred, it limits what can be done with the output. • Option 2: • Overload “<<“, that way we can use cout on computer. • This is easier to use, but still limits what can be done with the output. • Option 3: • Create a function that returns a string of what we want to output. • We will use options 2 and 3 in this class

  24. computer.h #ifndef COMPUTER_H #define COMPUTER_H class computer { string name,processor_type,brand,video_card; int processor_speed,ram_speed,ram_amount; public: string getoutput(); }; #endif

  25. computer.cpp #include”computer.h” string computer::getoutput() { string retval = “”; retval = “Computer: “ + name; return retval; }

  26. Other ADT benefits • An ADT allows us to group data together. This way everything we want to manipulate is close together. • The variables/fields declared in the class prototype are “global” to all the class functions/methods.

  27. ADT constraints • Generally, you don’t give a user access to your variables/fields. • They could mess things up, so, mutators and accessors are created. • Mutators mutate/change data. • Accessors access data. • ADTs may require constructors and destructors

  28. int main() { computer mycomputer; mycomputer.set_ram_amount(1024); computer *mycomputer_ptr = new computer; mycomputer_ptr->set_ram_amount(1024); cout << mycomputer.getoutput() << endl; cout << mycomputer.get_ram_amount() <<endl; return 0; }

  29. Constructors/Destructors • Constructors: • Allow you to provide initial values • These are needed for dynamic memory within an ADT • Destructors: • If you have any dynamic memory in the ADT, this is where you deallocate it.

  30. computer.h #ifndef COMPUTER_H #define COMPUTER_H class computer { string name,processor_type,brand,video_card; int processor_speed,ram_speed,ram_amount; int *somedynamicmemory; public: string getoutput(); //provides output computer(); //constructor ~computer(); //destructor }; #endif

  31. computer.cpp #include”computer.h” string computer::getoutput() { string retval = “”; retval = “Computer: “ + name; return retval; } computer::computer() { somedynamicmemory = new int[100]; brand = “No name brand, inc.”; processor_speed = 0; } computer::~computer() { delete [] somedynamicmemory; }

  32. Class Project • We will go over the implementation of a card game as a class. • We will implement either: • Triple Triad, http://en.wikipedia.org/wiki/Triple_Triad#Triple_Triad • Tetra Master http://en.wikipedia.org/wiki/Triple_Triad#Tetra_Master • There are some common features of each which we can work on before we make a decision of which game to implement. • We will take a vote on it next Thursday.

More Related