Advertisement
Guest User

Untitled

a guest
Feb 11th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class NameCard
  6. {
  7. private:
  8.     char *name;
  9.     char *phone;
  10.     char *company;
  11.     char *position;
  12. public:
  13.     NameCard(char *name, char *phone, char *company, char *position)
  14.     {
  15.         this->name = new char(strlen(name) + 1);
  16.         this->phone = new char(strlen(phone) + 1);
  17.         this->company = new char(strlen(company) + 1);
  18.         this->position = new char(strlen(position) + 1);
  19.  
  20.         strncpy_s(this->name, strlen(name) + 1, name, strlen(name));
  21.         strncpy_s(this->phone, strlen(phone) + 1, phone, strlen(phone));
  22.         strncpy_s(this->company, strlen(company) + 1, company, strlen(company));
  23.         strncpy_s(this->position, strlen(position) + 1, position, strlen(position));
  24.     }
  25.  
  26.     NameCard(const NameCard& c)
  27.     {
  28.         this->name = new char(strlen(c.name)+1);
  29.         this->phone = new char(strlen(c.phone) + 1);
  30.         this->company = new char(strlen(c.company) + 1);
  31.         this->position = new char(strlen(c.position) + 1);
  32.        
  33.         strncpy_s(this->name, strlen(c.name) + 1, c.name, strlen(c.name));
  34.         strncpy_s(this->phone, strlen(c.phone) + 1, c.phone, strlen(c.phone));
  35.         strncpy_s(this->company, strlen(c.company) + 1, c.company, strlen(c.company));
  36.         strncpy_s(this->position, strlen(c.position) + 1, c.position, strlen(c.position));
  37.     }
  38.  
  39.     ~NameCard()
  40.     {
  41.         delete[] this->name;
  42.         delete[] this->phone;
  43.         delete[] this->company;
  44.         delete[] this->position;
  45.     }
  46.  
  47.     void ShowData()
  48.     {
  49.         cout << this->name << endl;
  50.         cout << this->phone << endl;
  51.         cout << this->company << endl;
  52.         cout << this->position << endl;
  53.     }
  54. };
  55.  
  56. int main()
  57. {
  58.     NameCard James("James", "11231123", "la", "manager");
  59.     James.ShowData();
  60.  
  61.     NameCard James2(James);
  62.     James2.ShowData();
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement