#include <iostream>
using namespace std;
class MyClass
{
int * data;
public :
MyClass( int value)
{
data = new int ( value) ;
cout << "consturctor called" << endl;
}
~MyClass( )
{
delete data;
cout << "destructor called" << endl;
}
MyClass( const MyClass & other)
{
data = new int ( * other.data ) ;
cout << "copy constructor called" << endl;
}
MyClass& operator= ( const MyClass & other)
{
if ( this ! = & other)
{
delete data;
data = new int ( * other.data ) ;
}
return * this ;
}
int getValue( ) {
return * data;
}
} ;
int main( ) {
MyClass obj1( 10 ) ;
MyClass obj2 = obj1; // Copy constructor
MyClass obj3( 20 ) ;
obj3 = obj1; // Assignment operator
std:: cout << "obj1 value: " << obj1.getValue ( ) << std:: endl ;
std:: cout << "obj2 value: " << obj2.getValue ( ) << std:: endl ;
std:: cout << "obj3 value: " << obj3.getValue ( ) << std:: endl ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgTXlDbGFzcwp7CglpbnQgKiBkYXRhOwoJcHVibGljOgoJTXlDbGFzcyhpbnQgdmFsdWUpCgl7CgkJZGF0YSA9IG5ldyBpbnQodmFsdWUpOwoJCWNvdXQ8PCJjb25zdHVyY3RvciBjYWxsZWQiPDxlbmRsOwoJfQoJCgl+TXlDbGFzcygpCgl7CgkJZGVsZXRlIGRhdGE7CgkJY291dDw8ImRlc3RydWN0b3IgY2FsbGVkIjw8ZW5kbDsKCX0KCQoJTXlDbGFzcyhjb25zdCBNeUNsYXNzICZvdGhlcikKCXsKCQlkYXRhID0gbmV3IGludCgqb3RoZXIuZGF0YSk7CgkJY291dDw8ImNvcHkgY29uc3RydWN0b3IgY2FsbGVkIjw8ZW5kbDsKCX0KCglNeUNsYXNzJiBvcGVyYXRvcj0oY29uc3QgTXlDbGFzcyAmb3RoZXIpCgl7CgkJaWYodGhpcyAhPSAmb3RoZXIpCgkJewoJCQlkZWxldGUgZGF0YTsKCQkJZGF0YSA9IG5ldyBpbnQoKm90aGVyLmRhdGEpOw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