Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Question
  6. {
  7.     char* name;
  8.     char answer;
  9.     int index;
  10.     int points;
  11.     void Free();
  12.     void CopyFrom(const Question& other);
  13. public:
  14.     Question();
  15.     Question(const char* name, char answer, int index, int points);
  16.     Question(const Question& other);
  17.     Question& operator=(const Question& other);
  18.     ~Question();
  19.     void setIndex(int index);
  20.     void setPoints(int points);
  21.     void setAnswer(char answer);
  22. };
  23. void Question::Free()
  24. {
  25.     delete[] name;
  26.     answer = '\0';
  27.     index = 0;
  28.     points = 0;
  29. }
  30. void Question::CopyFrom(const Question& other)
  31. {
  32.     int len1 = strlen(other.name);
  33.     name = new char[len1 + 1];
  34.     strcpy(name, other.name);
  35.  
  36.     answer = other.answer;
  37.  
  38.     index = other.index;
  39.  
  40.     points = other.points;
  41. }
  42. Question::Question()
  43. {
  44.     name = nullptr;
  45.     answer = '\0';
  46.     index = 0;
  47.     points = 0;
  48. }
  49. Question& Question::operator=(const Question& other)
  50. {
  51.     if (this != &other)
  52.     {
  53.         Free();
  54.         CopyFrom(other);
  55.     }
  56.     return *this;
  57. }
  58. Question::Question(const Question& other)
  59. {
  60.     CopyFrom(other);
  61. }
  62. Question::~Question()
  63. {
  64.     Free();
  65. }
  66. Question::Question(const char* name, char answer, int index, int points)
  67. {
  68.     int len = strlen(name);
  69.     this->name = new char[len + 1];
  70.     strcpy(this->name, name);
  71.  
  72.     setAnswer(answer);
  73.     setIndex(index);
  74.     setPoints(points);
  75.  
  76. }
  77. void Question::setPoints(int points)
  78. {
  79.     if (points<0)
  80.     {
  81.         points = 0;
  82.     }
  83.     this->points = points;
  84. }
  85. void Question::setIndex(int index)
  86. {
  87.     if (index<1)
  88.     {
  89.         index = 1;
  90.     }
  91.     this->index = index;
  92. }
  93. void Question::setAnswer(char answer)
  94. {
  95.     if (answer<'a' || answer>'z')
  96.     {
  97.         answer = 'a';
  98.     }
  99.     this->answer = answer;
  100. }
  101.  
  102. class Test
  103. {
  104.     char* name;
  105.     Question* questions;
  106.     int questionsSize;
  107.     int count;
  108.     void Free();
  109.     void CopyFrom(const Test& other);
  110. public:
  111.     Test(int questionsCount);
  112.     Test(const Test& other);
  113.     ~Test();
  114.     Test& operator=(const Test& other);
  115.  
  116.     bool addQuestion(const Question& q)
  117. };
  118. void Test::Free()
  119. {
  120.     delete[] name;
  121.     delete[] questions;
  122. }
  123. void Test::CopyFrom(const Test& other)
  124. {
  125.     int len1 = strlen(other.name);
  126.     name = new char[len1 + 1];
  127.     strcpy(name, other.name);
  128.  
  129.     questions = new Question[other.questionsSize];
  130.     questionsSize = other.questionsSize;
  131.     count = other.count;
  132.     for (int i = 0; i < count; i++)
  133.     {
  134.         questions[i] = other.questions[i];
  135.     }
  136.        
  137. }
  138. Test::Test(int questionsCount)
  139. {
  140.     questions = new Question[questionsCount];
  141.     questionsSize = questionsCount;
  142.     count = 0;
  143. }
  144. Test::Test(const Test& other)
  145. {
  146.     CopyFrom(other);
  147. }
  148. Test::~Test()
  149. {
  150.     Free();
  151. }
  152. Test& Test::operator=(const Test& other)
  153. {
  154.     if (this != &other)
  155.     {
  156.         Free();
  157.         CopyFrom(other);
  158.     }
  159.     return *this;
  160. }
  161. bool Test::addQuestion(const Question& q)
  162. {
  163.  
  164.     if (count == questionsSize)
  165.         return false;
  166.     questions[count] = q;
  167.     count++;
  168.     return true;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement