Untitled
unknown
plain_text
2 years ago
1.8 kB
9
Indexable
#include "Animal.h"
#include <cstring>
#include <stdexcept>
Animal::Animal(const char* name, const char* species, int age) : age(age) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->species = new char[strlen(species) + 1];
strcpy(this->species, species);
}
Animal::Animal(const Animal& other) : age(other.age) {
name = new char[strlen(other.name) + 1];
strcpy(name, other.name);
species = new char[strlen(other.species) + 1];
strcpy(species, other.species);
}
Animal::Animal(Animal&& other) : name(other.name), species(other.species), age(other.age) {
other.name = nullptr;
other.species = nullptr;
}
Animal::~Animal() {
delete[] name;
delete[] species;
}
Animal& Animal::operator=(const Animal& other) {
if (this != &other) {
delete[] name;
delete[] species;
name = new char[strlen(other.name) + 1];
strcpy(name, other.name);
species = new char[strlen(other.species) + 1];
strcpy(species, other.species);
age = other.age;
}
return *this;
}
const char* Animal::getName() const {
return name;
}
const char* Animal::getSpecies() const {
return species;
}
int Animal::getAge() const {
return age;
}
bool Animal::setName(const char* name) {
if (name == nullptr) return false;
delete[] this->name;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
return true;
}
bool Animal::setSpecies(const char* species) {
if (species == nullptr) return false;
delete[] this->species;
this->species = new char[strlen(species) + 1];
strcpy(this->species, species);
return true;
}
bool Animal::setAge(int age) {
if (age < 0) return false;
this->age = age;
return true;
}Editor is loading...
Leave a Comment