1 / 19

Container Classes

Container Classes. A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as a class, along with member functions to add, remove, and examine items. For the first example, think about a bag. Bags.

tania
Télécharger la présentation

Container Classes

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. Container Classes • A container class is a data type that is capable of holding a collection of items. • In C++, container classes can be implemented as a class, along with member functions to add, remove, and examine items.

  2. For the first example, think about a bag. Bags

  3. For the first example, think about a bag. Inside the bag are some numbers. Bags

  4. When you first begin to use a bag, the bag will be empty. We count on this to be the initial state of any bag that we use. Initial State of a Bag THIS BAG IS EMPTY.

  5. Numbers may be inserted into a bag. Inserting Numbers into a Bag I AM PUTTING THE NUMBER 4 INTO THE BAG.

  6. Numbers may be inserted into a bag. Inserting Numbers into a Bag THE 4 IS IN THE BAG.

  7. Numbers may be inserted into a bag. The bag can hold many numbers. Inserting Numbers into a Bag NOW I'M PUTTING ANOTHER NUMBER IN THE BAG -- AN 8.

  8. Numbers may be inserted into a bag. The bag can hold many numbers. Inserting Numbers into a Bag THE 8 IS ALSO IN THE BAG.

  9. Numbers may be inserted into a bag. The bag can hold many numbers. We can even insert the same number more than once. NOW I'M PUTTING A SECOND 4 IN THE BAG. Inserting Numbers into a Bag

  10. Numbers may be inserted into a bag. The bag can hold many numbers. We can even insert the same number more than once. NOW THE BAG HAS TWO 4'S AND AN 8.. Inserting Numbers into a Bag

  11. We may ask about the contents of the bag. HAVE YOU GOT ANY 4's ? Examining a Bag YES, I HAVE TWO OF THEM.

  12. We may remove a number from a bag. Removing a Number from a Bag THIS 4 IS OUTTA HERE!

  13. We may remove a number from a bag. But we remove only one number at a time. Removing a Number from a Bag ONE 4 IS GONE, BUT THE OTHER 4 REMAINS.

  14. Another operation is to determine how many numbers are in a bag. How Many Numbers IN MY OPINION, THERE ARE TOO MANY NUMBERS.

  15. Summary of the Bag Operations • A bag can be put in its initial state, which is an empty bag. • Numbers can be inserted into the bag. • You may count how many occurrence of a certain number are in the bag. • Numbers can be erased from the bag. • You can check the size of the bag (i.e. how many numbers are in the bag).

  16. C++ can be used to implement a container class such as a bag. The class definition includes: The bag Class class bag • The heading of the definition

  17. C++ classes (introduced in Chapter 2) can be used to implement a container class such as a bag. The class definition includes: The bag Class classbag { public: bag( ); • The heading of the definition • A constructor prototype

  18. The bag Class • C++ classes can be used to implement a container class such as a bag. • The class definition includes: classbag { public: bag( ); void insert(... void erase(... ...and so on • The heading of the definition • A constructor prototype • Prototypes for public • member functions

  19. The bag Class • C++ classes can be used to implement a container class such as a bag. • The class definition includes: classbag { public: bag( ); void insert(... void erase(... ...and so on private: }; • The heading of the definition • A constructor prototype • Prototypes for public • member functions • Private member variables We’ll look at private members later.

More Related