OOP related Question

19 views
Skip to first unread message

saad azhar

unread,
May 10, 2013, 3:44:22 AM5/10/13
to cs1003-s...@googlegroups.com
RESPECTED TEACHERS
                           Pasting a code with output please Explain according to output step wise in detail. I Shall be very thankfull ..



#include <iostream>

using namespace std;

class B{

public:

  B() { cout << "B default constructor" << endl; }

  B( B& ) { cout << "B copy constructor" << endl; }

  ~B() { cout << "B destructor" << endl; }

};



class C{

public:

  C() { cout << "C default constructor" << endl; }

  C( C& ) { cout << "C copy constructor" << endl; }

  ~C() { cout << "C destructor" << endl; }

};

int main(){

  C c;

  B b;

  cout << "before function call" << endl;

  c = f( b );

  cout << "after function call" << endl;

}

C f( B bb ){

  C cc;

  return cc;

}


NOW OUTPUT:-



C default constructor

B default constructor

before function call

B copy constructor

C default constructor

C copy constructor

C destructor

B destructor

C destructor

after function call

B destructor

C destructor



it is actually a slide from a lecture in topic OOP DETAILED. 

saad azhar

unread,
May 10, 2013, 3:47:24 AM5/10/13
to cs1003-s...@googlegroups.com
Sir sami if possible please explain by your self because you must be having this slide..You might not be able to understand the pasted code please have a look on slides ..This is from OOP detailed.. Slide page is 78,79,80

saad azhar

unread,
May 10, 2013, 3:49:12 AM5/10/13
to cs1003-s...@googlegroups.com
Actually i am having problem in 8th and 9th line of output!! But Please explain the whole output step by step again!!


On Friday, May 10, 2013 12:44:22 AM UTC-7, saad azhar wrote:

Sami Ullah Kashif

unread,
May 10, 2013, 4:39:03 AM5/10/13
to cs1003-s...@googlegroups.com
This output is not easy to explain in an email. However I shall try tonight to explain it via email.

Regards,
Sami

Sami Ullah Kashif

unread,
May 10, 2013, 12:42:00 PM5/10/13
to cs1003-s...@googlegroups.com
Okay lets start from Line # 6:

C copy constructor

The above is called when we run the "return cc" statement. In this case, a new temporary object is created and the contents of cc are written in that object. Hence a copy constructor for class C would be called.

C destructor

Since the value of "cc' has been returned, the function f() would be "popped" from the stack. Hence all the variables that were being created would be destroyed in reverse order. The above line is printed when the object cc is destroyed.

B destructor

The above would be printed when the local object "bb" would be destroyed.

C destructor

After the function has been popped, the cursor would return to the line c = f(b). Now that temp object would be "assigned" to c. Hence the assignment function would be called. Now we don't need the temp object, so it will be destroyed and the above line will be printed.

after function call

The above is pretty much obvious.

B destructor

The above will be printed when the local object "b" in the main() function would be destroyed.

C destructor

The above will be printed when the local object "c" in the main() function would be destroyed.

Hope this clears up any confusion that you had.

Regards,
Sami
Reply all
Reply to author
Forward
0 new messages