#include <iostream>
#include <string>
class Int
{ protected:
int val;
string name;
public:
Int(void)
{ cout << "Creating uninitialised anonymous Int\n";
name="anonymous"; }
Int(string nm)
{ cout << "Creating uninitialised Int called " << nm << "\n";
name=nm; }
Int(int v)
{ cout << "Initialising an anonymous Int called to " << v << "\n";
val=v;
name="anonymous"; }
Int(string nm, int v)
{ cout << "Initialising an Int called " << nm << " to " << v << "\n";
val=v;
name=nm; }
Int(const Int & v)
{ cout << "Initialising an anonymous Int to " << v.val << "\n";
val=v.val;
name="anonymous"; }
Int(string nm, const Int & v)
{ cout << "Initialising an Int called " << nm << " to " << v.val << "\n";
val=v.val;
name=nm; }
void setname(string nm)
{ cout << "Setting name of Int valued " << val << " to " << nm << "\n";
name=nm; }
Int & operator=(const Int & v)
{ cout << "Reassigning an Int called " << name << " to " << v.val << "\n";
val=v.val;
return *this; }
Int & operator=(int v)
{ cout << "Reassigning an Int called " << name << " to " << v << "\n";
val=v;
return *this; }
operator int(void)
{ return val; } };
void main(void)
{ Int x=32, z("z",21);
Int yyy("yyy", 99);
x.setname("x");
cout << " --- step 1\n";
x=yyy+2;
cout << " --- step 2\n";
yyy=x*z;
cout << " --- step 3\n";
cout << "yyy = " << yyy << "\n"; }
Initialising an Int called z to 21
Initialising an Int called yyy to 99
Setting name of Int valued 32 to x
--- step 1
Reassigning an Int called x to 101
--- step 2
Reassigning an Int called yyy to 2121
--- step 3
yyy = 2121