C++ Tutorial 11 : Polymorphism

Code & Transcript :
Best C++ Book :

We cover a lot in this tutorial. We cover Polymorphism, Structs, Friend Classes, Abstract Classes, Override, Final, Virtual and much more.

Tag: polymorphism c++, C++ Tutorial, C++ Polymorphism, Polymorphism

Xem thêm: https://meohay360.com/category/internet

Nguồn: https://meohay360.com

47 thoughts on “C++ Tutorial 11 : Polymorphism

  1. developingakernel2 says:

    Understanding typecasting and pointer arithmetics could be helpful to fully understand this:

    Say that you have 2 classes: class base and class derived. base contains 2 integer variables: a and b, and derived inherits from base and has 2 additional integers: c and d.
    Now let's say that we initialize derived: class derived* d = new derived; d->a = 1; d->b = 2; d->c = 3; d->d = 4;

    Now let's initialize the base class by not using new but instead by placing it's pointer to the same memory location as d: class base* b = d;
    Now what happens if we try to print out the values?
    cout << b->a << endl; cout << b->b << endl; we will see these values: 1 and 2.
    Now for the pointer arithmetics: if we do: cout << (b+1)->a << endl; cout << (b+1)->b << endl; we get these values: 3 and 4.

    This makes sense because we use pointer arithmetics.
    But this is also typecasting. When we did "class base* b = d" we actually did: class base* b = (class base*)d; This (class base*) in parenthesis can be done using the g++ compiler for example.
    So this is a part of polymorphism that is not talked about so much, maybe. This (b+1)->a should not be used when virtual functions are present though because the virtual table is then present.

  2. maxim25o2 says:

    I have some problems with inherit classes, and after playing it, I get that. { I dont know why Your version didn't worked for me. 🙁 }
    #include <iostream>
    #include <cmath>

    using namespace std;

    class SHAPE{
    double height;
    double width;
    void s(double lenght){ // changed to function
    height = lenght;
    width = lenght;
    void s(double h, double w){ // changed to function
    height = h;
    width = w;
    virtual double area(){
    return height * width;

    class CIRCLE : public SHAPE{
    void c(double w) { // changed to function
    s(w); // used as function.
    double area(){
    return 3.14159 * pow((width / 2), 2);

    void showarea(SHAPE& shape){
    cout << "Area : " << shape.area() << endl;

    int main(){

    SHAPE square;
    square.s(10,5); //after creating object.
    CIRCLE circle;
    return 0;

    program works great, but after that changes 🙁

  3. maxim25o2 says:

    NOw I understand! I needed look this one 3 times 11:32. Polymorphism is a way to copy all containers to class circle, that means i don't need create new values of 'width, height, area' because class shape have that already. And I can use structure from that class for new object. Its like #Include file.h, polymorphism works at that rule . I am include structure from another class for new object, and I don't need anymore create this same names.
    Do I have right??
    If You could do tutorial with more examples about that I will be super HAPPY.

  4. kira light says:

    Hello Derek,
    Thank you for explaining hard concepts in a very clear way. You are very helpful in helping lots of Com Sci student understand the concept thoroughly. I have two questions regarding this video Derek, and they are from the Polymorphism part in this video. 

    1) In the Shape class, you did not define a default constructor, isn't it like when you decide to explicitly define general constructors then you also have to explicitly define default constructor(with no arguments or with default parameter)?
    2) For the void Show Area(Shape& shape), why did you use pass by reference instead of pass by value? I mean you aren't modifying data member from the object so why did you use it? 

    Thank you so much for your time Derek. God bless.🙏

  5. Full Iton says:

    A little comment: Common convention is since the beginning of time, NEVER to name a member function or a method or operation with a beginning capital letter. The naming conventions are there to enhance readability and increasing the value of code in many ways. The same is for class names, they are not named containing imperative verbs, NEVER. A class is the something that contains data and operations on that data. So it is to be seen as a static real world object. a car, a tree, a shape, a customer etc.. a class named GetCustomerName is wrong. Instead, Customer and CustomerData or CustomerMeintainance or CustomerQueries or off course straight in Customer class, with getCusomterName() as a member function is correct.

  6. Dan Cobb says:

    Derek, I really can't tell you how much I appreciate your training videos. Just terrific. I'm a technical writer, not a professional programmer. I do need to write short snippets and examples of using new classes and methods my company adds to its very deep code base, and your videos have helped me incredibly. I am so grateful. Much fun to write the code here, play with it to break and fix. It's amazing how deep and powerful C++ is. C++ engineers are geniuses. Thanks for everything. I am a fan!

  7. Syed Imad Haqqi says:

    Exceptional tutorials Derek.
    At 2:33, you have not used the data access specifier for circle inheriting from struct shape. Can we use protected or private data specifier for the derived struct and in that case, do we have to use specifiers.
    At 14:53, why should the compiler be forced to check if virtual function of base class is same as derived class. Due to the law of inheritance, the double Area() inside the circle class should be virtual.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *