Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- using namespace std;
- class Shop
- {
- string name;
- public:
- Shop() { }
- Shop(string n): name(n) { }
- void getShopName() {cout << name << endl;}
- };
- class ASubject
- {
- private:
- vector<Shop* >list;
- /*
- This function is finding the element in the vector
- if the element is available in the array it return the position( index number )
- of that element.
- if element is not available it return -1.
- */
- int get_position(Shop *s)
- {
- for(unsigned short i = 0; i < list.size() ; i++)
- {
- if( list[i] == s )
- return i;
- }
- return -1;
- }
- public:
- void attach(Shop *shop)
- {
- list.push_back(shop);
- }
- /*
- for unsubscribe the shop from the notification list.
- */
- void detach(Shop * shop)
- {
- int index = get_position(shop);
- if (index == -1){
- cout << "element is not found in the list " << endl;
- }
- else
- list.erase(list.begin()+index);
- }
- void notify(float price)
- {
- // todo : notify function is yet to implement.
- }
- void print_vector()
- {
- /* old style of for loop with stl*/
- //for(unsigned int i = 0 ; i < pe.size() ; i++)
- // cout << pe[i] << " " ;
- for(Shop *x : list)
- cout << x << " " << endl; ;
- cout << endl;
- }
- };
- int main()
- {
- ASubject a ;
- Shop s1;
- Shop s2, s3, s4, s5, s6, s7, s8, s9;
- a.attach(&s1); // 0
- a.attach(&s2); // 1
- a.attach(&s3); // 2
- a.attach(&s4); // 3
- a.attach(&s5); // 4
- a.attach(&s6); // 5
- a.attach(&s7); // 6
- a.attach(&s8); // 7
- a.print_vector();
- a.detach(&s5);
- a.print_vector();
- a.detach(&s4);
- a.print_vector();
- a.detach(&s1);
- a.print_vector();
- a.detach(&s8);
- a.print_vector();
- a.detach(&s9);
- a.print_vector();
- return 0;;
- }
- ====================Output====================
- sumitmac:RevisionCpp sumit$ ./a.out
- 0x7ff7b5aa3858
- 0x7ff7b5aa3830
- 0x7ff7b5aa3818
- 0x7ff7b5aa3800
- 0x7ff7b5aa37e8
- 0x7ff7b5aa37d0
- 0x7ff7b5aa37b8
- 0x7ff7b5aa37a0
- 0x7ff7b5aa3858
- 0x7ff7b5aa3830
- 0x7ff7b5aa3818
- 0x7ff7b5aa3800
- 0x7ff7b5aa37d0
- 0x7ff7b5aa37b8
- 0x7ff7b5aa37a0
- 0x7ff7b5aa3858
- 0x7ff7b5aa3830
- 0x7ff7b5aa3818
- 0x7ff7b5aa37d0
- 0x7ff7b5aa37b8
- 0x7ff7b5aa37a0
- 0x7ff7b5aa3830
- 0x7ff7b5aa3818
- 0x7ff7b5aa37d0
- 0x7ff7b5aa37b8
- 0x7ff7b5aa37a0
- 0x7ff7b5aa3830
- 0x7ff7b5aa3818
- 0x7ff7b5aa37d0
- 0x7ff7b5aa37b8
- element is not found in the list
- 0x7ff7b5aa3830
- 0x7ff7b5aa3818
- 0x7ff7b5aa37d0
- 0x7ff7b5aa37b8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement