Question

What will be printed by the code below What will be printed by the code below...

What will be printed by the code below

What will be printed by the code below

#include <iostream>
using namespace std;

class A {
public:
A() {cout << "+1";}
~A() {cout << "-1";}
};

class B: public A {
public:
B() {cout << "+2";}
~B() {cout << "-2";}
};

class C: public B {
publc:
C() {cout << "+3";}
~C() {cout << "-3";}
};

int main() {
A a1;
B b1;
A *a2 = new A;
B *b2; = new B;
delete a2;

delete b2;
return 0;
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;
class A {
public:
A() {cout << "+1";}
~A() {cout << "-1";}
};

class B: public A {
public:
B() {cout << "+2";}
~B() {cout << "-2";}
};

class C: public B {
public:
C() {cout << "+3";}
~C() {cout << "-3";}
};

int main() {
A a1;//due to this line +1 is printed as contructor A() is invoked is invoked internally
B b1;//due to this line +1+2 is printed as it invokes parent A() and then B()is invoked internally
A *a2 = new A; // due to this line +1 is printed as contructor A() is invoked is invoked internally
B *b2 = new B; //due to this line +1+2 is printed as it invokes parent A() and then B() is invoked internally
delete a2;//due to this line -1 is printed as destructor ~A() is invoked is invoked internally

delete b2;//due to this line -2-1 is printed as destructor ~B() and then parent destructor ~A() is invoked internally
//now a1 and b1 are not used, so garbage collector destroys them from latest to first ie b1 then a1;
//due to b1 getting destroyed -2-1 is printed as destructor ~B() and then parent destructor ~A()
//due to a1 getting destroyed -1 is printed as destructor ~A() is invoked is invoked internally

//so output is +1+1+2+1+1+2-1-2-1-2-1-1
return 0;
  
}

Add a comment
Know the answer?
Add Answer to:
What will be printed by the code below What will be printed by the code below...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is printed by the following program? (read the code carefully) #include <iostream> using namespace std;...

    What is printed by the following program? (read the code carefully) #include <iostream> using namespace std; class A { public: int x; }; int main() { A *a = new A; A *b = new A; a->x=1; b->x=1; if (a == b) { cout << "black"; } else { cout << "white"; } delete a; delete b; return 0; }

  • Redesign your Array class from lab6 as a class template to work with the application below....

    Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() {   Array<char> c(3);   c.setValue(0,'c');   c.setValue(1,'s');   c.setValue(2,'c');   cout << c;   Array<int> i(3);   i.setValue(0,1);   i.setValue(1,2);   i.setValue(2,5);   cout << i;   Array<int> j(3);   j.setValue(0,10);   j.setValue(1,20);   j.setValue(2,50);   cout << j;   Array<int> ij;   ij = i + j;   cout << ij;...

  • There are many problems with the program below. Describe 5 of these problems in detail. For...

    There are many problems with the program below. Describe 5 of these problems in detail. For each, also explain how you could fix or avoid the issue. #include <iostream> using namespace std; class Duck { public: virtual void quack() { cout << "Quack" << endl; } }; class RubberDuck : public Duck { public: RubberDuck() { array = NULL; } RubberDuck(int size) { array = new int[size]; array[0] = 1; array[1] = 2; array[2] = 3; } ~RubberDuck() { delete...

  • 2. What is the value of x alter the following code is executed # include<iostream> using...

    2. What is the value of x alter the following code is executed # include<iostream> using namespace std; int main int t-0, c- 0,x-3; while (c < 4) t=t+x; cout << x return 0; a) 3 b) 21 c) 24 d) 48 3. What is the output of the following code? # include<iostream> using namespace std; int main0 int a- 3; for (int i 5; i >0; i) a a+i cout << a << “ “ return 0; d) 8...

  • 5. What’s wrong with the code below? Feel free to compile and run the program to...

    5. What’s wrong with the code below? Feel free to compile and run the program to investigate. [6] #include <iostream> using namespace std; class HW1 { public:    char *word;    HW1() { }    ~HW1()    { delete [] word;    } }; void foo(HW1 a) { //some operation here } int main() { HW1 a; a.word = new char[80]; strcpy(a.word, "abc"); foo(a); return 0; }

  • UUS TOP Question 17: What is the output of the code presented below? int y =...

    UUS TOP Question 17: What is the output of the code presented below? int y = 5; int x 7; intx yPtr = &x; *yPtr = 8 + 4 * 6/2 + ( 5 cout<oxyPtr; *yPtr = *yPtr + *yPtr / 2; cout<<xyPtr; 2 ); Question 18: The following is true about the code below. Tine Time: 1 SetMinute(unsigned int ninutes) this.minutes - minutes; return this; Question 19: What is the output of the code presented below? include <iostream using...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • 47 What is printed out by the code listed below the classes (i.e., below the line)?...

    47 What is printed out by the code listed below the classes (i.e., below the line)? class Animal public virtual void whoAreYou () cout << "an Animal\n";h class Insect public Animal public void whoAreYou() cout < "an Insect\n"; class Spider public Insect public void whoAreYou() cout << "a Spider\n"; Animal* a1-new Animal; Animal* a2 = new insect ; Animal* a3 = new spider; al->whoAreYou ); a2->whoAreYou ); ??->whoAreYou ( ) ; an Insect, a Spider, a Spider O an Animal,...

  • Convert the below code into if else selection: #include <iostream> using namespace std; int main() {...

    Convert the below code into if else selection: #include <iostream> using namespace std; int main() { int num; sin. >> num; switch (num) { case 1: cout << "Casel: Value is: << num << endl; break; case 2: break; case 3: cout << "Case3: Value is: " << num << endl; break; default: cout << "Default: Value is: << num << endl; break; } return; }

  • C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your...

    C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your code in the source code where the comment “// your code” locates. After you finish the implementation, please also provide the output of your program. #include <iostream> using namespace std; class Shape { protected: // your code public: void setWidth (int w) { // your code } void setHeight (int h) { // your code } }; class Rectangle: public Shape { public: int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT