How Constructors Work With Inheritance | C++ Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ม.ค. 2025

ความคิดเห็น • 38

  • @ArM-wo6wd
    @ArM-wo6wd ปีที่แล้ว +7

    Easily the most concise explanation on this topic on YT. You answered every single one of my questions. Thanks a lot and I hope you get more recognition for your awesome work!

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      You’re very welcome, I’m happy to hear the video answered your questions! :-)

  • @nugget7579
    @nugget7579 ปีที่แล้ว +4

    Insanely underrated video lectures you are giving out for free, I am going to college soon and this has been a great help, will share with my friends(if i make any)👍

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      I'm really glad to hear you're enjoying the video lectures, and thank you so much for sharing them with people, that's very much appreciated! :-D

  • @victorsuarez4333
    @victorsuarez4333 ปีที่แล้ว +3

    This was exactly what I was looking for, thank you so much!

  • @shovnikpaul3297
    @shovnikpaul3297 ปีที่แล้ว

    Very good explanation. Addressed all the nuances that I was struggling with. Thanks!

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      I'm so glad to hear that Shovnik, and you're welcome! :-)

  • @itsme-oc4vl
    @itsme-oc4vl 2 ปีที่แล้ว

    Great explanation! I hope your channel grows!

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      Thank you very much for the encouragement! :-)

  • @Sergio-sd8fh
    @Sergio-sd8fh 2 ปีที่แล้ว +1

    Clear explanation, well done

  • @mymo_in_Bb
    @mymo_in_Bb ปีที่แล้ว +3

    Okay that's cool, but what if I need to call the base class constructor with some parameters (not hard-coded into the derived class) and then also the derived class constructor with more parameters? For example, making an instance of Drink with the name "hot chocolate" and a volume of 7 ounces?
    Would it look something like Drink( string name, double ounces ) : MenuItem( name ) ? Or is it something different?

  • @shanewalsch
    @shanewalsch ปีที่แล้ว

    very good explanation, thanks!

  • @behzad.tabari
    @behzad.tabari ปีที่แล้ว

    Hi, thanks it was a plain and good explanation, yet I have this question, is it possible to instantiate the derived and base class param constructor in run time, for example at the end you instantiated the base class at run time by giving it a string is there any notation which I could give the string and the ounces at the same time?

  • @David-mk8kh
    @David-mk8kh 2 ปีที่แล้ว

    Thankyou so munch man
    You made it clear

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      You're welcome David, I'm glad that it made things clear! :-)

  • @Aashutosh_Bajaj
    @Aashutosh_Bajaj 10 หลายเดือนก่อน

    Which is the IDE that you are using?

  • @gavinthecrafter
    @gavinthecrafter 2 ปีที่แล้ว +1

    One question: What if my base class has only a constructor with parameters, and my derived class has only a seperate constructor with parameters? Is there a way to put in parameters for both constructors at the same time?

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +1

      Like when you create the object, you want to provide arguments for both constructors, one in the base class and one in the derived class? The only way to do that, that I'm aware of, is to make a derived class constructor that accepts whatever parameters are needed for itself and whatever the base class constructor would need. And then that construct can "do some work" and then call the base class constructor as well. So you still only use one constructor when making the object, but the derived class constructor uses the base class constructor to have it do work too.

    • @gavinthecrafter
      @gavinthecrafter 2 ปีที่แล้ว +1

      @@PortfolioCourses Thanks!

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      @@gavinthecrafter You're welcome! 🙂

  • @muhammadluqman3452
    @muhammadluqman3452 ปีที่แล้ว

    hello sir great video , how can we stop the parent class constructor to be used in the child class ? thanks

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      So in general that is not something we want to do, the idea is that the parent class constructor sets up any member variables that the child class uses too. It may be possible though, there is an answer here for example: stackoverflow.com/questions/4065109/not-calling-base-class-constructor-from-derived-class

  • @deadapprox2267
    @deadapprox2267 2 ปีที่แล้ว

    Hello, thank you for this video, however I have faced a problem working on a project, what if the based param constructor was protected and not public, how can we use it from the derived constructor ?

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +1

      Can you maybe share your code in a comment so I can see what you're trying to do? :-) In general we cannot make something "more public" in a derived class... if it's protected in the base class it should be protected in the derived class too. So I'm just wondering what your code looks like and what you mean by "use it".

    • @deadapprox2267
      @deadapprox2267 2 ปีที่แล้ว

      @@PortfolioCourses Thank you for your reply, here is the part of the code :
      ----- header file ----
      class Piece : public CObject
      {
      private:
      CString model; // type of the piece
      protected:
      CString reference; // reference of the piece
      Piece(CString); // constructor, the parameter gives the piece type ; the reference will be composed of the three first letters of the type, all defined in cpp file below.
      public:
      Piece(); // default constructor, the type of the piece will be "piece" and the reference to "NR" in cpp file.
      void display(); //function that will display the model and reference
      };
      class Engine : public Piece
      {
      private:
      int cptref; // counter
      public:
      Engine(); // default constructor, the type of the piece will be "engine" and the reference will be composed of the 3 first letters of the type followed by a underdash "_" then the number of the piece.
      void display();
      };
      ------- cpp file -------
      Piece::Piece()
      {
      model = _T("piece");
      reference = _T("NR");
      }
      Piece::Piece(CString type)
      {
      modele = type;
      reference=modele.Left(3);
      }
      void Piece::display()
      {
      cout

  • @mowafkmha4505
    @mowafkmha4505 2 ปีที่แล้ว +1

    dude you are a legend I have been stuck with that error with the parametrized constructor and no default one
    but actually, the using base_class::base_class Methode didn't work with me I don't know why

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      I'm really glad to hear this video helped you out Mowafk! :-) I'm not sure why that wouldn't work for you, if you post your code here in a comment I could take a look at it (or maybe someone else may be able to help you out too if I can't).

  • @and_then_I_whispered
    @and_then_I_whispered 2 ปีที่แล้ว +1

    Thanks man!!!

  • @arafathdhroba8897
    @arafathdhroba8897 ปีที่แล้ว

    thank you very much

  • @destroylevi
    @destroylevi 2 ปีที่แล้ว

    thank you!