Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : test2.cpp
- // Author :
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <exception>
- #include <string>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- using namespace std;
- class OutOfMemoryException{};
- class Person
- {
- private:
- char* name;
- char* surname;
- public:
- Person()
- {
- name = (char*)calloc(1,sizeof(char));
- surname = (char*)calloc(1,sizeof(char));
- }
- Person(const char* aName, const char* aSurname)
- {
- name = strdup(aName);
- if (name == NULL) {
- throw OutOfMemoryException();
- }
- surname = strdup(aSurname);
- if (surname == NULL){
- throw OutOfMemoryException();
- }
- }
- virtual ~Person()
- {
- free(surname);
- free(name );
- }
- Person(const Person& person)
- {
- name = strdup(person.name );
- surname = strdup(person.surname);
- }
- void setName(const char* value)
- {
- free(name);
- name = strdup(value);
- if(name == NULL){
- cout<<"Out of memory";
- throw OutOfMemoryException();
- }
- }
- void setSurname(const char* value)
- {
- free(surname); //pt memory leak
- surname = strdup(value);
- }
- virtual char* getFullName() const
- {
- int fullNameSize = strlen(name) + strlen(surname) + strlen(" ") + 1;
- char* fullname = new char[fullNameSize];
- strcpy(fullname, name);
- strcat(fullname, " ");
- strcat(fullname, surname);
- return fullname;
- }
- Person& operator = (const Person& other)
- {
- if (this == &other){
- return *this;
- }
- setName (other.name );
- setSurname(other.surname);
- return (*this);
- }
- };
- void setRandomName(Person& person)
- {
- const char* names[3] = { "Maria", "Ion", "Vasile" };
- const char* surnames[3] = { "Marinescu", "Ionescu", "Vasilescu" };
- int namePosition = rand()%3;
- int surnamePosition = rand()%3;
- person.setName (names[namePosition] );
- person.setSurname(surnames[surnamePosition]);
- }
- void setLongName(Person& person){
- const unsigned int LongNameSize = 1000000;
- char name[LongNameSize + 1];
- memset(name, 'a', LongNameSize);
- name[LongNameSize] = '\0';
- person.setName(name);
- person.setSurname(name);
- }
- void printPersonName(const Person &person){
- const char* name = person.getFullName();
- cout<<name<<endl;
- delete[] name;
- }
- class Doctor : public Person
- {
- public:
- Doctor(const char* aName, const char* aSurname)
- :Person(aName, aSurname)
- {
- }
- char* getFullName() const
- {
- char doctorPrefix[] = "Dr. ";
- char* personName = Person::getFullName();
- char* doctorName = (char*)malloc(strlen(personName) + strlen(doctorPrefix) + 1);
- strcpy(doctorName, doctorPrefix);
- strcat(doctorName, personName );
- delete[] personName;
- return doctorName;
- }
- };
- class PersonWithTitle : public Person
- {
- private:
- char* title;
- public:
- PersonWithTitle(const char* aName, const char* aSurname, const char* aTitle)
- :Person(aName, aSurname)
- {
- title = strdup(aTitle);
- if (title == NULL){
- throw OutOfMemoryException();
- }
- }
- ~PersonWithTitle()
- {
- free(title);
- }
- char* getFullName() const
- {
- char* personName = Person::getFullName();
- char* doctorName = (char*)malloc(strlen(personName) + strlen(title) + 1);
- strcpy(doctorName, title);
- strcat(doctorName, personName );
- delete[] personName;
- return doctorName;
- }
- };
- int main()
- {
- const unsigned int NumberOfPersons = 3;
- Person* persons[NumberOfPersons];
- persons[0] = new Person("Ion", "Ionescu" );
- persons[1] = new Doctor("Maria", "Marinescu");
- persons[2] = new PersonWithTitle("Vasile", "Vasilescu", "Mr. ");
- for(unsigned int i = 0; i < NumberOfPersons; i++)
- printf("Person %d: %s\n", i, persons[i]->getFullName());
- for(unsigned int i = 0; i < NumberOfPersons; i++)
- delete persons[i];
- printf("Program finished successfully!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment