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 Pet.h size _ t

// Pet.cpp
//2a-Lab-06-Pets
//
#include
#include
#include
#include "Pet.h"
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--;
}
std::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(std::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;
}
std::string Pet::to_string() const {
std::ostringstream oss;
oss "(Name: "_name ", ID: "_id ", Limb Count: "_num_limbs ")";
return oss.str();
}
void Pet::get_n_pets(size_t n, std::vector& pets, int name_len){
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
pets[i].set_name(make_a_name(name_len));
prev_id = id;
}
}
std::string Pet::make_a_name(int len){
if (len =0){
return ""; // Return an empty string for len =0
}
const std::string vowels = "aeiou";
const std::string consonants ="bcdfghjklmnpqrstvwxyz";
std::string result;
// First letter is either a vowel or consonant
if (rand()%2==0){
result += vowels[rand()% vowels.length()];
} else {
result += consonants[rand()% consonants.length()];
}
for (int i =1; i len; i++){
// Alternate between vowels and consonants
if (i %2==0){
// If the previous character was a vowel, choose a consonant
if (result.back()== vowels[0]|| result.back()== vowels[1]|| result.back()== vowels[2]|| result.back()== vowels[3]|| result.back()== vowels[4]){
result += consonants[rand()% consonants.length()];
} else {// If the previous character was a consonant, choose a vowel
result += vowels[rand()% vowels.length()];
}
} else {
// Alternate between vowels and consonants
if (result.back()== vowels[0]|| result.back()== vowels[1]|| result.back()== vowels[2]|| result.back()== vowels[3]|| result.back()== vowels[4]){
result += vowels[rand()% vowels.length()];
} else {
result += consonants[rand()% consonants.length()];
}
}
}
return result;
}
std::ostream& operator(std::ostream& os, const Pet& pet){
os pet.to_string();
return os;
}
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);
} this is the code i wrote but i get wrong output the error is i called a make_a_name(6) and giot yhuukf but i expect ihukuf
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

More Books

Students also viewed these Databases questions

Question

3. Get an idea of sources to which you may turn.

Answered: 1 week ago

Question

Which form of proof do you find most persuasive? Why?

Answered: 1 week ago