Stoycho_KK

някакъв клас

Mar 26th, 2021 (edited)
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. /*MAIN*/
  2. #include<iostream>
  3. #include"Dev.h"
  4.  
  5. int main() {
  6.     Dev anton(19, false, "Anton", "Pashov");
  7.  
  8.     Dev movingAnton(std::move(anton));
  9.  
  10.     movingAnton.print(std::cout);
  11. }
  12.  
  13. /*DEV.H*/
  14. #pragma once
  15. #pragma warning(disable:4996)
  16. #include<iostream>
  17. #include<cstring>
  18.  
  19. class Dev {
  20. private:
  21.     unsigned int age;
  22.     char* firstName;
  23.     char* secondName;
  24.     bool graduated;
  25. public:
  26.     //construct:
  27.     Dev() : age(18), graduated(false), firstName(nullptr), secondName(nullptr) {}; //default constructor
  28.  
  29.         //const -> because we only read and never change the value
  30.     Dev(const unsigned int& _age, const bool& _grad, const char* fName, const char* sName) : age(_age), graduated(_grad), firstName(copyDynamic(fName)), secondName(copyDynamic(sName)) {};
  31.  
  32.     //copy constructor:
  33.     Dev(const Dev& other);
  34.  
  35.     //move:
  36.     Dev(Dev&&) noexcept;
  37.  
  38.     //set:
  39.     void setAge(const unsigned int& _age);
  40.     void setGrad(const bool& grad);
  41.     void setFName(const char* fName);
  42.     void setSName(const char* sName);
  43.  
  44.     //get:
  45.     unsigned int getAge() const;
  46.     bool getGrad() const;
  47.     char* getFName() const;
  48.     char* getSName() const;
  49.  
  50.     //functions:
  51.     void print(std::ostream& os);
  52.  
  53.     //overloads:
  54.         //why i got into infinite loop?
  55.     Dev& operator=(const Dev& other);
  56.  
  57.     bool operator==(const Dev& other);
  58.  
  59.     //not necessary:
  60.     char* copyDynamic(const char* str);
  61.  
  62.     void copyEntity(const Dev& other);
  63.  
  64.     void free();
  65.     //destruct:
  66.     ~Dev();
  67. };
  68.  
  69. /*DEV.CPP*/
  70. #include "Dev.h"
  71.  
  72. Dev::Dev(const Dev& other) {
  73.     copyEntity(other);
  74. }
  75.  
  76. Dev::Dev(Dev&& other) noexcept {
  77.     this->age = std::move(other.age);
  78.     this->firstName = std::move(other.firstName);
  79.     this->secondName = std::move(other.secondName);
  80.     this->graduated = std::move(other.graduated);
  81.     other.firstName = other.secondName = nullptr;
  82. }
  83.  
  84. void Dev::setAge(const unsigned int& _age) {
  85.     this->age = _age;
  86. }
  87.  
  88. void Dev::setGrad(const bool& grad) {
  89.     this->graduated = grad;
  90. }
  91.  
  92. void Dev::setFName(const char* fName) {
  93.     delete[] this->firstName;
  94.     this->firstName = copyDynamic(fName);
  95. }
  96.  
  97. void Dev::setSName(const char* sName) {
  98.     delete[] this->secondName;
  99.     this->secondName = copyDynamic(sName);
  100. }
  101.  
  102. unsigned int Dev::getAge() const {
  103.     return this->age;
  104. }
  105.  
  106. bool Dev::getGrad()const {
  107.     return this->graduated;
  108. }
  109.  
  110. char* Dev::getFName()const {
  111.     return this->firstName;
  112. }
  113.  
  114. char* Dev::getSName() const {
  115.     return this->secondName;
  116. }
  117.  
  118. void Dev::print(std::ostream& os) {
  119.     os << "\n===========DEV===========\n" <<
  120.         this->firstName << " " << this->secondName <<
  121.         "\nGraduated: ";
  122.     if (this->graduated)
  123.         os << "Yes\n";
  124.     else
  125.         os << "No\n";
  126.     os << "Age: " << this->age;
  127. }
  128.  
  129. Dev& Dev::operator=(const Dev& other) {
  130.     if (this != &other) {
  131.         this->free();
  132.         this->copyEntity(other);
  133.     }
  134.     return *this;
  135. }
  136.  
  137. bool Dev::operator==(const Dev& other) {
  138.     return strcmp(this->firstName, other.firstName) == 0 &&
  139.         strcmp(this->secondName, other.secondName) == 0 &&
  140.         this->age == other.age && this->graduated == other.graduated;
  141. }
  142.  
  143. char* Dev::copyDynamic(const char* str) {
  144.     char* result = new char[strlen(str) + 1];
  145.     strcpy(result, str);
  146.     return result;
  147. }
  148.  
  149. void Dev::copyEntity(const Dev& other) {
  150.     this->age = other.age;
  151.     this->graduated = other.graduated;
  152.     this->firstName = copyDynamic(other.firstName);
  153.     this->secondName = copyDynamic(other.secondName);
  154. }
  155.  
  156. void Dev::free() {
  157.     delete[] this->firstName;
  158.     delete[] this->secondName;
  159. }
  160.  
  161. Dev::~Dev() {
  162.     if (this->firstName != nullptr) {
  163.         std::cout << "\nDeleted: " << this->getFName() << std::endl;
  164.         this->free();
  165.     }
  166. }
  167.  
  168.  
Add Comment
Please, Sign In to add comment