I will say this. This guy is awesome I have a little more programming experience than he does but, the way he is explaining things is not only clear BUT RIGHT. I will always see videos, even from college professors, that explain things that are all WRONG. This guy deserves credit and his videos are a great reference or a resource if i need a refresher. Thanks Bucky Keep At It.,
@TheK00paKingdom9 жыл бұрын
*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.
@parikrama889 жыл бұрын
+TheKoopaKingdom Awesome explanation buddy!! Gr8 help :)
@sweeseen95778 жыл бұрын
commended
@sethiojas8 жыл бұрын
+TheKoopaKingdom Dude I had the same thing in mind. Why use enemy class. Cheers!
@ReddoX305 жыл бұрын
Thank you for the explanation, it cleared things up a little!
@lilweez21754 жыл бұрын
Nigga, nobody understands pointers bitch
@puffbluesam02110 жыл бұрын
Sadly, my professors didn't teach us these techniques or concepts. Thank you so much Sir Bucky! You deserve a tuna coming from Tina! :)
@1mol8312 жыл бұрын
Python feels easier but then this runs faster. Also struct in C is more straightforward. But I could see the appeal of class. (Though why can’t I just write a library of functions instead?)
@Thriving_in_Exile11 жыл бұрын
This one in particular was EXTREMELY useful. Thanks, boss. Aaaaaaaaaand let's just go ahead and end line.
@alexisperez458110 жыл бұрын
Wow I learned polymorphism in 10 minutes. Thank you sir
@phe200312 жыл бұрын
@allaboutmath, not necessarily true. The special case is that both the Base and Derived classes have the same function which is declared "virtual" in Base. Then we can use a Base pointer to point to either a Base object or Derived object. Which version of the function will be called depends on the "dynamic" object which can only be detected at "runtime".
@JrJreet13 жыл бұрын
this is probably the most usefull c++ tutorial to me in this playlist until now lol
@kashmutt13 жыл бұрын
After this video, I went back and took a good look at pointers. I don't think I could have understood it without watching your tutorials. Thanks a lot!! :D
@hamsalak97793 жыл бұрын
10 years ago and yet such a high quality. Tnx
@moonquartzs3 жыл бұрын
If Bucky didn't exist, I'd fail my labs and exams. THANK YOU BUCKY!
@nasriwidaatalla67114 жыл бұрын
Bucky is a great programming instructor. You can use enemy1 and enemy2 if they were Ninga and monster type respectively. Also you can access the function by pointer (if it was a Ninga type for example) by typing enemy1->attack();
@Javii966 жыл бұрын
This was a perfect illustration of polymorphism for me to understand. Thanks!
@fidahussain63664 жыл бұрын
Sir Bucky, these are nodoubt The Best! c++ tutorials. Love you 💕
@DFsdf3443d9 жыл бұрын
Im a ninja and Im offended.
@eeeflying86327 жыл бұрын
Thats because ninjas are pussies
@mr.marmot397 жыл бұрын
because you're "public" enemy
@bredmond8126 жыл бұрын
Yeah! I am a monster, and I feel the same way.
@b784183646 жыл бұрын
you are a public enemy number 2
@danaadalaide56486 жыл бұрын
There is always one.
@ImaginaryHuman0728897 жыл бұрын
"eating you doesn't kill you, it only does 25 damage"
@BrandonGamingTV10 жыл бұрын
I still don't get the point. Instead of making pointers, why don't you just write: n.setAttackPower(29); m.setAttackPower(99); and then call n.attack() and m.attack() in the main. What is the point of the pointers?
@krgrief10 жыл бұрын
in this example, just to familiarize viewers with them. eventually, when you're passing class objects to functions, you don't want to pass the entire object by *value* (because it's usually larger than a pointer), so you use pointers. it's faster, albeit more confusing.
@yangzhang368610 жыл бұрын
krgrief hello I also have this question. As you said, using polymorphism transfers a pointer to functions instead of the entire object, but couldn't we transfer the reference to the functions? e.g. in the nijia example, I don't use polymorphism, but transfer &n to functions? thank you! I really want to know where polymorphism is good.
@krgrief10 жыл бұрын
Polymorphism isn't passing an object to a function using a pointer. That's "pass by pointer". Polymorphism is using a member function of the parent class to access private members in a derived class, if that makes sense.
@yangzhang368610 жыл бұрын
krgrief Yes that makes sense! But what if child class has its own extra members such as: class monster: public enemy { public: int extraMember; }; I tried to access extraMember (which is not in parent class enemy), but it failed. does that mean if i want to access child class's members(which mother class doesn't have), I will not be able to use polymorphism? Or in other words, is polymorphism only limited to the situation that child class doesn't have its own members other than the ones inherited from parent class? Thank you so much!
@krgrief10 жыл бұрын
Yang Zhang How did you try accessing extraMember?
@skippyscourge18112 жыл бұрын
I have a question.... i've tested this, and i've found that n.setAttackPower(29); n.setAttackPower(99); works just fine, and replaces the 4 lines of code of our two enemy objects. I understand that we're learning about polymorphism, but what are the advantages of doing it this way, when it seems like c++ already handles it automatically?
@maxim25o24 жыл бұрын
I thing there is an answer why He use pointers.. kzbin.info/www/bejne/pJXdZHh-pat4kMU
@sam-u-el4 жыл бұрын
@@maxim25o2 memory management
@codeforkrishna3 жыл бұрын
pointers are faster and lead to better memory management, if you wanna reduce the number of lines, then use the "new" keyword to assign a new memory address to the pointer of the base class(which points at an object of the child class) in the following manner: mother *obj1 = new daughter; //"daughter" class inherits from "mother" class
@illyakoshkin37719 жыл бұрын
probably repetitive question, but why you need to create an Enemy obj, since this is a base class and Ninja will get everything from Enemy anyways? why not just say: Monster m; m.setAttackPower(90); m.attack(); instead of: Monster m; Enemy *e = &m; e->setAttackPower(90); m.attack(); or if you want to use pointers so badly, why not Monster m; Monster *mp = &m; mp->setAttackPower(90); mp->attack(); why would you choose to confuse us? ;)
@acqbilal165 жыл бұрын
This is exactly what I was thinking. What difference does this make?
@alexandrufilipescu13015 жыл бұрын
A bit late but maybe it is because this way he can have multiple enemies with different attacks?
@naniwho46915 жыл бұрын
It is because for an example might want to strore it in an array[ ] of all kinds of enemies(ninjas, monsters, etc..) And because vector is a great dynamic array, and it is very easy to use and learn, i will give you and example with how you can use it in vector array.... So basically this will happen: vector EnemyArray; for(int i = 0; i < EnemyArray.size(); i++) { // In that 'for loop', you can access all of your Monsters and Ninjas, thru the Enemy pointer. // You can make an 'if statement', that says for example: if (attackpower > 60) // cout ShowNameOfEnemy() // There is no ShowNameOfEnemy function, but you could type one. Basically this is the main idea, // to be able to just access all your objects from a single array. }
@chosen_x_create77084 жыл бұрын
exactly my point , the calling and passing by address was not necessary , should have just called it through the objects and function
@RoninXsumari10 жыл бұрын
Awesome definition of polymorphism!! Thankyou!!
@briancarducci63518 жыл бұрын
Darn, I was dissapointed to see you didn't make a Public Enemy joke...
@JauVi857 жыл бұрын
:D
@jcinaty894 жыл бұрын
Thank you sooooo much bro. These examples are exactly what i need! you da best
@andyc.58786 жыл бұрын
Im watching this video 7 years from when it was released
@SomeDSquares4 жыл бұрын
9 years now m8
@dsdcp3 жыл бұрын
@lazarpuppet almost 10 years now.
@pedrobenitez21913 жыл бұрын
thanks bucky from c++ student at Johns Hopkins
@NikorasuChan12 жыл бұрын
Wait what? One day tutorial 1 -> tutorial 55? O.o LETS PARTAY! XD I'm not confused of all of the topics from 1-55 :D
@abhisheksamboddu65804 жыл бұрын
cool dude i took 2 days ..haha
@Vertigo60007 жыл бұрын
Mentions video games, immediately draws all of my attention. Lol
@AnonyMouse31612 жыл бұрын
Don't quote me on this, but from what I have heard from a teacher of mine is that if possible, use as many pointers as possible. He said that it is much more efficient to pass by reference rather than to pass most values.
@2coolrun3087 жыл бұрын
I don't mean to be a dick here, but this is WAAAAAAAY better than my professor who can't even speak English.
@letmecommentalready12 жыл бұрын
you use pointers because in larger programs, you will be able to use all of your objects functions in a loop instead of individual statements for each one. something like creating an array of pointers to print out all of your classes getFunctions. you declare the pointer array like baseClass * myArray[3] = {&one, &two, &three} then you just put it in a loop to print it all out
@kashmutt13 жыл бұрын
You have got some wild imagination bucky! I love your tutorials!!! :D
@emad-man52274 жыл бұрын
Yo you still alive?
@Inshaine10 жыл бұрын
I uh... just made four Ninja objects (bruceLee -- not a ninja, I know -- donatello, snakeEyes, greyFox), and four Monster objects (freddyKruger, jasonVoorhees, creeper, bettyWhite) and made a new function to give them names that prints out "I am (name), ninja chop!" and "(name) must eat you!"... Thank you, Bucky! This is awesome!
@jrM54926 жыл бұрын
after made it through the 51 video, man it goes smooth
@curtislbyrd13 жыл бұрын
I really love the way you tell it man..no doubt!!
@sammaine695310 жыл бұрын
Cleared up everything in 10 minutes, instead of sitting through a whole lecture.
@Shegg7713 жыл бұрын
@Cezarijus He was showing you important details about polymorphism which allow you to make a base class Pointer which point to inherited.
@webnavigator00011 жыл бұрын
thanks for explaining. How come you don't use New keyword? I really don't know when do we need to use New keyword to instantiate object.
@CrunchyFishy12 жыл бұрын
actually, it does not matter where you put your braces it's personal preference. In PHP and Java I prefer to write: function(){ } were as in C++, I prefer to write in: void function() } } ONLY because of the extra stuff that may be added to the end before the opening brace.
@mastermax777713 жыл бұрын
great tutorial bucky !!! love it ...
@SCHUCH51213 жыл бұрын
Feeling some C++ Game Development videos.
@SirRobbStarkGamin10 жыл бұрын
So Bascally what Polymorphism is, when you have a parent class that has multipal child class, and the parent class will take different forms (poly = many, morpic = forms) accordingly to the child classes. Just like how enemy changed for the ninja and monster. This basically makes coding a lot less and cleaner. since we can use one parent class over and over again.
@mohhamedrafi476310 жыл бұрын
By they that thing buckys doing with the class for example public :
@mohhamedrafi476310 жыл бұрын
mohhamed rafi dam youtube back to what i was saying it like public : void attack(int a) {cout
@yangzhang368610 жыл бұрын
so does that mean it make the codes cleaner? e.g. it is easy to tell that enemy1, enemy2, enemy3 are from the same base class, whereas it is harder to tell if nijia, monster are from the same base class? thank you.
@mohhamedrafi476310 жыл бұрын
Yeah it dose. If you are a program you should try to keep your program clean as possible so other people can have a easy time reading it.
@mohhamedrafi476310 жыл бұрын
Wow I need spell check. So if you did not get what i mean if you are a programmer you should try to keep your code clean
@martinsvk1711 жыл бұрын
Yep, youre right... 1st time I see Buckys made a mistake a little bit. The origin of polymorhism would be clear when you do it like this : You make a function attack also in you Enemy class. Then you make Enemy e1 = &ninja and the same way for monster. And then you can call maybe in a for each loop for all enemy instance just e1.attack(), e2.attack() and all the attacks do their own stuff and they call the same, i think it also call method overriding (in java).
@essamgouda16098 жыл бұрын
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 ?
@lechi_20028 жыл бұрын
This might help you: stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself
@essamgouda16098 жыл бұрын
Thank you so much !!!!!
@talhaahmed38958 жыл бұрын
Now That Moster and Ninja Makes it more interesting! than boring variables and classes
@NikorasuChan12 жыл бұрын
I learnt most of it beforehand in another language... and its a quite easy transition... so its a no worry for me ;)
@user-og6ol2im7v8 жыл бұрын
Couldn't the attack power be set inside the ninja and monster classes?
@user-og6ol2im7v8 жыл бұрын
***** thank you very much
@bloggerswork8996 жыл бұрын
@Don't Touch Me Simp
@veljko100able5 жыл бұрын
what's difference between video and this: Ninja n; Monster m; m.setattackPower(29); n.setattackPower(10); n.attack(); m.attack();
@j.s.534 жыл бұрын
Why is it printing garbage value for me?...
@J0nDaFr3aK11 жыл бұрын
hey all, everything's clear to me but why the pointers? why not just access the setAttackPower() function from the objects itself? like, n.setAttackPower(x) and m.setAttackPower(y). wouldn't this work? the pointers here seem like no use to me here
@avi_mukesh8 жыл бұрын
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?
@avi_mukesh8 жыл бұрын
*setAttackPower
@Thelouqman11 жыл бұрын
Thanks , its really helpful
@zablablukas8 жыл бұрын
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
@SW-hx4rl8 жыл бұрын
zablablukas exactly what I'm wondering too... can anyone explain?? Why call the functions on pointers rather than objects themselves??
@MrSonikProd8 жыл бұрын
yeah was wondering too
@ceriwestcott87848 жыл бұрын
lets say you have a Class Person and 2 sub classes Female/Male with the methods "printSex". You can make objects like Person p = new Male(); Person p1 = new Female(); Vector persons = new Vector(); for(int i =0; i < persons.size(); i++){ cout
@ahmeddeghady45105 жыл бұрын
Why there was pointers in the code? was it really needed?
@gauravskumar56734 жыл бұрын
Why use pointer? Is there any advantage of using pointer?
@donovantheprogrammer29894 жыл бұрын
Why do a pointer to Enemy and not just a pointer to Ninja?
@asadmehmoodleo12 жыл бұрын
can you please tell me the compiler name ?
@J0nDaFr3aK11 жыл бұрын
Yeah i didn't get why he used pointers. so it can basically be done without them, right? what's the use of pointers here?
@giancarlo4262 жыл бұрын
"All ninjas are enemies ... we already know that." Lmao.
@muneebmuhammad16086 жыл бұрын
why cant we replace the pointers with normal declaration.
@seigeengine12 жыл бұрын
You use pointers to keep a reference to an object. Otherwise you're always passing by value. AKA: Making copies. Also memory tricks, etc. etc.
@ismailelshafey10033 жыл бұрын
it's 2021. This ladies and gentlemen is how to make passive income
@abirhossen53089 жыл бұрын
that ninja chop killed me xD
@mmykandil13 жыл бұрын
ok I understood the polymorphism idea from the Java tuts but now this video missed it up :(
@letmecommentalready12 жыл бұрын
"Also, I really don't know why he used pointers for this." Yea, sounds like I added a lot of stuff you already knew.
@khasha61664 жыл бұрын
Buckys C++ Programming Tutorials 2011 >>>>>>>>> University Professors 2021 :)))))
@51AD312 жыл бұрын
Why didn't he set the attack power of Ninja's and Monsters within their derived classes? I expect that they would always have the same attack power, so why set them inside of main?
@jeffreydilley33807 жыл бұрын
are we not worried about slicing because you're passing the address ? wouldn't having the parent class be set as the child class lose the uniqueness of the child ?
@seigeengine12 жыл бұрын
That is basically exactly what I said. Adding redundant information is not useful.
@basslahat3876 жыл бұрын
really helpful thankyou.
@j3froc635 жыл бұрын
setAttackpower was never used??????? Only made in the base class. And why would you want to make the pointers to monster and ninja of type enemy? Hasn't the classes ninja and monster already inherited the member variables/functions from class enemy?????? How are you able to set the attackpower a certain value for each object without using the setAttackpower function in the first place??????????????????????????????????????????????
@Canbay125 жыл бұрын
absolutely right, because he declared attackpower as protected, the child classes inherited the attribute of the parent class directly,which makes implementing a set function and displaying the usage of polymorphism on this example unnecessary ...
@j3froc635 жыл бұрын
I still don't get polymorphism. Shit video. I don't know where else to find a good video that explains the concept properly with a good example. FFs
@KishoreG23965 жыл бұрын
@@j3froc63 10 months late, but here we go: 1.) attackPower can be used within derived classes and the base class method by itself without having to use setAttackPower(). This is because attackPower is a protected variable and you can access it within the class heirarchy (i.e. within their methods) but not outside of it. 2.) the reason you would want to make a pointer of type Enemy rather than a pointer of monster or ninja is to be able to use that data for ALL classes that are enemies. For example, lets say you have a function like this outside of the class: void DoSomethingWithEnemy(Enemy* myEnemy); If Enemy* were instead Monster*, that means you cannot pass an object of type Enemy or of type Ninja. The same would be true if it were a Ninja*. Thus, having it as an Enemy* means you can pass all pointers that are Enemies. Since both Monster and Ninjas are Enemies, they can be passed to an Enemy*, but an Enemy is not a Ninja and a Ninja is not a Monster.
@danagamer11 жыл бұрын
You don't really need to use pointers for this? to work, right
@cybrhckr7 жыл бұрын
SoloLearn app in the playstore used the exact same example in their virtual functions course. Did you know that?
@vardaan_agarwal3 жыл бұрын
Still helping in 2022 ❤❤
@maileong55115 жыл бұрын
good explaination!
@areola_ayatollah12 жыл бұрын
Is there any reason not use pointers here and write out like so? : Ninja enemy_ninja; Monster enemy_monster; enemy_ninja.setAttackPower(80); enemy_monster.setAttackPower(30); enemy_ninja.attack(); enemy_monster.attack(); I am struggling to find a use case for pointers...
@giancarloandrebravoabanto70915 жыл бұрын
so you use the base class to manipulate its derived classes.
@shatley12312 жыл бұрын
i'm just learning c++ but i think it's suppose to work as like a way of organizing for example we might have a Vehicle Class, then maybe we want to have a Car Class and a Truck Class. the Car Class and the Truck Class can have the same stuff from Vehicle Class but they also have there own features. eh but what do i know
@elaineschmidt533811 жыл бұрын
Is there a performance difference in using pointers to set the attack power instead of just using n.setAttackPower(29)?
@SpomenkoJabucar12 жыл бұрын
Top notch tutorials.
@dannytheman22177 жыл бұрын
So what was polymorphism exactly? Was it use of a class within a class ? I followed along and havent gotten to the portion of the book about polymorphism, but seems simple enough
@zunpre13 жыл бұрын
Why not do "n.setAttackPower(29); m.setAttackPower(99);"?
@tryingmybest20611 жыл бұрын
I really don't understand. Please help! I thought that if you go Enemy *enemy1 = &n; it would make the pointer point to the ninja object, basically making it the ninja object. But then what did the fact that it was an Enemy pointer change about it? Did it change anything? Basically I'm asking why he used this line, and could not have just used the n object, and what it changes when a pointer is a specific type of class.
@minhnguyen-te4eb9 жыл бұрын
what if i have constructor , how can i use pointer
@donovantheprogrammer29895 жыл бұрын
Put on your thinking caps for this one: "Member Enemy::attackPower is inaccessible". attackPower is protected
@SAURAV1403406SAXENA6 жыл бұрын
Can we typecast the Objects into parent class Public??
@skyworm800611 жыл бұрын
Do you think using a loop is a good practice for making something such as a text-based game that is turn-based?
@skyworm800611 жыл бұрын
Yes, I don't even know why I wrote this. 'Tis sort of obvious isn't it?
@volikoto8 жыл бұрын
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?
@CrunchyFishy12 жыл бұрын
So can someone explain why I would want to do this instead of just creating a variable int attackPower inside my Ninja and Monster class. I understand the need for inheritance for the functions but why not just make it simple and skip the pointer use
@ArslanGhaffar9 жыл бұрын
for this tutorial y access modifier isn't used ?
@DebaucherousDaniels12 жыл бұрын
As always a life saver -
@youngslyyme11 жыл бұрын
@7:07 what does -> mean in c++? Is there another alternative for using -> in your code?
@AhmedKhashabaa11 жыл бұрын
you can use (*x).y=0; instead of using x->y=0;
@ujjwalmainali392711 жыл бұрын
Ahmed Khashaba (*x).y=0 in this case you are dereferencing a memory address and is equivalent to y=0 however x->y=0 is not deferencing but actually place 0 in the memory address of y.
@engelfriedrich978110 жыл бұрын
why can't it work in visual c++? is coding C++ in Visual Studio different in the one that you use? thank you..
@BrandonGamingTV10 жыл бұрын
Is your code correct? C++ is C++. The program you use shouldn't be very wrong. I notice that his program automatically says return 0 at the end even if he doesn't write it, but the logic should still be the same.
@RedstoneEditor11 жыл бұрын
I would assume it is similar to casting in Java?
@akshaykhobragade48719 жыл бұрын
It's showing an error that ‘ememy1’ was not declared in this scope. plz help
@utkarshjha6575 жыл бұрын
Can we inherit private values of base class by writing private instead of public, in derived class?
@WarnerBrosWannaB9 жыл бұрын
lol watching this at 2015 and time files!
@1kalekip17 жыл бұрын
Which files?
@akshayaggarwal59257 жыл бұрын
Flying files
@SomebodyOutTh3re6 жыл бұрын
to avoid working with pointers we can just use Virtual methods am I right ?
@Qazqi12 жыл бұрын
Congratulations. You've stumbled upon object slicing. You need a pointer or reference in order for this to work.
@brandonkejick41307 жыл бұрын
Heyy!! You used these examples from the SoloLearn app!!
@32Praful6 жыл бұрын
Brandon Kejick it's the opposite
@izephyrmor13 жыл бұрын
Thanks, Bucky.
@wssz1129 жыл бұрын
what is the difference between this polymorphism and just override every attack(); in inherited classes
@XodinBlood8 жыл бұрын
+Zheng SUn Thats whats I'm wondering, in the next video he shows the virtual function says it allows the function to know which class function to use, but it does the exact same thing here so, making virtual functions not that useful.
@vaypnishon18108 жыл бұрын
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