dburner

intel

Nov 6th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. //============================================================================
  2. // Name        : test2.cpp
  3. // Author      :
  4. // Version     :
  5. // Copyright   : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <exception>
  11. #include <string>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15.  
  16. using namespace std;
  17.  
  18. class OutOfMemoryException{};
  19.  
  20. class Person
  21. {
  22. private:
  23.     char* name;
  24.     char* surname;
  25.  
  26. public:
  27.     Person()
  28.     {
  29.         name    = (char*)calloc(1,sizeof(char));
  30.         surname = (char*)calloc(1,sizeof(char));
  31.     }
  32.  
  33.     Person(const char* aName, const char* aSurname)
  34.     {
  35.         name = strdup(aName);
  36.         if (name == NULL) {
  37.             throw OutOfMemoryException();
  38.         }
  39.  
  40.         surname = strdup(aSurname);
  41.         if (surname == NULL){
  42.             throw OutOfMemoryException();
  43.         }
  44.     }
  45.  
  46.     virtual ~Person()
  47.     {
  48.         free(surname);
  49.         free(name   );
  50.     }
  51.  
  52.     Person(const Person& person)
  53.     {
  54.         name    = strdup(person.name   );
  55.         surname = strdup(person.surname);
  56.     }
  57.  
  58.     void setName(const char* value)
  59.     {
  60.         free(name);
  61.         name = strdup(value);
  62.         if(name == NULL){
  63.             cout<<"Out of memory";
  64.             throw OutOfMemoryException();
  65.         }
  66.     }
  67.     void setSurname(const char* value)
  68.     {
  69.         free(surname); //pt memory leak
  70.         surname = strdup(value);
  71.     }
  72.     virtual char* getFullName() const
  73.     {
  74.         int fullNameSize = strlen(name) + strlen(surname) + strlen(" ") + 1;
  75.         char* fullname = new char[fullNameSize];
  76.         strcpy(fullname, name);
  77.         strcat(fullname, " ");
  78.         strcat(fullname, surname);
  79.         return fullname;
  80.     }
  81.  
  82.     Person& operator = (const Person& other)
  83.     {
  84.         if (this == &other){
  85.             return *this;
  86.         }
  87.         setName   (other.name   );
  88.         setSurname(other.surname);
  89.  
  90.         return (*this);
  91.     }
  92. };
  93.  
  94.  
  95. void setRandomName(Person& person)
  96. {
  97.     const char* names[3]    = { "Maria",     "Ion",     "Vasile"    };
  98.     const char* surnames[3] = { "Marinescu", "Ionescu", "Vasilescu" };
  99.  
  100.     int namePosition    = rand()%3;
  101.     int surnamePosition = rand()%3;
  102.  
  103.     person.setName   (names[namePosition]      );
  104.     person.setSurname(surnames[surnamePosition]);
  105. }
  106.  
  107. void setLongName(Person& person){
  108.     const unsigned int LongNameSize = 1000000;
  109.  
  110.     char name[LongNameSize + 1];
  111.     memset(name, 'a', LongNameSize);
  112.  
  113.     name[LongNameSize] = '\0';
  114.  
  115.     person.setName(name);
  116.     person.setSurname(name);
  117. }
  118.  
  119. void printPersonName(const Person &person){
  120.     const char* name = person.getFullName();
  121.     cout<<name<<endl;
  122.     delete[] name;
  123. }
  124.  
  125. class Doctor : public Person
  126. {
  127. public:
  128.     Doctor(const char* aName, const char* aSurname)
  129.         :Person(aName, aSurname)
  130.     {
  131.     }
  132.  
  133.     char* getFullName() const
  134.     {
  135.         char doctorPrefix[] = "Dr. ";
  136.  
  137.         char* personName = Person::getFullName();
  138.         char* doctorName = (char*)malloc(strlen(personName) + strlen(doctorPrefix) + 1);
  139.  
  140.         strcpy(doctorName, doctorPrefix);
  141.         strcat(doctorName, personName  );
  142.  
  143.         delete[] personName;
  144.  
  145.         return doctorName;
  146.     }
  147. };
  148.  
  149. class PersonWithTitle : public Person
  150. {
  151. private:
  152.     char* title;
  153. public:
  154.     PersonWithTitle(const char* aName, const char* aSurname, const char* aTitle)
  155.         :Person(aName, aSurname)
  156.     {
  157.         title = strdup(aTitle);
  158.         if (title == NULL){
  159.             throw OutOfMemoryException();
  160.         }
  161.     }
  162.  
  163.     ~PersonWithTitle()
  164.     {
  165.         free(title);
  166.     }
  167.  
  168.     char* getFullName() const
  169.     {
  170.         char* personName = Person::getFullName();
  171.         char* doctorName = (char*)malloc(strlen(personName) + strlen(title) + 1);
  172.  
  173.         strcpy(doctorName, title);
  174.         strcat(doctorName, personName  );
  175.  
  176.         delete[] personName;
  177.  
  178.         return doctorName;
  179.     }
  180. };
  181.  
  182. int main()
  183. {
  184.     const unsigned int NumberOfPersons = 3;
  185.     Person* persons[NumberOfPersons];
  186.  
  187.     persons[0] = new Person("Ion",    "Ionescu"  );
  188.     persons[1] = new Doctor("Maria",  "Marinescu");
  189.     persons[2] = new PersonWithTitle("Vasile", "Vasilescu", "Mr. ");
  190.  
  191.     for(unsigned int i = 0; i < NumberOfPersons; i++)
  192.             printf("Person %d: %s\n", i, persons[i]->getFullName());
  193.  
  194.     for(unsigned int i = 0; i < NumberOfPersons; i++)
  195.         delete persons[i];
  196.  
  197.     printf("Program finished successfully!\n");
  198.  
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment