//########################################################################
/*
Compiler silently writes 4 functions if they are not explicitly declared
1.Copy constructor
2.Copy Assignment Operator
3.Destructor
4.Default constructor(only if there is no constructor declared).
*/
class dog {};
/* equivalent to */
class dog
{
public:
dog(const dog& rhs)
{
...//Member by member initialization
};
dog& operator=(const dog& rhs)
{
//member by member copying
};
dog() {};
//1.call base class's default constructor;
//2.call data member's default constructor;
~dog() {};
//1.call base class's destructor;
//2.call data member's destructor;
};
/*
1.They are public and inline.
2.They are generated only if they are needed.
*/
//Example:
class dog
{
public:
string m_name;
dog(string name = "Bob")
{
m_name = name;
cout << "is born" << endl;
}
~dog()
{
cout << m_name << "is destroyed.\n" << endl;
}
};
int main()
{
dog dog1("Henry");
dog dog2;
dog2 = dog1;
}
/*
OUTPUT:
Henry is born.
Bob is born.
Henry is distroyed.
Henry is distroyed.
*/
/*example 2*/
class collar
{
public:
collar()
{
std::cout << "collar is born.\n";
}
};
class dog
{
collar m_collar;
//string& m_name;//this snippet will not be compiled
};
int main()
{
dog dog1;
}
/*C++ 11 updata: */
class dog
{
public:
dog() = default;
dog(string name) { ... };
};