1

this is the mock up test for my final. I have to be able to explain how these kind of code works. But to be honest, I do not understand this code quite well. Can you guys please explain my how this works? I will be so glad to be prepared for my final if I can understand it perfectly. Thank you.

   #include <iostream>
    using namespace std;
    class Wind {
        int category;
    public:
        Wind(int cat = 3) {
            category = cat;
            cout << "1." << cat << endl;
        }
        virtual void increase(int amount) {
            category += amount;
            cout << "A. " << category << endl;
        }
        void operator++() {
            ++category;
            cout << "B. " << category << endl;
        }
        virtual ~Wind() {
            cout << "C. " << category << endl;
        }
    };
    class Tornado : public Wind {
        double velocity;
    public:
        Tornado(int cat, double vel) : Wind(cat) {
            velocity = vel;
            cout << "2. " << vel << endl;
        }
        virtual void increase(int value) {
            velocity += value;
            cout << "X. " << velocity << endl;
        }
        void operator++() {
            Wind::operator++();
            velocity += 20;
            cout << "Y. " << endl;
        }
        ~Tornado() {
            cout << "Z. " << velocity << endl;
        }
    };
    int main() {
        Wind* wind_array[2];
        wind_array[0] = new Tornado(7, 66.5);
        wind_array[1] = new Wind(5);
        for (int i = 0; i < 2; i++) {
            wind_array[i]->increase(5);
            ++(*wind_array[i]);
        }
        for (int i = 0; i < 2; i++)
            delete wind_array[i];
        return 0;
    }

This is the output.

1.7
2. 66.5
1.5
X. 71.5
B. 8
A. 10
B. 11
Z. 71.5
C. 8
C. 11
Coder234
  • 21
  • 2
  • 1
    please be more specific. what part of this code exactly do you not understand? what output do you expect, and what actual output do you get? – goodvibration Aug 09 '20 at 05:07
  • I think I only understand first 3lines. But I am not even sure whether I understand it correctly. So I wanna know how this code works overall. – Coder234 Aug 09 '20 at 05:14
  • So, essentially, you want someone to explain everything to you about how the code works. That won't happen. There are several elements of that code which interact - and starting with the "I only understand the first three lines" means that anyone will have to expend a huge effort to help you - and deal with you asking basic questions that are answered in any introductory textbook on C++. You probably need to start by reading - and, better, seeking to understand - any basic introductory text on C++. – Peter Aug 09 '20 at 05:23
  • Sorry, I tried hard on other parts, but I cannot understand this one. I still do not understand why we define how the virtual functions works since I learned we shouldn't define anything for virtual functions, and I cannot understand how the code is working basically and am too desperate. – Coder234 Aug 09 '20 at 05:46
  • I really suggest you to debug it, it's pretty simple when you take in step by step :) and if you fail to understand specific output ask us. keep in mind, the ++ is overloaded, the tornado is derived class, it means it will perform fathers constructor and after his. – InUser Aug 09 '20 at 05:59
  • What I dont understand about line 4 is that they both have increase function. But I do not understand why it doesnt call the base class increase function at all. – Coder234 Aug 09 '20 at 06:13
  • It's virtual function, if function is virtual it will only call tornado's increase function – InUser Aug 09 '20 at 06:18

1 Answers1

1

Please read about virtual functions and derived classes, it will make sense after.

Derived classes

Virtual functions cpp

1.7 - constructor wind (Wind is the base class of the tornado so the this constructor executed first)
2. 66.5 - constructor tornado
1.5 - constructor wind
X. 71.5 - increase tornado (Virtual function so the base class increase is not executed)
B. 8 - ++ wind (Not a virtual function - so tornado's ++ is not executed)
A. 10 - increase wind
B. 11 - ++ wind
Z. 71.5 - destructor tornado
C. 8 - destructor wind (Wind is the base class )
C. 11 - destructor wind

InUser
  • 1,138
  • 15
  • 22
  • Thank you so much but I just have several questions. – Coder234 Aug 09 '20 at 06:43
  • In line number 4 even though is is virtual function, the pointer is allocated in the base function. Then shouldn't it call the base increase? and in case of ++ shouldn't it call the derived ++ too after it execute base operator? and when it calls the destructor, as I know, destructor destruct the recent object first. Shouldn't it destruct array 1 first instead of array 0? – Coder234 Aug 09 '20 at 06:49
  • 1. Virtual function "last" override is the strongest, it will search the "lowest" available implementation and execute it and not the implementation in the base class . In this case the tornado's implementation. 2. No, because ++ is *Not* virtual function that mean it will execute just the base class implementation. 3. The destructor is called explicitely on the 0 element and then on the 1, so it will be in this order – InUser Aug 09 '20 at 06:55
  • Thank you so much. I think now I understand everything. But just one last thing. In the derived destroctor, why does it call base destructor after this function works? instead of calling base destructor immediately like it did in the constructor? – Coder234 Aug 09 '20 at 07:42
  • I think this is great answer -> https://stackoverflow.com/questions/3261694/why-base-class-destructor-virtual-is-called-when-a-derived-class-object-is-del – InUser Aug 09 '20 at 07:49
  • I fully understand this code now bro. Thank you so much. I think I can nail my final!!! – Coder234 Aug 09 '20 at 08:13
  • good luck with the final, pls consider accept this or any other answer so the question will not remain open – InUser Aug 09 '20 at 10:05