You are on page 1of 1

#include<iostream> using namespace std; class BasicCopy { private: int value1, value2; public: BasicCopy(int tmpval1, int tmpval2):value1(tmpval1),

value2(tmpval2) { cout<<"BasicCopy parameterized ctor called\n"; } /* Not providing our own copy ctor in this example BasicCopy(const BasicCopy& object) { cout<<"BasicCopy copy ctor called\n"; value1 = object.value1; value2 = object.value2; } */ void display() { cout<<"Value 1 : "<<value1<<"\n"; cout<<"Value 2 : "<<value2<<"\n"; } ~BasicCopy() { cout<<"BasicCopy Dtor called\n\n"; } }; int main() { BasicCopy object_1(10,30); cout<<"Calling object_1 display\n"; object_1.display(); { // creating object2 in a different scope cout<<"\nCreating a new object_2 from an already existing object_1\n"; BasicCopy object_2(object_1); //compilers copy ctor called cout<<"Calling object_2 display\n"; object_2.display(); }//end of scope for object_2 and hence its dtor is called cout<<"Calling object_1 display again\n"; object_1.display(); //evrything works fine return 0; }

You might also like