#include <iostream>
#include <string>
usingnamespace std;
class Cat
{
public:
Cat();
~Cat();
int GetAge() const { return *itsage; } //inline
void SetAge(int age){ *itsage = age; } //inline
int GetWeight() const { return *itsweight; } //inline
void SetWeight(int weight) { *itsweight = weight; } //inline
string GetName() const { return *itsname; } //inline
void SetName(string name) { *itsname = name; } //inline
private:
int* itsage = nullptr;
int* itsweight = nullptr;
string* itsname = nullptr;
};
Cat::Cat()
{
itsage = newint(12);
itsweight = newint(4);
itsname = new string;
}
Cat::~Cat()
{
cout << "\nDestructor Called - Cleaning up memory.\n";
delete itsage;
delete itsweight;
delete itsname;
}
int main()
{
shortint amountofkittens;
cout << "How many kittens would you like to purchase?\n";
cin >> amountofkittens;
while (amountofkittens >= 10)
{
cout << "You cannot fit 10 or more kittens in your farm.\n";
cout << "Try buying a smaller amount: ";
cin >> amountofkittens;
}
Cat* kittens = new Cat[amountofkittens];
delete[] kittens;
kittens = nullptr;
return 0;
}
So what i need to do is i need to give the user the ability to name their own kittens, i need to make another for loop which allows the user to name their kittens 1 after the other and then another for loop to display their names, how do i achieve this?
I know how to print out their names but i don't know how to let the user name each cat.
You don't need those dynamic allocations. As it stands, you don't do anything about copies, so if your object was copied, you t application would eventually crash.
So Cat could be: ... I'll add an isNamed() method so we can check if it has a name yet.
for (i = 0; i < amountofkittens; ++i)
{
if (!kittens[i]->isNamed())
{
std::string name;
cout << "This kitten doesn't have a name. Please type one, or just press <Enter> to ignore" << std::endl;
std::getline(std::cin, name);
if (!name.empty())
kittens[i]->setName(name);
}
}