How would I go about splitting these classes into different .cpp files? The program is meant to simulate a dog parlor that gets a dog's name, age, it's owners name (if it has an owner) and store them into an array of object pointers.
#include<iostream>
#include<string>
using namespace std;
class Citizen{
private:
string name;
public:
Citizen(string name){
this->name = name;
}
string getName(){
return name;
}
string getAddress(){
return address;
}
void setName(string name){
this->name = name;
}
};
class Animal{
private:
string name;
int age;
Citizen* owner;
public:
Animal(string name, int age, Citizen* owner){
this->name = name;
this->age = age;
this->owner = owner;
}
string getName(){
return name;
}
int getAge(){
return age;
}
Citizen* getCitizen(){
return owner;
}
void setName(string name){
this->name = name;
}
void setAge(int age){
this->age = age;
}
void setCitizen(Citizen* owner){
this->owner = owner;
}
};
void swapNames(Animal* a1, Animal* a2){
string temp = a1->getName();
a1->setName(a2->getName());
a2->setName(temp);
}
int main(){
Animal* animals[10];
int numAnimals = 0;
int choice;
string name;
int age;
string ownerName;
string hasOwner;
string firstName;
string secondName;
bool foundFirst = false;
bool foundSecond = false;
Animal* firstAnimal;
Animal* secondAnimal;
do{
cout << "Enter 1 to add a dog, 2 to swap names or 3 to stop: ";
cin >> choice;
if(choice == 1){
cout << "Please enter the name of the dog: ";
cin >> name;
cout << "Please enter the age: ";
cin >> age;
cout << "Does the dog have an owner: ";
cin >> hasOwner;
//in case of yes take owner's name else don't take
//compare hasOwner value with string "Yes" || "yes" || "y" || "Y"
if(hasOwner == "Yes" || hasOwner == "yes" || hasOwner == "y" || hasOwner == "Y"){
cout << "Please enter the owners name: ";
cin >> ownerName;
cout << "Please enter the address: ";
cin >> address;
Citizen* owner = new Citizen(ownerName);
animals[numAnimals] = new Animal(name, age, owner);
}
else{
animals[numAnimals] = new Animal(name, age, NULL);
}
numAnimals++;
cout << "The list of dogs are as follows: ";
for(int i = 0; i < numAnimals; i++){
cout << animals[i]->getName() << " (Owner: ";
if(animals[i]->getCitizen() == NULL){
cout << "hoping for adoption)";
}
else{
cout << animals[i]->getCitizen()->getName() << ")";
}
if(i != numAnimals - 1){
cout << ", ";
}
}
cout << endl;
}
else if(choice == 2){
cout << "Enter name of first dog: ";
cin >> firstName;
cout << "Enter name of second dog: ";
cin >> secondName;
for(int i = 0; i < numAnimals; i++){
if(animals[i]->getName() == firstName){
firstAnimal = animals[i];
foundFirst = true;
}
if(animals[i]->getName() == secondName){
secondAnimal = animals[i];
foundSecond = true;
}
}
if(foundFirst && foundSecond){
swapNames(firstAnimal, secondAnimal);
cout << "The list of dogs are as follows: ";
for(int i = 0; i < numAnimals; i++){
cout << animals[i]->getName() << " (Owner: ";
if(animals[i]->getCitizen() == NULL){
cout << "hoping for adoption)";
}
else{
cout << animals[i]->getCitizen()->getName() << ")";
}
if(i != numAnimals - 1){
cout << ", ";
}
}
cout << endl;
}
else{
cout << "No dog found" << endl;
}
}
}while(choice != 3);
return 0;
}
The following is given in the citizen.cpp file:
#include "citizen.h"
Citizen::Citizen() {
}
Citizen::~Citizen() {
}
And here's an example of the output:
Enter 1 to add a dog, 2 to swap names or 3 to stop: 1
Please enter the name of the dog: Jake
Please enter the age: 12
Does the dog have an owner: Yes
Please enter the owners name: Steve
Please enter the address: Brooklyn
The list of dogs are as follows: Jake(Owner: Steve)
Enter 1 to add a dog, 2 to swap names or 3 to stop: 1
Please enter the name of the dog: John
Please enter the age: 4
Does the dog have an owner: No
The list of dogs are as follows: Jake(Owner: Steve), John(Owner: hoping for adoption)
Enter 1 to add a dog, 2 to swap names or 3 to stop: 1
Please enter the name of the dog: Lemon
Please enter the age: 9
Does the dog have an owner: Yes
Please enter the owners name: Craig
Please enter the address: Brooklyn
The list of dogs are as follows: Jake(Owner: Steve), John(Owner: hoping for adoption)
,Lemon(Owner: Craig)
Enter 1 to add a dog, 2 to swap names or 3 to stop: 2
Enter name of first dog: Jake
Enter name of second dog: Lemon
The list of dogs are as follows: Lemon(Owner: Steve),
John(Owner: hoping for adoption), Jake(Owner: Craig)
Enter 1 to add a dog, 2 to swap names or 3 to stop: 3
I'm just having a bit of a hard time figuring this out so any help is much appreciated!