Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*MAIN*/
- #include<iostream>
- #include"Dev.h"
- int main() {
- Dev anton(19, false, "Anton", "Pashov");
- Dev movingAnton(std::move(anton));
- movingAnton.print(std::cout);
- }
- /*DEV.H*/
- #pragma once
- #pragma warning(disable:4996)
- #include<iostream>
- #include<cstring>
- class Dev {
- private:
- unsigned int age;
- char* firstName;
- char* secondName;
- bool graduated;
- public:
- //construct:
- Dev() : age(18), graduated(false), firstName(nullptr), secondName(nullptr) {}; //default constructor
- //const -> because we only read and never change the value
- Dev(const unsigned int& _age, const bool& _grad, const char* fName, const char* sName) : age(_age), graduated(_grad), firstName(copyDynamic(fName)), secondName(copyDynamic(sName)) {};
- //copy constructor:
- Dev(const Dev& other);
- //move:
- Dev(Dev&&) noexcept;
- //set:
- void setAge(const unsigned int& _age);
- void setGrad(const bool& grad);
- void setFName(const char* fName);
- void setSName(const char* sName);
- //get:
- unsigned int getAge() const;
- bool getGrad() const;
- char* getFName() const;
- char* getSName() const;
- //functions:
- void print(std::ostream& os);
- //overloads:
- //why i got into infinite loop?
- Dev& operator=(const Dev& other);
- bool operator==(const Dev& other);
- //not necessary:
- char* copyDynamic(const char* str);
- void copyEntity(const Dev& other);
- void free();
- //destruct:
- ~Dev();
- };
- /*DEV.CPP*/
- #include "Dev.h"
- Dev::Dev(const Dev& other) {
- copyEntity(other);
- }
- Dev::Dev(Dev&& other) noexcept {
- this->age = std::move(other.age);
- this->firstName = std::move(other.firstName);
- this->secondName = std::move(other.secondName);
- this->graduated = std::move(other.graduated);
- other.firstName = other.secondName = nullptr;
- }
- void Dev::setAge(const unsigned int& _age) {
- this->age = _age;
- }
- void Dev::setGrad(const bool& grad) {
- this->graduated = grad;
- }
- void Dev::setFName(const char* fName) {
- delete[] this->firstName;
- this->firstName = copyDynamic(fName);
- }
- void Dev::setSName(const char* sName) {
- delete[] this->secondName;
- this->secondName = copyDynamic(sName);
- }
- unsigned int Dev::getAge() const {
- return this->age;
- }
- bool Dev::getGrad()const {
- return this->graduated;
- }
- char* Dev::getFName()const {
- return this->firstName;
- }
- char* Dev::getSName() const {
- return this->secondName;
- }
- void Dev::print(std::ostream& os) {
- os << "\n===========DEV===========\n" <<
- this->firstName << " " << this->secondName <<
- "\nGraduated: ";
- if (this->graduated)
- os << "Yes\n";
- else
- os << "No\n";
- os << "Age: " << this->age;
- }
- Dev& Dev::operator=(const Dev& other) {
- if (this != &other) {
- this->free();
- this->copyEntity(other);
- }
- return *this;
- }
- bool Dev::operator==(const Dev& other) {
- return strcmp(this->firstName, other.firstName) == 0 &&
- strcmp(this->secondName, other.secondName) == 0 &&
- this->age == other.age && this->graduated == other.graduated;
- }
- char* Dev::copyDynamic(const char* str) {
- char* result = new char[strlen(str) + 1];
- strcpy(result, str);
- return result;
- }
- void Dev::copyEntity(const Dev& other) {
- this->age = other.age;
- this->graduated = other.graduated;
- this->firstName = copyDynamic(other.firstName);
- this->secondName = copyDynamic(other.secondName);
- }
- void Dev::free() {
- delete[] this->firstName;
- delete[] this->secondName;
- }
- Dev::~Dev() {
- if (this->firstName != nullptr) {
- std::cout << "\nDeleted: " << this->getFName() << std::endl;
- this->free();
- }
- }
Add Comment
Please, Sign In to add comment