Hi All,
Here is the basic example of the using polymorphism. And then my question is below.
	So, in this example, instead of std::vector<Veh  icle*>, can I use std::vector<Veh  icle> and still achieve the polymorphism, like calling the function of the derived class object reference it contains??
Can you give an example of the same.
Thanks
rsennat
					Here is the basic example of the using polymorphism. And then my question is below.
Code:
	 typedef std::vector<Vehicle*>  VehicleList;
 
 void myCode(VehicleList& v)
 {
   for (VehicleList::iterator p = v.begin(); p != v.end(); ++p) {
     Vehicle& v = **p;  // just for shorthand
 
     // generic code that works for any vehicle...
     ...
 
     // perform the "foo-bar" operation.
     v.fooBar();
 
     // generic code that works for any vehicle...
     ...
   }
 }
===================
 class Car : public Vehicle {
 public:
   virtual void fooBar();
 };
 
 void Car::fooBar()
 {
   // car-specific code that does "foo-bar" on 'this'
   ...  ← this is the code that was in {...} of if (v is a Car)
 }
 
 class Truck : public Vehicle {
 public:
   virtual void fooBar();
 };
 
 void Truck::fooBar()
 {
   // truck-specific code that does "foo-bar" on 'this'
   ...  ← this is the code that was in {...} of if (v is a Truck)
 }
Can you give an example of the same.
Thanks
rsennat
 
	
Comment