Buckys C++ Programming Tutorials – 55 – Introduction to Polymorphism

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

47 responses to “Buckys C++ Programming Tutorials – 55 – Introduction to Polymorphism”

  1. John Brown Avatar

    So I can use polymorphism to add unique moves to each Chess peice (Queen, King, Rook, Knight, Bishop, Pawn)?

  2. Yu Zhao Avatar

    This was far to easy for me, I understood every line of code you wrote

  3. Pravin Patil Avatar

    Nice explanation…Ty

  4. ImaginaryHuman072889 Avatar

    "eating you doesn't kill you, it only does 25 damage"

  5. COMB0RICO Avatar

    Very helpful. Good teaching. May Jesus Christ bless you.

  6. Robert Marlin Avatar

    How does being eaten not kill you? seriously step back from the pipe 🙂

  7. ramana naidu Avatar

    sir i am getting garbage value some 168425678,-965335677 in output for both areas
    #include <iostream>
    using namespace std;

    class Shape {
    protected:
    int width,height ;
    public:
    val( int a , int b) {
    width = a;
    height = b;
    }

    };

    class Rectangle: public Shape {
    public:

    int area () {
    cout << "Rectangle class area :" <<endl;
    return (width * height);
    }
    };

    class Triangle: public Shape{
    public:

    int area () {
    cout << "Triangle class area :" <<endl;
    return (width * height / 2);
    }
    };

    // Main function for the program
    int main( ) {

    Rectangle rec;
    Triangle tri;

    // store the address of Rectangle
    Shape shape1;
    shape1.val(10,2);

    cout<<rec.area()<<endl;
    cout<<tri.area()<<endl;

    return 0;
    }

  8. Weyran Avatar

    how exactly does eating you not kill you! wtf lol
    This kind of stuff is what makes marathoning this tutorial much easier 😀

  9. Sara Tokic Avatar

    Why don't we just write n.setAttackPower(895); or
    m.SetAttackPoWer(204); instead of writing it with pointers:
    Ninja n;
    Monster m;
    Enemy *s=&n;
    Enemy *x=&m;
    s->setAttackPower(895);
    x->SetAttackPoWer(204);???
    Because Ninja and Monster have inherited everything from enemy(there's nothing private in enemy) .

  10. zablablukas Avatar

    Uhm so what's the point of polymorphism? Couldn't I just not make pointers and directly call n.attackPower(23); m.attackpower(56) and then n.attack(); m.attack() ? It would be more simple that way

  11. Burak Yildirim Avatar

    im about to go crazy :'( can someone please explain how the function 'setAttackPower' can assign a value while working with pass-by-value style ? shouldn't it only use the copy of the variable ?

  12. Farine Avatar

    Couldn't the attack power be set inside the ninja and monster classes?

  13. Level Gen Avatar

    i pause you video on 9:59 and write every thing you wrote and i run it error pop up and i spend my 1.5 house to solve this but then i realize that video is not finish yet i click play and then i saw that attackpower problume

  14. Vayp Nishon Avatar

    shouldnt the data member be private and can still be accesed through inheritance if you use a get function in the cout derived classes? so instead of protected and then trying to do cout << " " << data, do private and then cout << " " get()?

  15. mira ilunga Avatar

    which app do you use to make this programs?

  16. Shahir ABD Avatar

    if i make an attack function for each class and in main function make two objects called ninja and monster and then write ninja.attack() and monster.attack(), will that be a polymorphism

  17. GameDev Now Avatar

    You instantiated a ninja, which is of type subclass (Ninja)
    Ninja n;

    But then you created a pointer which, even though is of base type (Enemy) you made it point to the subclass(Ninja)
    Enemy *enemy1 = &n;

    Then using the pointer, modified the ninja's attack power
    enemy1->attackPower(29);

    Then you called attack on the ninja instance.

    if we create an Enemy pointer and point it to an Enemy instance then call setAttackPower the changes
    will affect the Enemy instance but will not be reflected on the subclass ninja, but the tricky part was that
    even though the pointer is of base class(Enemy) you pointed to subclass(Ninja), making the base modify the sub

    In other words, the ninja attacked but was his mother who set his attackPower.
    How cool is that! Bucky you are the one who is the ninja.

  18. volikoto Avatar

    Does this mean that the enemy1 and enemy2 are the objects of the base class Enemy, and it pointed to the address of objects n and m? In that case the n object inherits the enemy1 as its base and then applies its changes?

  19. G Avatar

    Followed the guide but
    When i created my ninja "attack" function i got an error.
    What was happening was that since the protected variable was named attack, i couldnt use attack as the name of the function .
    This was the error i got. Ninja::attack': function call missing argument list

  20. Stoyan Lupov Avatar

    Isn't it better to make the attack function virtual ? Also id make the enemy instances shared pointers cuz they are never deleted

  21. Weichean Chong Avatar

    can i use this way? cause i dont like pointer
    #include <iostream>
    using namespace std;

    class enemy
    {
    protected:
    int attackpower;
    public:
    int attack(int a)
    {
    attackpower=a;
    }
    };

    class ninja:public enemy
    {
    public:
    int ninjaattack()
    {
    cout<<"ninja chop!"<<attackpower<<endl;
    }
    };

    class bear:public enemy
    {
    public:
    int bearattack()
    {
    cout<<"bear bite!"<<attackpower<<endl;
    }

    };

    int main()
    {

    ninja ninjao;
    bear bearo;
    ninjao.attack(99);
    bearo.attack(199);
    ninjao.ninjaattack();
    bearo.bearattack();

    }

  22. Essam Gouda Avatar

    I get why its allowed to type Enemy *enemy1 = &n; but why do we even use it ? why did we thought about using pointers and addresses not equalizing them normally ( although I tried Enemy enemy1 = n;) and it didn't give me an error but didn't work also and I know you can't just equalize objects like that but I am confused by the idea of using pointers in here ?

  23. JA10 Avatar

    I am a ninja chop!!!!!!!

  24. Brian Carducci Avatar

    Darn, I was dissapointed to see you didn't make a Public Enemy joke…

  25. Talha Studios 2 Avatar

    Now That Moster and Ninja Makes it more interesting! than boring variables and classes

  26. michal234486 Avatar

    you should never copy!

  27. yu jie ng Avatar

    what if setAttackPower() is a virtual function and unique throughout all sub classes, can it still push the value into attackPower inside Enemy class?

  28. steve122288 Avatar

    if you use operator overload you have to use the pointer deference:
    ( * enemy1 ) << x ; // (this example has the << operator overloaded)
    Either that or you have to use the operater overload function's full name:
    enemy1 -> operator << ( x ) ;
    I take it this is because the arrow member selection operator will access the pointed object like the dot operator would normally. Either that or something to do with two operators next to one another that are supposed to be incompatible. It appears they are both called member operators.
    I noticed you can actually include a space when writing the function operator <<, or operator +, ect. It makes me wonder how this function is really named or if it has a name. Considering it is a keyword and an operator. It might be some kind of C++ thing.

  29. Tech A.M Avatar

    If the class Ninja is the derived class of class Enemy, would it make any difference if you wrote:

    n.attackPower(29)

    Instead of creating a pointer and all that confusing crap? I mean, ninja has access to all functions that are inside of the enemy class, right? What am I missing here?

  30. Brad Rollings Avatar

    My only problem is that when i create pointer to attack power it doesn't display it only let's me use setattackpower for it why is this? I thought that a derived class cannot point at a base class but a base class can point at derived classes 😮

  31. Zheng SUn Avatar

    what is the difference between this polymorphism and just override every attack(); in inherited classes

  32. minh nguyen Avatar

    what if i have constructor , how can i use pointer

  33. Emanuel Miroiu Avatar

    Why not Enemy *enemy1 = new Ninja(); ?

  34. D3FY -_- Avatar

    Hey can u tell me. Where can i find the c++ compiler u r using

  35. Artem Sherbachuk Avatar

    If I add 'attack' with default implementation to the base class Enemy, and then will override it in Subclasses with target implementation, it polymorphism? I`m right?.

  36. Westonator5000 Avatar

    You should !never copy/paste because you will make mistakes

  37. KINSLAYER Avatar

    Turn on the captions and read the first line :'D lol

  38. Stav7 Avatar

    Im a ninja and Im offended.

  39. Sean Halnon Avatar

    "a way of calling the same function and having many different outcome"

    Sounds like hell to debug
    Ive generally avoided polymorphism for this reason.

  40. TheDukeGreat Avatar

    How can I call through enemy1 or enemy2 the attack() method? Compiler says to me that class Enemy does not contain attack() method, whether i call it like enemy1->attack(), or enemy[0].attack(). Can someone explain to me what I am doing wrong?

  41. Renato Lopes Avatar

    A game with ninja and monster (all togheter) would be a very fun idea actually.

  42. vipul agarwal Avatar

    you saved my life…!!!!!!1

  43. TheKoopaKingdom Avatar

    Let me clear this up:
    Here is a basic form of Bucky's code

    Ninja n; //Creates a ninja enemy
    n.setAttackPower(29) //Sets damage
    n.attack // Applies damage

    That code would work, but, when you are programming, you want your code to be fast, and efficient. Pointers are faster, because of how they are used in the computer.

    Ninja n; //Creates a ninja enemy
    Ninja *np = &n //Creates pointer to ninja enemy
    np->setAttackPower(29) //Sets damage
    np->attack // Applies damage

    That's all well and good, but why do we have to get the enemy class involved?

    Ninja n; //Creates a ninja enemy
    Enemy *e = &n //Pointer stuff
    e->setAttackPower(29) //Sets damage
    n.attack // Applies damage

    Well, as crazy as it sounds, it's easier to deal with, because of virtual functions.
    I promise you, watching the next video will clear up some of your concerns.

  44. Ahmad El Sayed Avatar

    I did it this way and it worked fine … Why did he create enmey1 obj and enemy2 obj?
    int main() {
    ninja n;
    monster m;

    n.setdamage(2);
    \set damaeg is the same as setpower

    m.setdamage(4);

    n.attack();
    m.attack();

    system ("PAUSE");
    }

  45. Abir Hossen Avatar

    that ninja chop killed me xD

Leave a Reply

Your email address will not be published. Required fields are marked *