0

I studying c++.I am having problem in under standing run time polymorphism ? why do we need to use reference / pointer when same work can done by creating object of class?

`for eg:

class A{
private:
    int p;
public:
    A(){}   //ctor
    void Print(){}
    void display(){}
    void showrecord(){}
} 
class B:public A
{
Private:
     int q;
public:
    B(){}    //ctor
    void Print(){}  
    void displayrecord(){}
}

now in above case i can access any method of B class by using its object and A class method b using scope resolution then why to use pointer and assign object to it?

yogi
  • 53
  • 1
  • 6
  • 2
    Your example isn't using polymorphism. – juanchopanza Oct 07 '13 at 10:41
  • Did you mean to have `Print` and `print`? DId you mean to make any functions virtual? – doctorlove Oct 07 '13 at 10:42
  • Have a look at this answer: http://stackoverflow.com/a/9260064/1168156 ;) – LihO Oct 07 '13 at 10:43
  • we need complete example that shows how you *use* polymorphism. – Karoly Horvath Oct 07 '13 at 10:43
  • The point of polymorphism is that the correct method gets called even when you don't know at compile time what type of object you have. If you declare an object of type B then you do know at compile time what type you have. In this case there is no point in polymorphism. – john Oct 07 '13 at 10:43

3 Answers3

1

Lets say you've 3 different cars. And you've different mechanisms of driving them. The driver needn't know about the underlying engines but only the protocol of how to drive a car i.e. press this pedal for acceleration, press that pedal for brakes, etc.

Now from the driver's perspective, it doesn't matter if its Honda, Ford or Buick. From his viewpoint, it is just a car. Likewise, if you've shed, where cars are parked, you call them a car shed. It houses cars and isn't bothered about what make each one is. So

std::vector<Car*> v;
v.push_back(new Ferrari());
v.push_back(new Honda());
v.push_back(new Ford());

why do we need to use reference / pointer when same work can done by creating object of class?

Without a pointer or reference, you cannot create a collection of objects that have some commonality, but different in some specific sense. This is circumvented in some strict OOP languages like Java, C#, etc. by making all objects derive from a base class called Object. C++ is a multi-paradigm language and the programmer is free to make such decisions as appropriate to his/her project. A way to do it in C++ is via pointers of the base class. This idiom is called run-time polymorphism.

for (const auto &this_car : v)
     this_car->drive();

Here irrespective of the actual make, the vector v will be able to hold the car, as long as the base class car is part of the type. Likewise, drive will be different for each make, but that isn't required to be known for the function which calls it.

EDIT:

Thanks to nijansen for pointing out that this post doesn't actually answer the question: why pointers or references (dynamic types) are required for run-time polymorphism? it only says they've to used to achieve it but doesn't explain why can't we use ordinary (static type) variables.

C++ is designed in such a way that an object's type may or may not be known completely at compile-time with just the type in hand. In Circle c; we know c is of type Circle; whereas in Shape *p = make_shape(); we really do not know what object p is pointing to; p's own type is Shape* but its pointed-to object's concrete type is unknown. All we know is that it is pointing to some object which derives from Shape. Please do not confuse yourself with dynamic or automatic memory allocation; this is the same for automatic variables too E.g. Shape *p = &c;. Here too p in isolation the compiler doesn't know what concrete type the object is of, which p is pointing to.

Had we written p as a static (non-pointer, non-reference) type Shape p = make_shape(); or Shape p = c; what really happens in slicing i.e. p will be of the concrete type Shape and since it's not a pointer, it'll copy (Shape) part of Circle object c to it, which is mostly undesirable.

Without knowing the underlying type, how do we call the right concrete type's functions? C++ provides virtual functions to do the job. This is why runtime polymorphism is always explained with virtual methods in C++; while in languages like Java and C# all methods are virtual and all object types are references/pointers. In a way the answer to your question is, the language is designed such that one needs reference/pointer variables for runtime polymorphism.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • That's explaining polymorphism, but not this question: "why do we need to use reference / pointer when same work can done by creating object of class?" the possible duplicate I've marked covers this pretty well – nikolas Oct 07 '13 at 10:50
0

To give you a general example,

Please excuse the pseduocode. Will add a Cpp Code in some time, bit rusty now

Super Class

Class Animal
{

}

Sub Classes

Class Dog : public Animal
{

}

Class Cat : public Animal
{

}

Another class,

class Vet 
{
    void checkAnimal(recieve Animal)
    {

    }          
}

Now consider this usage,

vetObject.checkAnimal(dogObject);
vetObject.checkAnimal(catObject);

If it weren't for polymorphism your Vet Class would have been something like this,

class Vet 
{
    void checkAnimal(recieve Cat)
    {

    }          

    void checkAnimal(recieve Dog)
    {

    }          

     ....  and so on
}
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
0

Thereby you can do some generic processess to every object. For example you have Chicken : Animal class and you have Cow : Animal class , and they have common function doSlaughter(), and you have SlaughterHouseJob class. When you want to process slaughterHouseJob.doSlaughterAnimals() method, you can simply write this code:

for(Animal animal:animals){
    animal.doSlaughter(); //It can be chicken or Cow or other animals. Simply one function for all jobs
}
nikolas
  • 8,707
  • 9
  • 50
  • 70
ibrahim demir
  • 421
  • 3
  • 16