Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ / Pet.cpp / / 2 a - Lab - 0 6 - Pets / / #include #include #include #include #include Pet.h using namespace

// Pet.cpp
//2a-Lab-06-Pets
//
#include
#include
#include
#include
#include "Pet.h"
using namespace std;
// This is a way to properly initialize (out-of-line) a static variable.
size_t Pet::_population =0;
Pet::Pet(std::string name, long id, int num_limbs) : _name(name),_id(id),_num_limbs(num_limbs){
_population++;
}
Pet::~Pet(){
_population--;
}
string Pet::get_name() const {
return _name;
}
long Pet::get_id() const {
return _id;
}
int Pet::get_num_limbs() const {
return _num_limbs;
}
bool Pet::set_name(string name){
if(!name.empty()){
_name = name;
return true;
}
return false;
}
bool Pet::set_id(long id){
if(id >=0){
_id = id;
return true;
}
return false;
}
bool Pet::set_num_limbs(int num_limbs){
if(num_limbs >=0){
_num_limbs = num_limbs;
return true;
}
return false;
}
string Pet::to_string() const {
stringstream ss;
ss "(Name: "_name ", ID: "_id ", Limb Count: "_num_limbs ")";
return ss.str();
}
// Fill in the supplied pets vector with n pets whose
// properties are chosen randomly.
// Don't mess with this method more than necessary.
void Pet::get_n_pets(size_t n, std::vector& pets, int name_len){
pets.resize(n);
long prev_id =0;
for (size_t i =0; i n; i++){
long id = prev_id +1+ rand()%10;
pets[i].set_id(id);
pets[i].set_num_limbs(rand()%9); // up to arachnids
string name = make_a_name(name_len);
pets[i].set_name(name);
prev_id = id;
}
}
//---------------------------------------------------------------------
string Pet::make_a_name(int len){
const string vowels = "aeiou";
const string consonants ="bcdfghjklmnpqrstvwxyz";
string name ="";
bool is_vowel =(rand()%5==0);
bool is_consonants =(rand()%21==0);
int i =0;
while (i len){
if (is_vowel){
name += vowels[rand()% vowels.length()];
} else if(is_consonants){
name += consonants[rand()% consonants.length()];
}
is_vowel =!is_vowel;
i++;
}
return name;
}
bool operator==(const Pet& pet1, const Pet& pet2){
return (pet1.get_name()== pet2.get_name() && pet1.get_id()== pet2.get_id() && pet1.get_num_limbs()== pet2.get_num_limbs());
}
bool operator!=(const Pet& pet1, const Pet& pet2){
return !(pet1== pet2);
}
ostream& operator(ostream& os, const Pet& pet){
os pet.to_string();
return os;
#ifndef Pet_h
#define Pet_h
#include
#include
#include
#include
using namespace std;
class Tests;
class Pet {
public:
string _name;
long _id;
int _num_limbs;
static size_t _population;
Pet(string name ="", long id =-1, int num_limbs =0);
~Pet();
string get_name() const;
long get_id() const;
int get_num_limbs() const;
bool set_name(string name);
bool set_id(long id);
bool set_num_limbs(int num_limbs);
string to_string() const;
static void get_n_pets(size_t n, std::vector& pets, int name_length);
static size_t get_population(){ return _population; }
static string make_a_name(int len);
};
std::ostream& operator(std::ostream& os, const Pet& pet);
bool operator==(const Pet& pet1, const Pet& pet2);
bool operator!=(const Pet& pet1, const Pet& pet2);
#endif /* Pet_h */Test Output
Check failed. I called make_a_name(7):
And got . But I expected to get: kuxeyos
You think that's it?
&
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Progress Monitoring Data Tracking Organizer

Authors: Teacher'S Aid Publications

1st Edition

B0B7QCNRJ1

More Books

Students also viewed these Databases questions