Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copy Assignment Operator
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Name {
- int m_len;
- char *m_p;
- public:
- Name(const char *p)
- {
- m_len = strlen(p);
- m_p = (char *)malloc(m_len + 1);
- ////
- strcpy(m_p, p);
- }
- Name(const Name &r)
- {
- m_len = r.m_len;
- m_p = (char *)malloc(m_len + 1);
- ///
- strcpy(m_p, r.m_p);
- }
- Name &operator=(const Name &r)
- {
- if (this == &r)
- return *this;
- free(m_p);
- m_len = r.m_len;
- m_p = (char *)malloc(m_len + 1);
- ///
- strcpy(m_p, r.m_p);
- return *this;
- }
- ~Name()
- {
- free(m_p);
- }
- void display()const
- {
- cout << "(" << m_p << ")" << endl;
- }
- ////
- };
- int main()
- {
- Name x{"Kerem Vatandas"};
- x = x;
- x.display();
- return 0;
- }
Add Comment
Please, Sign In to add comment