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

Buckys C++ Programming Tutorials – 55 – Introduction to Polymorphism
by
Tags:
Comments
47 responses to “Buckys C++ Programming Tutorials – 55 – Introduction to Polymorphism”
-
So I can use polymorphism to add unique moves to each Chess peice (Queen, King, Rook, Knight, Bishop, Pawn)?
-
This was far to easy for me, I understood every line of code you wrote
-
Nice explanation…Ty
-
"eating you doesn't kill you, it only does 25 damage"
-
Very helpful. Good teaching. May Jesus Christ bless you.
-
How does being eaten not kill you? seriously step back from the pipe 🙂
-
thanks
-
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;
} -
how exactly does eating you not kill you! wtf lol
This kind of stuff is what makes marathoning this tutorial much easier 😀 -
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) . -
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
-
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 ?
-
Couldn't the attack power be set inside the ninja and monster classes?
-
Ninja cop!? XD
-
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
-
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()?
-
which app do you use to make this programs?
-
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
-
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 subIn 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. -
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?
-
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 -
Isn't it better to make the attack function virtual ? Also id make the enemy instances shared pointers cuz they are never deleted
-
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();}
-
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 ?
-
I am a ninja chop!!!!!!!
-
Darn, I was dissapointed to see you didn't make a Public Enemy joke…
-
Now That Moster and Ninja Makes it more interesting! than boring variables and classes
-
you should never copy!
-
what if setAttackPower() is a virtual function and unique throughout all sub classes, can it still push the value into attackPower inside Enemy class?
-
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. -
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?
-
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 😮
-
what is the difference between this polymorphism and just override every attack(); in inherited classes
-
what if i have constructor , how can i use pointer
-
Why not Enemy *enemy1 = new Ninja(); ?
-
Hey can u tell me. Where can i find the c++ compiler u r using
-
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?.
-
You should !never copy/paste because you will make mistakes
-
Turn on the captions and read the first line :'D lol
-
Im a ninja and Im offended.
-
"a way of calling the same function and having many different outcome"
Sounds like hell to debug
Ive generally avoided polymorphism for this reason. -
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?
-
A game with ninja and monster (all togheter) would be a very fun idea actually.
-
you saved my life…!!!!!!1
-
Let me clear this up:
Here is a basic form of Bucky's codeNinja n; //Creates a ninja enemy
n.setAttackPower(29) //Sets damage
n.attack // Applies damageThat 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 damageThat'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 damageWell, 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. -
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 setpowerm.setdamage(4);
n.attack();
m.attack();system ("PAUSE");
} -
that ninja chop killed me xD
Leave a Reply