Inheritance in C++

Sdílet
Vložit
  • čas přidán 9. 09. 2024
  • Twitter ► / thecherno
    Instagram ► / thecherno
    Patreon ► / thecherno
    Slack ► slack.thechern...
    Series Playlist ► • C++
    BEST laptop for programming! ► geni.us/pakTES
    My FAVOURITE keyboard for programming! ► geni.us/zNhB
    FAVOURITE monitors for programming! ► geni.us/Ig6KBq
    MAIN Camera ► geni.us/t6xyDRO
    MAIN Lens ► geni.us/xGoDWT
    Second Camera ► geni.us/CYUQ
    Microphone ► geni.us/wqO6g7K

Komentáře • 239

  • @KE010101
    @KE010101 Před 4 lety +351

    I'm learning C++ now, and these videos showing the code being written while explaining the concepts are most valuable.

    • @appsenence9244
      @appsenence9244 Před rokem +1

      Yes that is the definition of a tutorial and the reason we are all here. But thanks for stating the obvious.

    • @KE010101
      @KE010101 Před rokem

      @@appsenence9244 🧡

    • @KE010101
      @KE010101 Před rokem

      @@appsenence9244 your absolutely correct, thanks for pointing that out, 🧡

    • @appsenence9244
      @appsenence9244 Před rokem +1

      @@KE010101 that'll be 5 bucks

    • @KE010101
      @KE010101 Před rokem

      @@appsenence9244 🤣🤣

  • @piyushphodu
    @piyushphodu Před 3 lety +105

    even though i know 99% percent of these stuff, i still watch the whole video for cherno's wisdom nuggets

    • @EtBukaneluu
      @EtBukaneluu Před 3 lety +2

      could we just move printName to entity class ?
      and how its gonna look like ?

    • @mohamedfouzibenreguia8343
      @mohamedfouzibenreguia8343 Před 3 lety +2

      @@EtBukaneluu you can do that. but in the example we don't need name for entity class. but we need create new class "player" has name & printName function because player have a name. but entity class didn't need a name
      yes ,you can move printName to entity class but you are entering function you do not need

    • @liminal6823
      @liminal6823 Před 2 lety +4

      This is a great point - hearing even the simplest stuff that you already know from someone very smart like Cherno reinforces your understanding and can even reveal that you don't know it as deeply as you thought. 👍

    • @martiananomaly
      @martiananomaly Před 2 lety +1

      yessir

  • @exnaruto1
    @exnaruto1 Před 7 lety +290

    "oh man, this hair today, it's just, just perfect" - xD. I love it. xD

    • @user-bi1qi8bl9y
      @user-bi1qi8bl9y Před 4 lety +6

      could move on to be a fashion vlogger as well, hahaha

  • @mikeock3164
    @mikeock3164 Před 4 lety +163

    I'm a Computer Science major and with the whole online learning thing, its been a but hard to learn things. Thanks for these videos !! They really are a big help.

    • @darksquirrel7436
      @darksquirrel7436 Před 4 lety +10

      yeah i will have an exam in 5 days and i haven't attended a class in weeks if it weren't for these videos i would have to learn from the teacher's powerpoints which are crap

    • @Thadnill
      @Thadnill Před 4 lety +3

      @@darksquirrel7436 I have exam tomorrow and crammmiing these videos as **** haha

    • @mryup6100
      @mryup6100 Před 4 lety +2

      In my area we are already on break.

    • @darksquirrel7436
      @darksquirrel7436 Před 4 lety +5

      @@Thadnill Good luck i also have my programming exam tomorrow LOL
      Edit: I passed the exam and btw if you have to take a java exam don't think c++ is similar because it is not

    • @Thadnill
      @Thadnill Před 4 lety +2

      @@darksquirrel7436 Lol it became an all - nighter for me, I haven't slept yet and my exam is in 2 hours, I might have to do it again after summer, haha well good luck to both of us!

  • @mr.budders8740
    @mr.budders8740 Před 3 lety +29

    Your tutorials are great, you edit out when your not talking to reduce the tutorial time making learning faster. You also talk fast and don't stutter also reducing the time. Its quick and concise unlike some people that don't do any editing and just talk without much idea of were they are going from there.

  • @yuchen3587
    @yuchen3587 Před 4 lety +18

    I go back and watch these videos every time I forget something. And Cherno always gives me the answer

  • @serkanozturk4217
    @serkanozturk4217 Před 2 lety +11

    Personal notes:
    - Example: “Cat : Animal” now Cat class has not only type cat but also has type Animal. That is, it is actually both of those types
    -Polymorphism ~= being able to use the same fuction for different type of objects thanks to inheritance

  • @JawwadHafeez
    @JawwadHafeez Před 3 měsíci +2

    Had been programming since decades and love ur description. It gives a new way of thinking and understanding.

  • @rufusprime0015
    @rufusprime0015 Před 4 lety +9

    you explain things so clearly and concisely, really helps with my c++ textbook and examples for class. The textbook is actually great but this video is more compressed and helped me get a clear understanding easily. I tend to use these videos as a supplement to what i'm learning in class and it helps so much. Thank you for taking the time to explain this.

  • @mahdishirazi5785
    @mahdishirazi5785 Před 2 lety +6

    this guy is amazing, taught me something in 8 minutes that my teacher failed to do in a whole semester

  • @LightPenStudio
    @LightPenStudio Před 3 lety +5

    That light coming through the window is somehow ethereal.

  • @josephh8117
    @josephh8117 Před 2 lety +1

    Cherno has helped me massively in my CS education these last few years. Thank you so much, my Ozz brotha.

  • @dxlge
    @dxlge Před rokem +15

    Notes:
    Inheritance is one of the most fundamental and powerful part of classes. It allows us to have a hierarchy of classes which relate to each other, it allows us to have a base class which contains common functionality and then it allows us to branch off from that class and create sub-classes from that initial parent class.
    The reason why this is so useful is because it allows us to avoid code duplication. Instead of us repeating ourselves over and over again we can put all of our common functionality between classes into a parent class and then simply make sub-classes from that base class, which either change the functionality in subtle ways or introduce new functionality.
    The idea behind inheritance is that it gives us a way to put all of our common code into a base class so we don’t have to repeat ourselves.

  • @JacobKrajewski
    @JacobKrajewski Před 5 lety +6

    Love the way you explain these things. The way you explain things is exactly the way I like to learn. Very talented teacher. Thanks

  • @WayneRiesterer
    @WayneRiesterer Před 5 lety +104

    class BjarneStroustrup { // stuff }; class TheCherno : public BjarneStroustrup { // extra stuff };

  • @haroldcarter8366
    @haroldcarter8366 Před 5 lety +15

    did this man just explain polymorphism in 23 seconds? (mind = blown)

  • @maxim25o2
    @maxim25o2 Před 3 lety +1

    This is exactly what i learned, is avoiding of code duplication, but many peoples doing tutorials don't understand that and they do it opposite way. Misleading many peoples who want learn inheritance.

  • @mahlecyrus3960
    @mahlecyrus3960 Před 2 lety +1

    YOU HAVE EARNED ALL MY RESPECT , I AM LEARNING EXCITING THINGS FROM U SINCE 2019 WHEN I STARTED C++ FROM TIME TO TIME IF DONT UNDERSTAND SOMETHIN I COME TO THESE VIDEOS OF YOURS ,YOUR VIDEOS ARE APPRECIATED

  • @ikemkrueger
    @ikemkrueger Před 2 lety +2

    You‘re doing a very good job at explaining the things. Thank you for sharing that with us.

  • @matthewe3813
    @matthewe3813 Před 3 lety +2

    After watching this video, I finally understand inheritance. All of the other tutorials just made no sense to me.

  • @j0n1
    @j0n1 Před 3 lety +1

    "Types in C++ are quite a complicated topic because on one hand they don't really exist, however on the other hand they kind of do." is just perfect.

  • @AhmetYusufYatkin
    @AhmetYusufYatkin Před 4 lety +5

    Speedrun of learning. I loved it :D

  • @joeman123964
    @joeman123964 Před 3 lety

    biggest issue with my murach's textbook. it tries to teach inheritance with a MASSIVE project of code. having a base class with 4 subclasses and other forms of nested functions without showing the entire code. this video kept it simple to teach the basic principle of inheritance. well done.

  • @yuchen3587
    @yuchen3587 Před 5 lety +1

    Excellent. It is good to have this video and have a look at it every time I forget something.

  • @abod4gamer
    @abod4gamer Před 4 lety +3

    3:09 Schrodinger's cat

  • @dilute6980
    @dilute6980 Před 5 lety +1

    Perfect example. The example of having different entities inheriting from 1 main class for the generic entity stuff makes complete sense

  • @IsaacC20
    @IsaacC20 Před rokem

    I've had this model for understanding inheritance for years. It's adequate until you learn about multi-ctor call and ctor initializer list, polymorphism vtables/vptrs, object slicing problem, and multi-inheritance and the diamond-inheritance problem.
    You then have to refine this basic understanding of inheritance to include the idea of "subobjects": if B inherits from A, it's not that B just has members from A, B actually "owns" a subobject of type A. In addition, to create a B object, the ctor of A must be called to initialize B's A subobject.
    Subobjects can be seen in visual studios while step debugging. When you inspect a B object, you'll see that it contains an A subobject!

  • @kietdo4379
    @kietdo4379 Před 2 lety

    Fast, summarize and precise. Good job man. Love to know you

  • @russellcannon9194
    @russellcannon9194 Před 5 lety +2

    Hey Cherno, I have been enjoying this series on C++ and learning a lot from you, but as far as I can tell, you have not done a video on polymorphism like you suggested you might in this video. I would love to see that from you. Cheers, Russ

  • @song5030
    @song5030 Před 7 lety +4

    Hey Cherno! Love you're videos man, please keep it up! I am quite experienced with C++, been programming for almost 4 years now. I just have couple of question regarding these tutorials. Are these tutorials going to be made with c++14 or up standart in mind? I find it really hard to find quality modern C++ tutorials online.

  • @Jester8890
    @Jester8890 Před 5 lety +1

    Just found your channel studying for my data structures exam, really great content! I'll be checking out the rest soon!

    • @tannerbarcelos6880
      @tannerbarcelos6880 Před 5 lety

      How’s data structures? I’m going into it this fall. Been killing my c++ class all semester till I got to copy constructors and now into inheritance and polymorphism

    • @Jester8890
      @Jester8890 Před 5 lety

      @@tannerbarcelos6880 Best way to describe it for me is, it is difficult but very rewarding. It kind of adds several different tools to your tool belt and helps you approach problems with a different view; instead of just brute forcing the solution you think of what would be the best data structure to solve the task. I'm still not 100% comfortable with certain things and I'm towards the end of the semester, but, I think having a better understanding on a more shallow level of what is available I can use the information to dive deeper in during certain situations. Let me know if you have any questions this fall, happy to learn more with you.

  • @pimp2570
    @pimp2570 Před 6 lety

    Thank you for explaining it that well, this looks super useful.

  • @valizeth4073
    @valizeth4073 Před 6 lety +48

    You should have covered the protected keyword

    • @arnmazing3156
      @arnmazing3156 Před 5 lety +6

      Vali Zeth protected just means anything that isn't a child of the parent class can't have access to that class member.

    • @valizeth4073
      @valizeth4073 Před 5 lety +31

      @@arnmazing3156 I know, I know. But in a video about inheritance and sort of visibility, the protected keyword should be explained, even though it's simple.

    • @nishithbaravkar7549
      @nishithbaravkar7549 Před 3 lety

      stop cribbing .. you could just ask the viewers to learn about it in a cinstructive way

  • @darimuhittin
    @darimuhittin Před 6 měsíci

    If you say 'Exactly' in a whole 10 hours video again and again i can watch it

  • @can3792
    @can3792 Před 2 lety

    at the end of the video had me in laugh :D :D

  • @TimPatDuDe
    @TimPatDuDe Před 4 lety +1

    Thank you for making this.

  • @RandomGuyyy
    @RandomGuyyy Před 7 lety

    That lens effect is like the default post processing setup in Unreal but with better bokehs.

  • @reycors6871
    @reycors6871 Před 4 měsíci

    thanks bro

  • @vegansynths7757
    @vegansynths7757 Před 4 lety

    This was your clearest video yet.

  • @Indraneel-Ahluwalia
    @Indraneel-Ahluwalia Před 6 lety +2

    Hi thanks for good videos . Please make a video on friend classes and functions.

  • @LunaFlahy
    @LunaFlahy Před rokem

    the end lol

  • @WaqarKhan-mw2de
    @WaqarKhan-mw2de Před 6 lety +1

    Really awesome, Thankyou so much dear

  • @HermanWillems
    @HermanWillems Před 6 lety +3

    Is there also a video about Composition... i mean they say try to avoid Inheritance as much as possible. Use composition.

  • @rarasdfa12312
    @rarasdfa12312 Před 4 lety

    you are the best

  • @h.hristov
    @h.hristov Před 7 lety

    Thanks! Love your videos man

  • @tannerbarcelos6880
    @tannerbarcelos6880 Před 5 lety

    My book and teacher are terrible. You have taught me what was making me second guess getting my cs degree lol. Stress has been slightly lessened! I still have an A in my c++ course but I’m starting to become a tad concerned on how all this operator overloading, inheritance and polymorphism is making me struggle..

  • @tcchan14
    @tcchan14 Před 5 lety +3

    interesting concept. Subclass actually contain more from superclass.

  • @ymahmoodi5817
    @ymahmoodi5817 Před 4 lety

    Your videos are perfect.
    It would be great if you made video about multiple inheritance!
    Thank you.

  • @inwerp
    @inwerp Před 7 lety +2

    Use ctrl + f5 to build-run and keep the console open.

  • @primulaeinscode
    @primulaeinscode Před 11 měsíci

    Tell your teacher this: "This is how you teach."

  • @Ebiko
    @Ebiko Před 2 lety

    Really good video.
    Can you explain why ` class Player : public Entity ` needs the public, whats the purpose of the "public " ?

  • @DigitalDivotGolf
    @DigitalDivotGolf Před 5 lety

    excellent job

  • @pdxbound81
    @pdxbound81 Před 4 lety +7

    I'm only going to learn C++ if you promise my hair will look as cool as yours!

  • @ceachibogdan8087
    @ceachibogdan8087 Před 5 lety +3

    Hey, can you make some tutorials for parallel programming in c++ ?

  • @zerotoinfinity3189
    @zerotoinfinity3189 Před 2 měsíci

    i am new to c++ can any explain what is meant by this statement i could not find answer to this online
    in example give in video
    class Player : public Entity::something
    ::something can anyone explain this
    what is meant by above state have seen similar in a code base trying to understand

  • @aVeryAngryDuck
    @aVeryAngryDuck Před 4 lety

    Great video very helpful explanation 👍

  • @AbdulMoiz-ho8rx
    @AbdulMoiz-ho8rx Před 2 lety

    Great

  • @taocry
    @taocry Před 4 lety +3

    After this video, something is missing. How to reuse the constructor from super class?

    • @anatolistankevich2592
      @anatolistankevich2592 Před 4 lety

      i've got the same question, dont know how to create an instance of my subclass, I've got a defined constructor in my superclass, but it doesnt work in subclass

  • @AgentM124
    @AgentM124 Před 7 lety +4

    What happens if you change "public Entity" into "private Entity"?
    does private make all the Entity fields and methods only accessible to player?

    • @dekrain
      @dekrain Před 7 lety +1

      Agent M it will be only visible to Player class' methods

    • @AgentM124
      @AgentM124 Před 7 lety

      so if public banana() in Entity then it won't be visible to anything but Player if it is private Entity

    • @dekrain
      @dekrain Před 7 lety +2

      Agent M yes, you can also write protected instead of public, then it will be visible in Player class, friend classes/functions and subclasses, but no outside

    • @Lmao-ke9lq
      @Lmao-ke9lq Před 3 lety

      @@dekrain so inhariting with is going to be same as inheriting public, but with protected class member from inherited class?

  • @thorep
    @thorep Před 5 lety +1

    is the __declspec and stuff like that explained in the c++ series?

  • @persistence2478
    @persistence2478 Před 7 lety +3

    Hey man! Love your videos! I have a question. Before you write your programs do you plan them out on paper? If so, can you share how exactly you do this and when do you know that you're ready to start writing code? If anyone else can share their method, please do. Thanks!!!

    • @eloquentlyemma
      @eloquentlyemma Před 7 lety +2

      Persistence 24/7 I use DFDs (data flow diagrams - which designs the code around the data in the system - I think it's part of the JSD methodology if I remember correctly), process flow charts and pseudo code. I did learn UML for university - I didn't like it much, but it is the one to learn if you are looking for job skills.

    • @persistence2478
      @persistence2478 Před 7 lety

      thanks so much! I'll look into those methods!

  • @theNeuo13
    @theNeuo13 Před 2 lety

    thanks for your videos, there are really helpful. which one of them is talking about overriding a function?

  • @jugnu361
    @jugnu361 Před rokem +1

    👍👍👍

  • @KeithKazamaFlick
    @KeithKazamaFlick Před 2 měsíci

    hair god

  • @DrGreenGiant
    @DrGreenGiant Před 4 lety +1

    May I request a video on virtual inherence please?

  • @Omar.Alamoudi
    @Omar.Alamoudi Před 4 lety

    Question: So if we have two classes split between 4 files, two headers and two cpp files, we only need to use #include in the child class?

  • @VoidAshen
    @VoidAshen Před 3 lety

    oh we aren't even scraping the surface
    *sees 27/95
    oh yea we aren't

  • @christophilous4831
    @christophilous4831 Před 6 lety

    Why don't the functions PrintName and Move occupy any memory in the sizeof calculation? Wouldn't the object need to hold a pointer to them, at least?

  • @Scherbakov
    @Scherbakov Před 2 lety

    Super! But sometimes the subtitles disappear somewhere.

  • @tessanix9771
    @tessanix9771 Před rokem

    I Always see the line "priorise composition over inheritance". Can you explain this concept in c++ ?

  • @sgyffysgyffy4736
    @sgyffysgyffy4736 Před 6 lety

    Great video

  • @LiquidMark
    @LiquidMark Před rokem

    Epic

  • @mmoa1985
    @mmoa1985 Před 6 lety

    So why is inheriting different than including? for example: if in this video we included the Entity class inside the Player class.. will this be the same or there are differences?

  • @Raul11madrid1
    @Raul11madrid1 Před 7 lety

    Thank you .. 🌹🌹

  • @mikeandjamiejarboe7211

    How/Why do the variables X, Y get initialized to 0? player.X = 0; player.Y = 0; Yet, entity.X and entity.Y are uninitialized???

  • @ghofranetorjmene2960
    @ghofranetorjmene2960 Před 11 měsíci

    hello , the size of char is 1 byte ! so the size of const* char Name is equal to 1 byte and not 4 bytes !

  • @bloodwolf8307
    @bloodwolf8307 Před 4 lety

    good

  • @botobeni
    @botobeni Před 3 lety

    why char* occupies 4 bytes of memory, but can be a very lenghty string(more than 4 bytes)??

    • @walkmoon3479
      @walkmoon3479 Před 2 lety

      char* is the pointer pointing the first char of a string. Once we know the where the first is, we literally knows where my string is

  • @reubenfrench6288
    @reubenfrench6288 Před 6 lety

    How do static variables get inherited? Is there still just one copy for all instances of BaseClass and its subclasses, or is there a separate copy for each of its subclass types?

  • @KillzoneKid
    @KillzoneKid Před 6 lety +38

    Kinda expected to see explanation of types of inheritance (public, protected and private) as well as default inheritance. Little disappointed with this episode of otherwise excellent series.

  • @user-iu9ph5ld2v
    @user-iu9ph5ld2v Před rokem

    I thought member function also takes some memory in a class....anyone could explain why size of only return member variables?

    • @bobdagamer640
      @bobdagamer640 Před rokem +1

      Member functions are stored with the rest of the code, not with the instance of the claas. Sizeof only returns the size of the memory used by an instance of an object

  • @EtBukaneluu
    @EtBukaneluu Před 3 lety

    could we just move printName to entity class ?
    and how its gonna look like ?

  • @johuremoker3818
    @johuremoker3818 Před 5 lety

    thanks a lot~

  • @TheZenytram
    @TheZenytram Před 3 lety

    so it is basicaly the same concept of a function but for classes.

  • @kushalvadher
    @kushalvadher Před 7 měsíci

    pls show altr for super key word in c++

  • @nikowill6979
    @nikowill6979 Před 7 lety

    great video, still need more in depth stuff...

  • @unclesam1545
    @unclesam1545 Před 2 lety

    showing off to my high school teacher

  • @erwinschrodinger2320
    @erwinschrodinger2320 Před 3 lety

    Amazin

  • @abdulcool1268
    @abdulcool1268 Před 6 lety

    hey one question. can we not have a function and a class of same name. for eg if we write void player(float xa,float ya) and we have a derived class player. if we create an obj and try to call the player function it shows an error

  • @1314520Joanna
    @1314520Joanna Před 6 lety +1

    I tested the same code, somehow I get total of 16 bytes but not 12 like you presented in the video? Is it something wrong with my machine or setup?

  • @fearmyrice
    @fearmyrice Před 5 lety

    perfect timing for exams

  • @seatoaster1491
    @seatoaster1491 Před 7 lety +5

    Could you do a video on what a operating system kernel is

  • @edwinov
    @edwinov Před 4 měsíci

    Now your name is "Acherno" !?

  • @bradcasper4823
    @bradcasper4823 Před 2 lety

    I get an error with code below. Does ":" sign copy contents of "father class" into its child?
    #include
    using namespace std;
    class Entity
    {public:
    int X, Y;
    Entity(int x, int y)
    {
    X = x;
    Y = y;
    ShowPos();
    }
    void ShowPos()
    {
    cout

  • @mba2ceo
    @mba2ceo Před 2 lety

    How do I use the const char *name ?

  • @giuliograziani3261
    @giuliograziani3261 Před 3 lety +1

    nice hair

  • @Djzaamir
    @Djzaamir Před 7 lety +1

    can someone please answer me , why did he use const char instead of just char

    • @Steven-tw7iz
      @Steven-tw7iz Před 7 lety +9

      the const keyword changes the variable to a "read only" state so you don't accidentally modify its value later on in the code and cause an unforeseen bug. Its generally considered good programming practice to make any variable that doesn't need to be modified in the code a 'const' variable.

    • @Djzaamir
      @Djzaamir Před 7 lety

      i see , Ste_3 D_ven thanks for quick reply :)

  • @livesimplyandhumbly
    @livesimplyandhumbly Před 4 lety

    I subscribed because we share the same fashion statement.

  • @sebastianricks9459
    @sebastianricks9459 Před 4 lety

    Hello :).
    Love the videos. Thank you so much!
    Have you made one on polymorphism yet?

  • @InFmou5
    @InFmou5 Před 4 lety

    Its sucks that classes inheritance in separate files was not handled here.... I'm having a lot of issues trying to figure out how to handle a class inheritance from a separate file and I'm getting a redefinition error that I have no idea why is happening.

    • @bobdagamer640
      @bobdagamer640 Před rokem

      Do yoy have include guards on the headers?