C + + implementation of the headfirst design pattern-iterator (Iterator)

Source: Internet
Author: User

Iterator.h

1 #ifndef _iterator_h_2 #define_iterator_h_3 4#include"menu_item.h"5 6 classIterator {7  Public:8     Virtual BOOLHas_next () =0;9     VirtualMenuItem *next () =0; Ten }; One #endif

Menu_item.h

1 #ifndef _menu_item_h_2 #define_menu_item_h_3 4#include <string>5 6 classMenuItem {7 Private:8STD::stringname;9STD::stringdescription;Ten     BOOLVegetarian; One     DoublePrice ; A  Public: -MenuItem (ConstSTD::string&_name, -               ConstSTD::string_description, the               BOOL_vegetarian, -               Double_price): - name (_name), - description (_description), + Vegetarian (_vegetarian), - Price (_price) {} +      ASTD::stringGet_name () {returnname;} atSTD::stringGet_description () {returndescription;} -     DoubleGet_price () {returnPrice ;} -     BOOLIs_vegetarian () {returnVegetarian;} - }; - #endif

Pancake_house_menu.h

1 #ifndef _pancake_house_menu_h_2 #define_pancake_house_menu_h_3 4#include"menu_item.h"5#include"Iterator.h"6 7 classPancakehousemenu {8  Public:9     voidAdd_item (ConstSTD::string&name,Ten                   ConstSTD::string&description, One                   BOOLVegetarian, A                   DoublePrice ) { -         if(Item_num <Max_items) { -menu_items[item_num++] =NewMenuItem (name, description, vegetarian, price); the         } -     } -      -Pancakehousemenu (): Item_num (0), Iterator (* This) { +Add_item ("Name_1","descrption_1",true,4.5); -Add_item ("name_2","descrption_2",true,6.2); +Add_item ("Name_3","Descrption_3",false,3.5); A     } at      -~Pancakehousemenu () { -          for(inti =0; i < Item_num; i++ ) { -             DeleteMenu_items[i]; -         } -     } in   -Iterator *Get_iterator () { to         return&iterator; +     } -      the Private: *     class_iterator: PublicIterator { $     Private:Panax NotoginsengPancakehousemenu &menu; -         intPos; the      Public: +_iterator (Pancakehousemenu &_menu): POS (0), menu (_menu) {} A         BOOLHas_next () {returnPOS <Menu.item_num;} theMenuItem *Next () { +             returnmenu.menu_items[pos++]; -         } $ } iterator; $      -     Const Static intMax_items =6; -MenuItem *Menu_items[max_items]; the     intItem_num; - };Wuyi #endif

Main.cpp

1#include"pancake_house_menu.h"2#include <iostream>3 4 intMain () {5 pancakehousemenu menu;6Iterator *iterator =menu.get_iterator ();7      while(iterator->Has_next ()) {8MenuItem *menu_item = iterator->next ();9Std::cout << menu_item->get_name () <<" "Ten<< menu_item->get_description () <<" " One<< Menu_item->is_vegetarian () <<" " A<< Menu_item->get_price () <<Std::endl; -     } -}

C + + implementation of the headfirst design pattern-iterator (Iterator)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.