Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trace the following program, show the sequence of executed instructions and the

ID: 3546392 • Letter: T

Question

Trace the following program, show the sequence of executed instructions and the result of each execution:
                            
                            #include <iostream>
                            using namespace std;
                            
                            class A {
                            protected:
                            int num;
                            public:
                            1. A() {cout << "Default Constructor A "; num = 0;}
                            2. A(int a) {cout << "Constructor A(int a) "; num = a;}
                            3. int getNum () {return num;};
                            4. void setNum (int a) {num = a;}
                            5. void increment(int a) {num = num + a;}
                            6. ~A() {cout << "Destroying A ";}
                            friend ostream& operator << (ostream& os, const A& right);
                            friend istream& operator << (istream& is, A& right);
                            };
                            ostream& operator << (ostream& os, const A& right) {
                            7. os << "num: " << right.num;
                            8. return os;
                            }
                            istream& operator << (istream& is, A& right) {
                            9. is >> right.num;
                            10. return is;
                            }
                            
                            class B: public A{
                            private:
                            double d;
                            public:
                            11. B():A() {cout << "Default Constructor B "; d = 0;}
                            12. B(double f):A() {cout << "Constructor B(double a) "; d = f;}
                            13. B(int a, double f):A(a) {cout << "Constructor B(int, double) "; d = f;}
                            14. void increment(int a) {num = num + 2*a;}
                            15. ~B() {cout << "Destroying B ";}
                            friend ostream& operator << (ostream& os, const B& right);
                            friend istream& operator << (istream& is, B& right);
                            };
                            ostream& operator << (ostream& os, const B& right) {
                            16. os << "num: " << right.num << " d: " << right.d;
                            17. return os;
                            }
                            istream& operator << (istream& is, B& right) {
                            18. is >> right.num >> right.d;
                            19. return is;
                            }
                            
                            int main () {
                            20. A objA;
                            21. A anotherA(3);
                            22. B objB;
                            23. B anotherB(3.4);
                            24. B thirdB(2, 1.2);
                            25. cout << objA << endl;
                            26. cout << anotherA << endl;
                            27. cout << objB << endl;
                            28. cout << anotherB << endl;
                            29. cout << thirdB << endl;
                            30. A& refA = thirdB;
                            31. refA.increment(4);
                            32. objA.increment(3);
                            33. anotherA.increment(3);
                            34. anotherB.increment(2);
                            35. thirdB.increment(2);
                            36. cout << objA << endl;
                            37. cout << anotherA << endl;
                            38. cout << refA << endl;
                            39. cout << objB << endl;
                            40. cout << anotherB << endl;
                            41. cout << thirdB << endl;
                            42. return 0;
                            }

Explanation / Answer

#include <iostream>
using namespace std;

class A {
protected:
int num;
public:
1. A() {cout << "Default Constructor A "; num = 0;}
2. A(int a) {cout << "Constructor A(int a) "; num = a;}
3. int getNum () {return num;};
4. void setNum (int a) {num = a;}
5. void increment(int a) {num = num + a;}
6. ~A() {cout << "Destroying A ";}
friend ostream& operator << (ostream& os, const A& right);
friend istream& operator << (istream& is, A& right);
};
ostream& operator << (ostream& os, const A& right) {
7. os << "num: " << right.num;
8. return os;
}
istream& operator << (istream& is, A& right) {
9. is >> right.num;
10. return is;
}

class B: public A{
private:
double d;
public:
11. B():A() {cout << "Default Constructor B "; d = 0;}
12. B(double f):A() {cout << "Constructor B(double a) "; d = f;}
13. B(int a, double f):A(a) {cout << "Constructor B(int, double) "; d = f;}
14. void increment(int a) {num = num + 2*a;}
15. ~B() {cout << "Destroying B ";}
friend ostream& operator << (ostream& os, const B& right);
friend istream& operator << (istream& is, B& right);
};
ostream& operator << (ostream& os, const B& right) {
16. os << "num: " << right.num << " d: " << right.d;
17. return os;
}
istream& operator << (istream& is, B& right) {
18. is >> right.num >> right.d;
19. return is;
}

int main () {
20. A objA;
// since object of type A is created its constructor will be called so it prints. line 1 will be executed.
// Default Constructor A
21. A anotherA(3);
//since object of type A with argument is created its argumented constructor will be called so it prints. line 2 will be executed.
// Constructor A(int a)
22. B objB;
// since B is inherited from A.
// since object of type B with is created its argumented constructor will be called so it prints. line 1 and 11 will be executed.
// Default Constructor A
// Default Constructor B
23. B anotherB(3.4);
// since B is inherited from A.
// since object of type B with double argument with is created its argumented constructor will be called so it prints.line 1 and 12 will be executed.
// Default Constructor A
// Constructor B(double a)
24. B thirdB(2, 1.2);
// since B is inherited from A.
// since object of type B with double argument with is created its argumented constructor will be called so it prints.
// line 2 and 13 will be executed.
//Constructor A(int a)
// Constructor B(int, double)
25. cout << objA << endl;
// since operator << is overloaded it executes line 7 thus it prints
// num: 0
26. cout << anotherA << endl;
// since operator << is overloaded it executes line 7 thus it prints
// num: 3
27. cout << objB << endl;
// since operator << is overloaded it executes line 16 thus it prints
// num: 0 d: 0
28. cout << anotherB << endl;
// since operator << is overloaded it executes line 16 thus it prints
// num: 0 d: 3.4
29. cout << thirdB << endl;
// since operator << is overloaded it executes line 16 thus it prints
// num: 2 d: 1.2
30. A& refA = thirdB;
// now refA points to thirdB means both are same.
31. refA.increment(4);
// for refA, increment will be called line 14 will be executed num = 2+2*4 = 2+8 = 10 now num will be 10.
// now for thirdB object num will be num = num + 4 => num = 2+4 = 6 (line 5 will be executed).
32. objA.increment(3);
// increment will be called line 5 will be executed thus num = num+3 =0+3 now num will be 3.
33. anotherA.increment(3);
// increment will be called line 5 will be executed thus num = num+3 now num = 3+3 = 6 now num will be 6
34. anotherB.increment(2);
// increment will be called line 14 will be executed num = num + 2*2 = num = 0+4 = 4 thus num will be 4.
35. thirdB.increment(2);
// increment will be called line 14 will be executed num = num+2*2 = 6+2*2 = 6+4 =10
36. cout << objA << endl;
// since operator << is overloaded it executes line 7 thus it prints
// num: 3
37. cout << anotherA << endl;
// since operator << is overloaded it executes line 7 thus it prints
// num: 6
38. cout << refA << endl;
// since operator << is overloaded it executes line 7 thus it prints
// num: 10
39. cout << objB << endl;
// since operator << is overloaded it executes line 16 thus it prints
// num: 0 d: 0
40. cout << anotherB << endl;
// since operator << is overloaded it executes line 16 thus it prints
// num: 4 d: 3.4
41. cout << thirdB << endl;
// since operator << is overloaded it executes line 16 thus it prints
// num: 10 d: 1.2
//since we have reached end of execution.
// destructors will be called in reverse automatically. thus
// Destroying B
// Destroying A
// Destroying B
// Destroying A
// Destroying B
// Destroying A
// Destroying A
// Destroying A
42. return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote