Question

In: Computer Science

What does this program output (make sure you can explain why the output is what it...

What does this program output
(make sure you can explain why the output is what it is)

#include <iostream>
using namespace std;

class A {
   int i;
public:
   A() {cout << "A()\n" ;}
   A(int x) :i(x) {cout << "A(int)\n" ;}
   A(const A& a) : i(a.i) {cout << "A(const A&)\n" ;}
   ~A() {cout << "A()\n" ;}
};

class B {
   A a;
public:
   B() {cout << "B()\n" ;}
   B(int x) :a(x) {cout << "B(int)\n" ;}
   B(const B& b) : a(b.a) {cout << "B(const B&)\n" ;}
   ~B() {cout << "~B()\n" ;}
};

B func(B k) {return k;}

int main() {
   B b1(2), b2;
   b2=func(b1);
   return 0;
}

Solutions

Expert Solution

The basic rule is constructors are always called in this order - base classes to derived
destructors are always called in reverse order - i.e derived class destructors first upto to base class destructors.

In this example since B derives from A, when ever an object of B is created, the corresponding constructor in A is called first. Then B's constructor.
Whenever an object of B goes out of scope/destroyed, B's destructor is called first and then A's destructor.


The output of the program is
A(int)
B(int)
A()
B()
A(const A&)
B(const B&)
A(const A&)
B(const B&)
~B()
A()
~B()
A()
~B()
A()
~B()
A()

- In main(), object b1 is created . this leads to 2 constructor calls A(int) and B(int)
- next object b2 is created, this leads to 2 constructor calls A() , B()
-Now function func() is called. Since it receives parameter by value, a copy of b1 is made using copy constructor. So this leads to A(const A&) , B(const B&)
-Next when the function returns, the value from k is copied into an intermediate object i.e return value of func(). This again leads A(const A&) , B(const B&)
-After the function returns, k is destroyed. So this leads to ~B() and ~A()
-After the return value is assigned to b2, that intermediate object is destroyed again leading to ~B() and ~A()
-Next object b1 and b2 go out of scope/destroyed . Hence 2 sets of ~B() and ~A()


Related Solutions

explain why cellular signaling is crucial in biology. You will make sure to mention what are...
explain why cellular signaling is crucial in biology. You will make sure to mention what are the two basic signaling switches and what is the general flow of signaling in a cell
This program is in C++ Please kindly make sure that the output exactly matches the test...
This program is in C++ Please kindly make sure that the output exactly matches the test case so I can vote a thumbs up. If it doesn't the program is wrong. Test Cases and the given code are given after the prompt Prompt: Modify the given code to: 1.. Store the data in Binary file and access it   in Random Access mode.   2.Replace Class with Structure for Employee and Department. 3. Inside each structure, replace all string variables with array...
Problem 5 This problem is designed to make sure you can write a program that swaps...
Problem 5 This problem is designed to make sure you can write a program that swaps data passed into it such that the caller's data has been swapped. This is something that is done very frequently in manipulating Data Structures. The Solution / Test Requirements I want your program to demonstrate that you understand how to swap information passed into a function and have the calling routine print out the data before and after the call to verify the data...
1.Explain in a short paragraph what a heat engine is and what it does. Make sure...
1.Explain in a short paragraph what a heat engine is and what it does. Make sure your explanation includes: (a) what is the purpose of a heat engine, (b) what does a heat engine need to achieve that purpose, and (c) what limits how well a heat engine can achieve that purpose ("efficiency")? 2.The idea that evaporation is a cooling process is a familiar and intuitive one. What sounds counter-intuitive at first is this claim: "Boiling is a cooling process"...
Elizabeth Holmes issue. Create a plan by which you can make sure this does not happen...
Elizabeth Holmes issue. Create a plan by which you can make sure this does not happen in your own company ?
What is some way you can make sure that the only focus is on the best...
What is some way you can make sure that the only focus is on the best possible performance of your investment? Do you think it would be hard for someone who usually works with older clients preparing for retirement to also be able to advise a 25-year-old who is starting a career?
The output of the following code is "11 0" But I am not sure why, can...
The output of the following code is "11 0" But I am not sure why, can someone please explain to me clearly and concisely why this is the case? main(){ int n=1; if(fork()) wait(&n); else n=n+10; printf("%d\n", n); exit(0); }
Explain the Leontief paradox. Make sure to explain why it is a paradox. Provide at least...
Explain the Leontief paradox. Make sure to explain why it is a paradox. Provide at least three explanations for this paradox.
solve the problem make sure to explain in words what you did with the problem and...
solve the problem make sure to explain in words what you did with the problem and state your conclusions in terms of the problem. Part I: Choose to do one of the following: 1) Test the claim that the mean Unit 3 Test scores of data set 7176 is greater than the mean Unit 3 Test scores of data set 7178 at the .05 significance level Class 7176 Class 7178 Unit Test 3 Course Grade Attendance Unit Test 3 Course...
can you detail explain how solved: "is just to practice" and make sure I have the...
can you detail explain how solved: "is just to practice" and make sure I have the same answer is from the economy for engineer and scientists You are deputy chief of the Space Federation Force (SFF), which means you do whatever the chief of the SFF says. She says you evaluate at 10% per year unless told otherwise. Space Fuel Inc. is considering establishing a new propellant depot to provide space vehicles a refueling point in their trek to Mars....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT