-1

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!

1 Answers1

0

You can create two header files citizen.h and animal.h, move the definition of class Citizen into citizen.h and move the definition of class Animal to animal.h, and replace these definitions with #include "citizen.h" and #include "animal.h". Ideally, you should also add include guards to both header files. Both header files should also have the line #include <string>, because they are using std::string. Also, animals.h should #include "citizen.h", because it requires the declaration of class Citizen.

It is generally not advisable to use using namespace std; in header files, so you should add std:: were necessary in the header files. See this question for further information:

Why is "using namespace std;" considered bad practice?

After doing that, you can create two new source files citizen.cpp and animal.cpp and move all function definitions from the header to the corresponding source file, converting the function definitions to function declarations in the header file. Each source file should also #include its corresponding header file.

After doing this, your files should have the following content:

citizen.h

#ifndef CITIZEN_H_INCLUDED
#define CITIZEN_H_INCLUDED

#include <string>

class Citizen{
    private:
        std::string name;
    public:
        Citizen( std::string name );
        std::string getName();
        std::string getAddress();
        void setName( std::string name );
};

#endif

animals.h

#ifndef ANIMALS_H_INCLUDED
#define ANIMALS_H_INCLUDED

#include <string>

#include "citizen.h"

class Animal{
    private:
        std::string name;
        int age;
        Citizen* owner;
    public:
        Animal( std::string name, int age, Citizen* owner );
        std::string getName();
        int getAge();
        Citizen* getCitizen();
        void setName( std::string name );
        void setAge( int age );
        void setCitizen( Citizen* owner);
};

#endif

citizen.cpp

#include "citizen.h"

Citizen::Citizen( std::string name ) {
    this->name = name;
}

std::string Citizen::getName() {
    return name;
}

std::string Citizen::getAddress() {
    return address;
}
        
void Citizen::setName( std::string name ) {
    this->name = name;
}

animal.cpp

#include "animal.h"

Animal::Animal( std::string name, int age, Citizen* owner) {
    this->name = name;
    this->age = age;
    this->owner = owner;
}

std::string Animal::getName() {
    return name;
}

int Animal::getAge() {
    return age;
}

Citizen* Animal::getCitizen() {
    return owner;
}

void Animal::setName( std::string name ) {
    this->name = name;
}

void Animal::setAge(int age) {
    this->age = age;
}

void Animal::setCitizen( Citizen* owner ) {
    this->owner = owner;
}

main.cpp

#include<iostream>
#include<string>

#include "citizen.h"
#include "animal.h"

using namespace std;

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 code above will not compile, for the same reason that your original code does not compile (address is not declared).

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • I would advise to use `#include "citizen.h"` instead of `#include ` when including personal/local headers – Ladislus Jun 03 '22 at 12:44
  • @Ladislus: Yes, you are correct. Thanks for pointing it out. I had written it correctly in the code, but written it wrongly in the explanation. I believe I have fixed it now. – Andreas Wenzel Jun 03 '22 at 12:46