Abstract Classes And Pure Virtual Functions | C++ Tutorial

Поделиться
HTML-код
  • Опубликовано: 28 сен 2024

Комментарии • 47

  • @brandongill368
    @brandongill368 Год назад +13

    How has this only got 3.9k views! This is a fantastic explanation, thank you :D

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You're very welcome Brandon, I'm glad you enjoyed it! :-)

  • @M3t4lik
    @M3t4lik Год назад +6

    Clear and concise explanation of the relationship between abstract classes and virtual functions. thank you:)

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You’re welcome, I’m glad that you found the explanation clear and concise! :-)

  • @mosslnnx
    @mosslnnx Год назад +1

    I really enjoyed watching your tutorials, concise and clear and good examples, Thanks a lot.

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      You’re welcome Miss, I’m glad you’re enjoying the tutorials! :-)

  • @ermangurses1278
    @ermangurses1278 2 года назад +2

    Very clear explanation, thank you!

  • @omercandemirci6410
    @omercandemirci6410 Год назад

    Altough my english is bad. I understand purposes of abstract classes. In view of that situation I should say you are good teacher. Thanks

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      I'm really glad to hear that you enjoyed the video Ömer, and thank you for the kind words! :-) And by the way your written english seems pretty good to me!

  • @dzeno7370
    @dzeno7370 2 года назад +4

    What is the purpouse of using pointers to classes (a.k.a objects) in your example (and also couple of previous videos) if you are not using anything pointer-related stuff such as pointer arithmetics ? Could you do all those examples with something like
    Shape shapes [] = {Triangle(3,4), Rectangle(5,6)} .....
    Can you also quickly elaborate what is the main adventage in general sense of dealing with object using pointers to instances, rather than object instances itself?
    One more thing:
    Does polymorphism work only with pointers so maybe you are using "Shape *shapes[]" on purpouse instead of "Shape shapes[]" ?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +5

      These are great questions! :-)
      Objects that are instantiated using local variables of a function will exist in a portion of memory called 'the stack' that is managed for us automatically, they will have the lifetime of that function call (i.e. the memory that object uses on the stack will be freed when the function returns). But objects that are dynamically allocated (e.g. with 'new') exist on 'the heap', a separate portion of memory that we need to manually manage (e.g. by calling 'delete' to free the memory that object is using). We use pointers to refer to objects that have been dynamically allocated on the heap. Technically, we can use pointers to refer to objects on the stack as well. But using pointers allows to keep a "pointer" (i.e. memory address) to these objects on the heap, and that pointer can be passed around to different functions so they can access the same object too. Sometimes we'll have code where one function creates the object with dynamic memory allocation, and much later, a different function will free the memory associated object, using the pointer to the object.
      And I do not believe it is possible to implement runtime polymorphism without pointers, as the idea with runtime polymorphism is that the exact method to be called is determined at runtime when the member function of the object the pointer is pointing to is accessed. I don't know how that could really be possible without pointers, but sometimes in languages like C++ that are so large and have so many features, I'll find out there are ways of doing things that I'm totally unaware of. :-) This comment on stackoverflow also seems to suggest that it is not possible though: stackoverflow.com/a/7223724

    • @dzeno7370
      @dzeno7370 2 года назад

      @@PortfolioCourses great answer, thank you :)

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      You're welcome! :-)

  • @mihaibozian
    @mihaibozian Год назад

    Thank you sir, I've been struggling why do we need to instantiate a pointer to an abstract class, and now I got it.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You’re welcome Bozian, I’m really glad to hear the video helped you out! :-)

  • @Bl0xxy
    @Bl0xxy 9 месяцев назад

    You used the new keyword. How do i safely do that without memory leaks?

  • @McDonaldIbekwe
    @McDonaldIbekwe 3 месяца назад

    I did some modification to your program. I even tried using Smart Pointers but it didn't work.
    #include
    #include
    #include
    #include
    class Shape {
    public:
    virtual double area() = 0;
    virtual const std::string name() const = 0;
    };
    class Square : public Shape {
    public:
    Square(double side) : m_side(side) {}
    virtual double area() final { return pow(m_side, 2); }
    virtual const std::string name() const { return "square"; }
    ~Square() {}
    private:
    double m_side;
    };
    class Triangle : public Shape {
    public:
    Triangle(double base, double height) : m_base(base), m_height(height) {}
    virtual double area() final { return 0.5 * m_base * m_height; }
    virtual const std::string name() const { return "triangle"; }
    ~Triangle() {}
    private:
    double m_base;
    double m_height;
    };
    class Parallelogram : public Shape {
    public:
    Parallelogram(double side1, double side2) : m_side1(side1), m_side2(side2) {}
    virtual double area() final { return m_side1 * m_side2; }
    virtual const std::string name() const { return "parallelogram"; }
    ~Parallelogram() {}
    private:
    double m_side1;
    double m_side2;
    };
    class Circle : public Shape {
    public:
    static double pi;
    Circle(double radius) : m_radius(radius) {}
    virtual double area() final { return pi * pow(m_radius, 2); }
    virtual const std::string name() const { return "circle"; }
    ~Circle() {}
    private:
    double m_radius;
    };
    double Circle::pi = 3.1452;
    int main()
    {
    Shape* shapes[4] = {
    new Square(4),
    new Triangle(6, 9),
    new Parallelogram(11, 6),
    new Circle(7)
    };
    for (int i{ 0 }; i < 4; ++i) {
    std::cout

  • @cndyflss
    @cndyflss 11 месяцев назад

    thank you sm dude🥳🥳🥳🥳

    • @PortfolioCourses
      @PortfolioCourses  11 месяцев назад

      You’re very welcome, thank you for watching! :-)

  • @Catherine-h6v
    @Catherine-h6v Год назад

    Nice video, very clear

  • @gabrieldias6430
    @gabrieldias6430 Год назад +2

    awesome video

  • @abrahamalejandroguzmancard6299

    You are amazing!

  • @animeIsle
    @animeIsle Год назад

    Uhm why am I getting this error? I don't understand why I am getting this at all, btw this error is apparently in the for loop cout command: "no match for 'operator

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      I'm not sure, it's hard to know without seeing your code. The original code for the video is found here: github.com/portfoliocourses/cplusplus-example-code/blob/main/abstract_class.cpp. Do you get the error if you use this code? Maybe there is a difference between this code and the code you're using? Apparently this error can happen if you use "end" instead of "endl" when outputting values, so maybe check for that, but the error can occur for other reasons as well.

    • @animeIsle
      @animeIsle Год назад

      @@PortfolioCourses uhm I found the solution to it myself. Apparently if you use a method that uses cout command in a cout statement, then that error shows up. So I fixed it by simply calling the method outside of the cout statement.
      Thanks for replying though.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      @@animeIsle That makes sense, I'm glad you figured it out! 🙂

    • @animeIsle
      @animeIsle Год назад

      @@PortfolioCourses Yea and oh boy, figuring it out yourself gives you a lot of satisfaction. You're surprisingly replying to the comments even if the video was uploaded quite a while ago. Hard to find people like you ngl. I respect you for that.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      @@animeIsle I totally agree that figuring it out yourself feels great, that's the "addiction" of programming. 🙂 And thanks for the kind words!

  • @kaizoku_04
    @kaizoku_04 Год назад

    Thank you so much

  • @McDonaldIbekwe
    @McDonaldIbekwe 3 месяца назад

    Please, is there a way of doing it with Smart Pointers?

  • @MarcArnold-p8y
    @MarcArnold-p8y 10 месяцев назад +1

    I've watched many of your videos and the thoroughness of your explanations is wonderful. Awesome work!

    • @PortfolioCourses
      @PortfolioCourses  10 месяцев назад

      I'm really happy that you're enjoying the content, thank you for the kind feedback! :-D

  • @laali_puppy
    @laali_puppy 6 месяцев назад

    Great explanation, thanks!

  • @clammyclams8968
    @clammyclams8968 Год назад

    Very Professional Explanation. Thank You So Much.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You're welcome, and thank you for the kind words! :-)

  • @joelparker
    @joelparker Год назад

    great!

  • @Vichained
    @Vichained 2 года назад

    Yo kevin, its me once again:
    its not a video-related question but, does template for switch case exist?
    for example i got this:
    case 1:
    std::cout

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      The short answer is "not really". Rather than use templating, something like a function would be a better approach to reducing code duplication in this case.

    • @Vichained
      @Vichained 2 года назад

      @@PortfolioCourses damn. Didn’t think about that. Thanks master!!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      @@Vichained Haha you're welcome! :-D