Can anyone explain the benefits of polymorphism?

So I really understand how this works, but I just can't figure out what makes it useful. You still need to define all the individual functions, you still need to create an instance of each object, so why not just call the function from this object and create the object by creating a pointer to the parent object and passing a reference to the derived objects, just to call the function? I do not understand the benefits of this extra step.

Why is this:

class Parent
{
    virtual void function(){};
};

class Derived : public Parent
{
    void function()
    {
    cout << "derived";
    }
};

int main()
{
    Derived foo;
    Parent* bar = &foo;
    bar->function();
    return -3234324;
}

vs this:

class Parent
{
    virtual void function(){};
};

class Derived : public Parent
{
    void function()
    {
    cout << "derived";
    }
};

int main()
{
    Derived foo;
    foo.function();
    return -3234324;
}

Do they do the same thing right? As far as I know, only one uses more memory and more confusion.

+5
source share
10 answers

, -.
function() , .

, , , , , .

?
- .

:

enter image description here

, , :

Shape *basep[] = { &line_obj, &tri_obj,
                   &rect_obj, &cir_obj};
for (i = 0; i < NO_PICTURES; i++)
    basep[i] -> Draw ();

line_obj, tri_obj .. Shape Line, Triangle .., Shape.

, , Rhombus, , Base class Shape. Base .

, Draw() , , ,

.

+15

, , - . , , , . , . , , . , .

, Segway . , - , , . , Segway-rider, , , Segway-riders, Segways, , , .

. , -, , . , . SMS-. , , - .

+6

, / , , - . , , . , "", . , .

+2

.NET Stream. , "FileStream", "MemoryStream", "GZipStream" .. , "Stream" "FileStream", .

+2

. , GUI. - :

class BaseWidget
{
...
virtual void draw() = 0;
...
};

. , , , . , , , ? , , GUI, , :

class ChildWidget
{
 ...
  void draw()
  {
     //draw this widget using the knowledge provided by this child class
  }
};


class ChildWidget2
{
 ...
  void draw()
  {
     //draw this widget using the knowledge provided by this child class
  }
};

, . , , (), . - :

for(int i = 0; i < numberOfWidgets; i++)
{
    widgetsArray[i].draw();
}

, ChildWidget1, ChildWidget2, TextBox, Button.

, .

+2

. , , .

. mode. , agnostic_function() , . , function().

, , , . . .

#include <iostream>
using namespace std;

class Parent
{
public:
    virtual void function() const {};
};

class Derived1 : public Parent
{
    void function() const { cout << "derived1"; }
};

class Derived2 : public Parent
{
    void function() const { cout << "derived2"; }
};

void agnostic_function( Parent const & bar )
{
   bar.function();
}

int main()
{
   int mode = 1;
   agnostic_function
   (
      (mode==1)
      ? static_cast<Parent const &>(Derived1())
      : static_cast<Parent const &>(Derived2())
   );
}
+2

, .

, : Vehicle > Car. Car SaloonCar, CoupeCar .. . , , Car. SaloonCar CoupeCar - , .

, ; IInternalCombustionEngine , , Garage (, , ). , . .

public abstract class Vehicle {..}

public abstract class Bus : Vehicle, IPassengerVehicle, IHydrogenPowerSource, IElectricMotor {..}

public abstract class Car : Vehicle {..}

public class FordCortina : Car, IInternalCombustionEngine, IPassengerVehicle {..}

public class FormulaOneCar : Car, IInternalCombustionEngine {..}

public abstract class PowerTool {..}

public class ChainSaw : PowerTool, IInternalCombustionEngine {..}

public class DomesticDrill : PowerTool, IElectricMotor {..}

, , FordCortina - Vehicle, , IInternalCombustionEngine ( , ), . .

+2

- . . Parent, , . . , , . .

+1

. . . ++ . , , . Python.

+1

, School educate(). , . . - , - ..

, , , . School , School.

Objects (, , ..) Ilearnable. School , .

public void Educate (ILearnable  anyone)

.

, . (PetSchool: School), , .

  • , , ,
  • .
  • .
  • Extension of the class (for example, after many years of training that you learn, hey, all those people who are around the School should go through the GoGreen program, where everyone should plant a tree in the same way. Here, if you have a base class all of these people like abstract LivingBeings, we can add a PlantTree invocation method and write code in PlantTree. Nobody should write code in their class, as they inherit from the LivingBeings class, and just attribute them to PlantTree, trees).
-1
source

All Articles