Slavik9510

Untitled

Oct 6th, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #pragma once
  2. #include <string.h>
  3. class Battery
  4. {
  5. private:
  6.     char* name;
  7.     int size;
  8. public:
  9.     Battery(char* name, int size)
  10.     {
  11.         if (name == nullptr)
  12.         {
  13.             this->name = nullptr;
  14.         }
  15.         else
  16.         {
  17.             this->name = new char(strlen(name) + 1);
  18.         }
  19.         this->size = size;
  20.     }
  21.     Battery(const Battery& battery)
  22.     {
  23.         this->size = battery.size;
  24.         if (this->name != nullptr)
  25.         {
  26.             delete[] this->name;
  27.         }
  28.         if (battery.name == nullptr)
  29.         {
  30.             this->name = nullptr;
  31.             return;
  32.         }
  33.         this->name = new char[strlen(battery.name) + 1];
  34.         strcpy(this->name, battery.name);
  35.     }
  36.     ~Battery()
  37.     {
  38.         if (name != nullptr)
  39.         {
  40.             delete[] name;
  41.         }
  42.     }
  43.     void setName(char* name)
  44.     {
  45.         if (this->name != nullptr)
  46.         {
  47.             delete[] this->name;
  48.         }
  49.         if (name == nullptr)
  50.         {
  51.             this->name = nullptr;
  52.             return;
  53.         }
  54.         this->name = new char[strlen(name) + 1];
  55.         strcpy(this->name, name);
  56.     }
  57.     char* getName()
  58.     {
  59.         char* toReturn = new char[strlen(this->name) + 1];
  60.         strcpy(toReturn, this->name);
  61.         return toReturn;
  62.     }
  63.     void setSize(int size)
  64.     {
  65.         this->size = size;
  66.     }
  67.     int getSize()
  68.     {
  69.         return size;
  70.     }
  71. };
Advertisement
Add Comment
Please, Sign In to add comment