Advertisement
Guest User

Untitled

a guest
May 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. /* Strings: C++ style */
  5. #include <string>
  6. using namespace std;
  7.  
  8. class OverflowException {
  9. public:
  10. OverflowException() {
  11. cout << endl << "Exception created!" << endl;
  12. }
  13. OverflowException(const OverflowException&) {
  14. cout << "Exception copied!" << endl;
  15. }
  16. ~OverflowException() {
  17. cout << "Exception finished!" << endl;
  18. }
  19. };
  20.  
  21. class Vehicle {
  22. protected:
  23. char* regnr;
  24. int razg;
  25. char* razvalsts;
  26. public:
  27. Vehicle();
  28. Vehicle(char*, int, char*);
  29. virtual ~Vehicle() {
  30. cout << "Message from the \"Vehicle\" - destroyed!" <<
  31. endl;
  32. }
  33. char* GetRegnr() const {
  34. return regnr;
  35. }
  36. void SetRegnr(char* regnr) {
  37. this->regnr = regnr;
  38. }
  39. int GetRazg() const;
  40. void SetRazg(int);
  41.  
  42. char* GetRazvalsts() const {
  43. return razvalsts;
  44. }
  45. void SetRazvalsts(char* razvalsts){
  46. this->razvalsts = razvalsts;
  47. }
  48.  
  49. virtual void Print() const;
  50. };
  51.  
  52. class Car : public Vehicle{
  53. private:
  54. int dzineja_jauda;
  55. public:
  56. Car():Vehicle(), dzineja_jauda(0){
  57. }
  58. Car(char*, int, char*, int);
  59. virtual ~Car(){
  60. cout << endl << "Message from the \"Car\" - destroyed!" << endl;
  61. }
  62. int GetDzinejajauda() const{
  63. return dzineja_jauda;
  64. }
  65. void SetDzinejajauda(int dzineja_jauda){
  66. this->dzineja_jauda = dzineja_jauda;
  67. }
  68. virtual void Print() const;
  69. };
  70.  
  71. class CarPark {
  72. private:
  73. typedef Car* DPPointer;
  74. DPPointer *Nodes;
  75. static const unsigned int DEFAULT_MAX_LENGTH;
  76. unsigned int MaxLength;
  77. unsigned int Length;
  78. public:
  79. CarPark() : MaxLength(DEFAULT_MAX_LENGTH), Length(0) {
  80. Nodes = new DPPointer[MaxLength];
  81. }
  82. CarPark(unsigned int MaxLength) : MaxLength(MaxLength), Length(0) {
  83. Nodes = new DPPointer[MaxLength];
  84. }
  85. ~CarPark();
  86. static unsigned int GetDefaultMaxLength() {
  87. return DEFAULT_MAX_LENGTH;
  88. }
  89.  
  90.  
  91. int GetMaxPower() const {
  92. int nowpower = 0;
  93. for (int i = 0; i < Length; i++) {
  94. if (Nodes[i]->GetDzinejajauda() > nowpower)
  95. nowpower = Nodes[i]->GetDzinejajauda();
  96. }
  97. return nowpower;
  98. }
  99.  
  100.  
  101. int GetMaxLength() const {
  102. return MaxLength;
  103. }
  104. int GetLength() const {
  105. return Length;
  106. }
  107. void AddNode(const Car&);
  108. void Print() const;
  109. };
  110. const unsigned int CarPark::DEFAULT_MAX_LENGTH = 5;
  111.  
  112.  
  113. Vehicle::Vehicle() : regnr("LV1234"), razg(1998), razvalsts("Latvija") {
  114. }
  115.  
  116. Vehicle::Vehicle(char* Pregnr, int Prazg, char* Prazvalsts) : regnr(Pregnr) {
  117. razg = Prazg;
  118. razvalsts = Prazvalsts;
  119. }
  120. inline int Vehicle::GetRazg() const {
  121. return razg;
  122. }
  123. inline void Vehicle::SetRazg(int razg) {
  124. this->razg = razg;
  125. }
  126. inline void Vehicle::Print() const {
  127. cout << "Regnr: " << regnr << ", Razgads: " << razg << ", Razvalsts: " << razvalsts;
  128. }
  129.  
  130. Car::Car(char* Pregnr, int Prazg, char* Prazvalsts, int Pdzineja_jauda) : Vehicle(Pregnr,Prazg,Prazvalsts){
  131. dzineja_jauda = Pdzineja_jauda;
  132. }
  133.  
  134. inline void Car::Print() const{
  135. Vehicle::Print();
  136. cout << ", Dzineja jauda: " << dzineja_jauda;
  137. }
  138.  
  139.  
  140. CarPark::~CarPark() {
  141. for(unsigned int i=0; i<Length; i++)
  142. delete Nodes[i];
  143. delete [] Nodes;
  144. }
  145. void CarPark::Print() const {
  146. cout << "Nodes:" << endl;
  147. for (unsigned int i=0; i<Length; i++) {
  148. cout << (i+1) << ". ";
  149. Nodes[i]->Print();
  150. cout << "." << endl;
  151. }
  152. }
  153. void CarPark::AddNode(const Car& Node) {
  154. if (Length == MaxLength)
  155. throw OverflowException();
  156. else
  157. Nodes[Length++] = new Car(
  158. Node.GetRegnr(), Node.GetRazg(), Node.GetRazvalsts(), Node.GetDzinejajauda()
  159. );
  160. }
  161.  
  162. void main(void) {
  163. CarPark *Park = new CarPark(2);
  164. Car *D1 = new Car("DE8887", 2005, "Vacija", 150);
  165. Car D2("FR5252", 2018, "Francija", 207);
  166.  
  167. try {
  168. Park->AddNode(*D1);
  169. cout << "\nNew node has been added successfully!" << endl;
  170. }
  171. catch (const OverflowException&) {
  172. cout << "*** Error: maximal length exceeded ! ***" << endl;
  173. }
  174. catch (...) {
  175. cout << "Unknown Error!" << endl;
  176. }
  177. delete D1;
  178.  
  179. cout << "\n\nDefault maximal power (from CLASS): " <<
  180. CarPark::GetDefaultMaxLength() << "." << endl;
  181. cout << "Default maximal power (from OBJECT): " <<
  182. Park->GetDefaultMaxLength() << "." << endl;
  183. cout << "Maximal power: " << Park->GetMaxLength() << "." << endl;
  184. cout << "Current power: " << Park->GetLength() << "." << endl;
  185.  
  186. try {
  187. Park->AddNode(D2);
  188. cout << "\nNew node has been added successfully!" << endl;
  189. }
  190. catch (const OverflowException&) {
  191. cout << "*** Error: maximal length exceeded ! ***" << endl;
  192. }
  193. catch (...) {
  194. cout << "Unknown Error !" << endl;
  195. }
  196.  
  197. try {
  198. Park->AddNode(D2);
  199. cout << "\nNew node has been added successfully!" << endl;
  200. }
  201. catch (const OverflowException&) {
  202. cout << "*** Error: maximal length exceeded! ***" << endl;
  203. }
  204. catch (...) {
  205. cout << "Unknown error !" << endl;
  206. }
  207.  
  208. cout << "Maksimala dzineja jauda: " << Park->GetMaxPower() << "." << endl;
  209.  
  210. Park->Print();
  211.  
  212. delete Park;
  213.  
  214. while (kbhit())
  215. getch();
  216. getch();
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement