1 / 24

class A { public: A(); ~A(); }; A :: A() { cout <<"constructor A"<< endl ; } A :: ~A() {

class A { public: A(); ~A(); }; A :: A() { cout <<"constructor A"<< endl ; } A :: ~A() { cout <<"destructor A"<< endl ; }. int main() { int x; A Test; A * Test2; cout <<"Test2 Stuff"<< endl ; Test2 = new A; delete Test2; cin >>x; return 0; }.

aria
Download Presentation

class A { public: A(); ~A(); }; A :: A() { cout <<"constructor A"<< endl ; } A :: ~A() {

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. class A { public: A(); ~A(); }; A :: A() { cout<<"constructor A"<<endl; } A :: ~A() { cout<<"destructor A"<<endl; } int main() { int x; A Test; A * Test2; cout<<"Test2 Stuff"<<endl; Test2 = new A; delete Test2; cin>>x; return 0; }

  2. constructor A is output because of the code “A Test;” Test2 Stuff outputs, because of the code “cout” constructor A is output because of the code “new A” destructor A is called because of the code “delete Test2” destructor A is also called because Test goes out of scope at the end of the function.

  3. class A { public: A(); ~A(); }; A :: A() { cout<<"constructor A"<<endl; } A :: ~A() { cout<<"destructor A"<<endl; } void TestFunc1(A t) { cout<<"In TestFunc1"<<endl; } void TestFunc2(A * t) { cout<<"In TestFunc2"<<endl; } int main() { int x; A Test; A * Test2; Test2 = new A; cout<<"Calling TestFunc1"<<endl; TestFunc1(Test); cout<<"Returned TestFunc1"<<endl; cout<<"Calling TestFunc2"<<endl; TestFunc2(Test2); cout<<"Returned TestFunc2"<<endl; cin>>x; return 0; }

  4. Constructor A is output from the code “A Test” Constructor A output from the code “new A” Calling TestFunc1 is output because of “cout” No constructor is called for the passed variable, because there is no copy constructor, therefore In TestFunc1 is output because of the cout. Upon leaving TestFunc1, the parameter goes out of scope, Destructor A is output The “cout” causes Returned TestFunc1 to output The “cout” causes Calling TestFunc2 to output No constructor is called for the passed variable, because the parameter is passed as a pointer. In TestFunc2 is the output. Since the parameter is passed as a pointer, no destructor is called. The “cout” causes Returned from TestFunc2 is then output Finally the destructor for Test is called and destructor A is output.

  5. class A { public: A(); ~A(); }; class B : public A { public: B(); ~B(); }; B :: B() { cout<<"constructor B"<<endl; } B :: ~B() { cout<<"destructor B"<<endl; } A :: A() { cout<<"constructor A"<<endl; } A :: ~A() { cout<<"destructor A"<<endl; } intmain() { intx; B Test; B * Test2; cout<<"Before new B"<<endl; Test2 = new B; delete Test2; cin>>x; return 0; }

  6. constructor A and constructor B output because of the code “B Test.” Since B is a subclass of A, first A is constructed then B is constructed. The next code to output a value is the “cout”, so Before new B is output. constructor A and constructor B output because of the code “new B.” Since B is a subclass of A, first A is destructor B and destructor A are output because of the code “delete Test2” Since Test2 is of class B and B is a subclass of A, first B’s destructor is called then A’s destructor is called. destructor B and destructor A are output because of the Test goes out of scope. Since Test2 is of class B and B is a subclass of A, first B’s destructor is called then A’s destructor is called.

  7. class A { public: A(); ~A(); void T(); }; class B : public A { public: B(); ~B(); void T(); }; B :: B() { } B :: ~B() {} A :: A() { } A :: ~A() {} void A :: T() { cout<<"A - T"<<endl; } void B :: T() { cout<<"B - T"<<endl; } intmain() { intx; B Test; B * Test2; A Test3; A * Test4; cout<<"Test.T"<<endl; Test.T(); Test2 = new B; cout<<"Test2.T"<<endl; Test2->T(); delete Test2; cout<<"Test3.T"<<endl; Test3.T(); Test4 = new A; cout<<"Test5.T"<<endl; Test4->T(); delete Test4; cin>>x; return 0; }

  8. Note there is not code in the destructors or constructors, so nothing is output when they are called and therefore are ignored in this example. Test.T is output via the “cout” statement. Since “Test” is declared as a B object, then B’s method is called via the call Test.T, so B – T is output. Test2->T is output via the “cout” statement. Since “Test2” is declared as a pointer to a B object, then B’s method is called via the call Test2->T, so B – T is output. Test3.T is output via the “cout” statement. Since “Test3” is declared as an A object, then A’s method is called via the call Test3.T, so A – T is output. Test4->T is output via the “cout” statement. Since “Test4” is declared as a pointer to an A object, then A’s method is called via the call Test4->T, so A – T is output.

  9. class A { public: A(); ~A(); void T(); }; class B : public A { public: B(); ~B(); void T(); }; B :: B() { } B :: ~B() {} A :: A() { } A :: ~A() {} void A :: T() { cout<<"A - T"<<endl; } void B :: T() { cout<<"B - T"<<endl; } intmain() { intx; B Test; A * Test2; A Test3; A * Test4; cout<<"Test.T"<<endl; Test.T(); Test2 = new B; cout<<"Test2.T"<<endl; Test2->T(); delete Test2; cout<<"Test3.T"<<endl; Test3.T(); Test4 = new A; cout<<"Test5.T"<<endl; Test4->T(); delete Test4; cin>>x; return 0; }

  10. Note there is not code in the destructors or constructors, so nothing is output when they are called and therefore are ignored in this example. Test.T is output via the “cout” statement. Since “Test” is declared as a B object, then B’s method is called via the call Test.T, so B – T is output. Test2->T is output via the “cout” statement. Since “Test2” is declared as a pointer to a Aobject, then A’s method is called via the call since it is statically scoped. Test2->T, so A – T is output. Test3.T is output via the “cout” statement. Since “Test3” is declared as an A object, then A’s method is called via the call Test3.T, so A – T is output. Test4->T is output via the “cout” statement. Since “Test4” is declared as a pointer to an A object, then A’s method is called via the call Test4->T, so A – T is output.

  11. class A { public: A(); ~A(); virtual void T(); }; class B : public A { public: B(); ~B(); void T(); }; B :: B() {} B :: ~B() {} A :: A(){ } A :: ~A(){} void A :: T() { cout<<"A - T"<<endl; } void B :: T() { cout<<"B - T"<<endl; } intmain() { intx; B Test; B * Test2; A Test3; A * Test4; cout<<"Test.T"<<endl; Test.T(); Test2 = new B; cout<<"Test2.T"<<endl; Test2->T(); delete Test2; cout<<"Test3.T"<<endl; Test3.T(); Test4 = new B; cout<<"Test5.T"<<endl; Test4->T(); delete Test4; cin>>x; return 0; }

  12. Note there is not code in the destructors or constructors, so nothing is output when they are called and therefore are ignored in this example. Test.T is output via the “cout” statement. Since “Test” is declared as a B object, then B’s method is called via the call Test.T, so B – T is output. Test2->T is output via the “cout” statement. Since “Test2” is declared as a pointer to a B object, then B’s method is called via the call Test2->T, so B – T is output. Test3.T is output via the “cout” statement. Since “Test3” is declared as an A object, then A’s method is called via the call Test3.T, so A – T is output. Test4->T is output via the “cout” statement. Since “Test4” is declared as a pointer to an A object, but allocated to a B object, since T is defined as virtual, then B’s method is called via the call Test4->T, so B – T is output.

  13. class A { public: A(); ~A(); A(A & el); }; A :: A (A & el) { cout<<"Copy Constructor A"<<endl; } A :: A() { cout<<"constructor A"<<endl; } A :: ~A() { cout<<"destructor A"<<endl; } void TestFunc1(A t) { cout<<"In TestFunc1"<<endl; } void TestFunc2(A * t) { cout<<"In TestFunc2"<<endl; } intmain() { intx; A Test; A * Test2; Test2 = new A; cout<<"Calling TestFunc1"<<endl; TestFunc1(Test); cout<<"Returned TestFunc1"<<endl; cout<<"Calling TestFunc2"<<endl; TestFunc2(Test2); cout<<"Returned TestFunc2"<<endl; cin>>x; return 0; }

  14. Constructor A is output from the code “A Test” Constructor A output from the code “new A” Calling TestFunc1 is output because of “cout” Upon entering TestFunc1, the parameter calls the copy constructor, since one is defined and therefore Copy Constructor A is output. In TestFunc1 is output because of “cout” Upon leaving TestFunc1, the parameter goes out of scope and the destructor is called, therefore destructor A is output. Returned TestFunc1 is output because of “cout” Calling TestFunc2 is output because of “cout” Since TestFunc2 passes it’s parameter as a pointer, no copy constructor is called. Since TestFunc2 passes it’s parameter as a pointer, no destructor is called. In Test Func2 is output Returned TestFunc2 is output because of “cout” When Test goes out of scope, the destructor is called and destructor A is output. Test2 is never destroyed, memory leak.

  15. class A { public: A(); ~A(); A(A & el); }; A :: A (A & el) { cout<<"Copy Constructor A"<<endl; } A :: A() { cout<<"constructor A"<<endl; } A :: ~A() { cout<<"destructor A"<<endl; } void TestFunc1(A & t) { cout<<"In TestFunc1"<<endl; } void TestFunc2(A * t) { cout<<"In TestFunc2"<<endl; } intmain() { intx; A Test; A * Test2; Test2 = new A; cout<<"Calling TestFunc1"<<endl; TestFunc1(Test); cout<<"Returned TestFunc1"<<endl; cin>>x; return 0; }

  16. Constructor A is output from the code “A Test” Constructor A output from the code “new A” Calling TestFunc1 is output because of “cout” Since TestFunc1 passes it’s parameter by reference, no copy constructor is called. In TestFunc1 is output because of “cout” Since TestFunc1 passes it’s parameter by reference, no destructor is called. Returned TestFunc1 is output because of “cout” When Test goes out of scope, the destructor is called and destructor A is output. Test2 is never destroyed, memory leak.

  17. class A { public: A(); ~A(); }; A :: A() { cout<<"constructor A"<<endl; } A :: ~A() { cout<<"destructor A"<<endl; } void Question1() { A Test[3]; } int main() { int x; Question1(); cin>>x; return 0; }

  18. constructor A is output 3 times, once for each element in the array. Since Test is declared as an automatic variable, it is automatically disposed of when Test goes out of scope, so destructor A is output 3 times.

  19. class A { public: A(); ~A(); }; A :: A() { cout<<"constructor A"<<endl; } A :: ~A() { cout<<"destructor A"<<endl; } void Question1() { A * Test = new A[3]; } int main() { int x; Question1(); cin>>x; return 0; }  

  20. constructor A is output 3 times, once for each element in the array. No destructor is called, since Test is declared as a pointer to A.

  21. class A { public: A(); ~A(); }; A :: A() { cout<<"constructor A"<<endl; } A :: ~A() { cout<<"destructor A"<<endl; } void Question1() { A * Test = new A[3]; delete Test; } int main() { int x; Question1(); cin>>x; return 0; }

  22. constructor A is output 3 times, once for each element in the array. A destructor is only called once since delete is only called on Test, but no an array called Test.

  23. class A { public: A(); ~A(); }; A :: A() { cout<<"constructor A"<<endl; } A :: ~A() { cout<<"destructor A"<<endl; } void Question1() { A * Test = new A[3]; delete [] Test; } int _tmain(intargc, _TCHAR* argv[]) { int x; Question1(); cin>>x; return 0; }

  24. constructor A is output 3 times, once for each element in the array. destructor A is output 3 times, once for each element in the array.

More Related