Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Base * bases[NUM_ITEMS];
- for(int i=0; i<NUM_ITEMS; i++) {
- int r = get_random_integer();
- if(r == 0)
- bases[i] = new Derived1;
- else if(r == 1)
- bases[i] = new Derived2;
- // ...
- }
- shared_ptr<Base> bases[NUM_ITEMS];
- for(int i=0; i<NUM_ITEMS; i++) {
- int r = get_random_integer();
- if(r == 0)
- bases[i].reset(new Derived1);
- else if(r == 1)
- bases[i].reset(new Derived2);
- // ...
- }
- std::vector< shared_ptr<Base> > bases;
- for(int i=0; i<NUM_ITEMS; i++) {
- int r = get_random_integer();
- if(r == 0)
- bases.push_back(shared_ptr<Base>(new Derived1));
- else if(r == 1)
- bases.push_back(shared_ptr<Base>(new Derived2));
- // ...
- }
- Derived1 * d = &dynamic_cast<Derived1&>(*bases[x]);
- shared_ptr<Derived1> d = dynamic_pointer_cast<Derived1>(bases[x]);
- if(d) {
- // conversion successful, it pointed to a derived. d and bases[x] point still
- // to the same object, thus share it.
- }
- interface IShape
- {
- double GetArea();
- }
- class Square : IShape
- {
- double GetArea()
- {
- return sideLengh*sideLength;
- }
- ...
- }
- class FunnyShape : IShape
- {
- //do funny stuff
- ...
- }
- ...
- void calcTotalArea(List<IShape> shapes)
- {
- double total = 0.0;
- foreach (IShape s in shapes)
- {
- total += s.GetArea();
- }
- return total;
- }
- class bankAccount
- {
- int money;
- bankAccount() {};
- int MoneyInAccount() {return money;};
- }
- class savingsAccount : public bankAccount
- {
- int SavingsInterest();
- }
- class MoneyMarket : public bankAccount
- {
- int CalculateAllPastReturns();
- }
Add Comment
Please, Sign In to add comment