Please review my code below. I am studying the Visitor Design Pattern and I think I have implemented it correctly. Please check. I have tried to split the method definitions and the prototypes as much as possible so there are many files. Any comments are welcome. And no, this is not homework.
AsteroidBase.cpp
#include <iostream>
#include "AsteroidBase.h"
#include "SpaceShipBase.h"
CAsteroidBase::CAsteroidBase(const std::string& aAsteroidName) : iAsteroidName(aAsteroidName)
{
}
AsteroidBase.h
#ifndef ASTEROIDBASE_H
#define ASTEROIDBASE_H
class CSpaceShipBase;
class CApolloSpaceShip;
class CColumbiaSpaceShip;
class CAsteroidBase
{
public:
CAsteroidBase(const std::string& aAsteroidName);
CAsteroidBase(CAsteroidBase& aAsteroidBase) = default;
CAsteroidBase(CAsteroidBase&& aAsteroidBase) = default;
~CAsteroidBase() = default;
CAsteroidBase& operator=(CAsteroidBase& aAsteroidBase) = default;
CAsteroidBase& operator=(CAsteroidBase&& aAsteroidBase) = default;
virtual const std::string& GetAsteroidName() const = 0;
protected:
std::string iAsteroidName;
};
#endif // ASTEROIDBASE_H
SpaceShipBase.h
#ifndef SPACESHIPBASE_H
#define SPACESHIPBASE_H
#include <memory>
class CAsteroidBase;
class CExplodingAsteroid;
class CStonyAsteroid;
class CSpaceShipBase
{
public:
CSpaceShipBase(const std::string& aSpaceShipName);
CSpaceShipBase(const CSpaceShipBase& aSpaceShipBase) = default;
CSpaceShipBase(CSpaceShipBase&& aSpaceShipBase) = default;
virtual ~CSpaceShipBase() = default;
CSpaceShipBase& operator=(CSpaceShipBase& aSpaceShipBase) = default;
CSpaceShipBase& operator=(CSpaceShipBase&& aSpaceShipBase) = default;
virtual const std::string& GetSpaceShipName() const = 0;
virtual void ShipCollidesWith(std::shared_ptr<CAsteroidBase> aAsteroidBase) const = 0;
virtual void ShipCollidesWith(std::shared_ptr<CExplodingAsteroid> aExplodingAsteroid) const = 0;
virtual void ShipCollidesWith(std::shared_ptr<CStonyAsteroid> aStonyAsteroid) const = 0;
protected:
std::string iSpaceShipName;
};
#endif // SPACESHIPBASE_H
SpaceShipbase.cpp
#include <iostream>
#include "SpaceShipBase.h"
#include "AsteroidBase.h"
CSpaceShipBase::CSpaceShipBase(const std::string& aSpaceShipName) : iSpaceShipName(aSpaceShipName)
{
}
ApolloSpaceShip.cpp
#include <iostream>
#include "ApolloSpaceShip.h"
#include "AsteroidBase.h"
#include "ExplodingAsteroid.h"
#include "StonyAsteroid.h"
CApolloSpaceShip::CApolloSpaceShip(const std::string& aSpaceShipName) : CSpaceShipBase(aSpaceShipName)
{
}
const std::string& CApolloSpaceShip::GetSpaceShipName() const
{
return iSpaceShipName;
}
void CApolloSpaceShip::ShipCollidesWith(std::shared_ptr<CExplodingAsteroid> aExplodingAsteroid) const
{
std::cout << __PRETTY_FUNCTION__ << ": " << GetSpaceShipName() << " and " << aExplodingAsteroid->GetAsteroidName()
<< " collide." << std::endl;
}
void CApolloSpaceShip::ShipCollidesWith(std::shared_ptr<CStonyAsteroid> aStonyAsteroid) const
{
std::cout << __PRETTY_FUNCTION__ << ": " << GetSpaceShipName() << " and " << aStonyAsteroid->GetAsteroidName()
<< " collide." << std::endl;
}
void CApolloSpaceShip::ShipCollidesWith(std::shared_ptr<CAsteroidBase> aAsteroidBase) const
{
std::cout << __PRETTY_FUNCTION__ << ": " << GetSpaceShipName() << " and " << aAsteroidBase->GetAsteroidName()
<< " collide." << std::endl;
}
ApolloSpaceShip.h
#ifndef APOLLOSPACESHIP_H
#define APOLLOSPACESHIP_H
#include "SpaceShipBase.h"
class CAsteroidBase;
class CExplodingAsteroid;
class CStonyAsteroid;
class CApolloSpaceShip : public CSpaceShipBase
{
public:
CApolloSpaceShip(const std::string& aSpaceShipName);
CApolloSpaceShip(CApolloSpaceShip& aApolloSpaceShip) = default;
CApolloSpaceShip(CApolloSpaceShip&& aApolloSpaceShip) = default;
~CApolloSpaceShip() = default;
CApolloSpaceShip& operator=(CApolloSpaceShip& aApolloSpaceShip) = default;
CApolloSpaceShip& operator=(CApolloSpaceShip&& aApolloSpaceShip) = default;
const std::string& GetSpaceShipName() const override;
void ShipCollidesWith(std::shared_ptr<CAsteroidBase> aAsteroidBase) const override;
void ShipCollidesWith(std::shared_ptr<CExplodingAsteroid> aExplodingAsteroid) const override;
void ShipCollidesWith(std::shared_ptr<CStonyAsteroid> aStonyAsteroid) const override;
};
#endif // APOLLOSPACESHIP_H
ColumbiaSpaceShip.cpp
#include <iostream>
#include "ColumbiaSpaceShip.h"
#include "AsteroidBase.h"
#include "ExplodingAsteroid.h"
#include "StonyAsteroid.h"
CColumbiaSpaceShip::CColumbiaSpaceShip(const std::string& aSpaceShipName) : CSpaceShipBase(aSpaceShipName)
{
}
const std::string& CColumbiaSpaceShip::GetSpaceShipName() const
{
return iSpaceShipName;
}
void CColumbiaSpaceShip::ShipCollidesWith(std::shared_ptr<CExplodingAsteroid> aExplodingAsteroid) const
{
std::cout << __PRETTY_FUNCTION__ << ": " << GetSpaceShipName() << " and " << aExplodingAsteroid->GetAsteroidName()
<< " collide." << std::endl;
}
void CColumbiaSpaceShip::ShipCollidesWith(std::shared_ptr<CStonyAsteroid> aStonyAsteroid) const
{
std::cout << __PRETTY_FUNCTION__ << ": " << GetSpaceShipName() << " and " << aStonyAsteroid->GetAsteroidName()
<< " collide." << std::endl;
}
void CColumbiaSpaceShip::ShipCollidesWith(std::shared_ptr<CAsteroidBase> aAsteroidBase) const
{
std::cout << __PRETTY_FUNCTION__ << ": " << GetSpaceShipName() << " and " << aAsteroidBase->GetAsteroidName()
<< " collide." << std::endl;
}
ColumbiaSpaceShip.h
#ifndef COLUMBIASPACESHIP_H
#define COLUMBIASPACESHIP_H
#include "SpaceShipBase.h"
class CAsteroidBase;
class CExplodingAsteroid;
class CStonyAsteroid;
class CColumbiaSpaceShip : public CSpaceShipBase
{
public:
CColumbiaSpaceShip(const std::string& aSpaceShipName);
CColumbiaSpaceShip(CColumbiaSpaceShip& aColumbiaSpaceShip) = default;
CColumbiaSpaceShip(CColumbiaSpaceShip&& aColumbiaSpaceShip) = default;
~CColumbiaSpaceShip() = default;
CColumbiaSpaceShip& operator=(CColumbiaSpaceShip& aColumbiaSpaceShip) = default;
CColumbiaSpaceShip& operator=(CColumbiaSpaceShip&& aColumbiaSpaceShip) = default;
const std::string& GetSpaceShipName() const override;
void ShipCollidesWith(std::shared_ptr<CAsteroidBase> aAsteroidBase) const override;
void ShipCollidesWith(std::shared_ptr<CExplodingAsteroid> aExplodingAsteroid) const override;
void ShipCollidesWith(std::shared_ptr<CStonyAsteroid> aStonyAsteroid) const override;
};
#endif // COLUMBIASPACESHIP_H
ExplodingAsteroid.cpp
#include <iostream>
#include "ExplodingAsteroid.h"
CExplodingAsteroid::CExplodingAsteroid(const std::string& aAsteroidName) : CAsteroidBase(aAsteroidName)
{
}
const std::string& CExplodingAsteroid::GetAsteroidName() const
{
return iAsteroidName;
}
ExplodingAsteroid.h
#ifndef EXPLODINGASTEROID_H
#define EXPLODINGASTEROID_H
#include "AsteroidBase.h"
class CExplodingAsteroid : public CAsteroidBase
{
public:
CExplodingAsteroid(const std::string& aAsteroidName);
CExplodingAsteroid(CExplodingAsteroid& aExplodingAsteroid) = default;
CExplodingAsteroid(CExplodingAsteroid&& aExplodingAsteroid) = default;
~CExplodingAsteroid() = default;
CExplodingAsteroid& operator=(CExplodingAsteroid& aExplodingAsteroid) = default;
CExplodingAsteroid& operator=(CExplodingAsteroid&& aExplodingAsteroid) = default;
const std::string& GetAsteroidName() const override;
};
#endif // EXPLODINGASTEROID_H
StonyAsteroid.cpp
#include <iostream>
#include "StonyAsteroid.h"
CStonyAsteroid::CStonyAsteroid(const std::string& aAsteroidName) : CAsteroidBase(aAsteroidName)
{
}
const std::string& CStonyAsteroid::GetAsteroidName() const
{
return iAsteroidName;
}
StonyAsteroid.h
#ifndef STONYASTEROID_H
#define STONYASTEROID_H
#include "AsteroidBase.h"
class CStonyAsteroid : public CAsteroidBase
{
public:
CStonyAsteroid(const std::string& aAsteroidName);
CStonyAsteroid(CStonyAsteroid& aStonyAsteroid) = default;
CStonyAsteroid(CStonyAsteroid&& aStonyAsteroid) = default;
virtual ~CStonyAsteroid() = default;
CStonyAsteroid& operator=(CStonyAsteroid& aStonyAsteroid) = default;
CStonyAsteroid& operator=(CStonyAsteroid&& aStonyAsteroid) = default;
const std::string& GetAsteroidName() const override;
};
#endif // STONYASTEROID_H
main.cpp
#include <vector>
#include <iostream>
#include <memory>
#include "AsteroidBase.h"
#include "SpaceShipBase.h"
#include "ApolloSpaceShip.h"
#include "ColumbiaSpaceShip.h"
#include "ExplodingAsteroid.h"
#include "StonyAsteroid.h"
typedef std::vector<std::shared_ptr<CSpaceShipBase>> TSpaceShipBaseVec;
typedef std::vector<std::shared_ptr<CAsteroidBase>> TAsteroidBaseVec;
TSpaceShipBaseVec CreateShipVec()
{
std::shared_ptr<CSpaceShipBase> spaceShip_1 = std::make_shared<CApolloSpaceShip>("Apollo 6");
std::shared_ptr<CSpaceShipBase> spaceShip_2 = std::make_shared<CApolloSpaceShip>("Apollo 13");
std::shared_ptr<CSpaceShipBase> spaceShip_3 = std::make_shared<CApolloSpaceShip>("Apollo 17");
std::shared_ptr<CSpaceShipBase> spaceShip_4 = std::make_shared<CColumbiaSpaceShip>("Columbia");
TSpaceShipBaseVec spaceShipBaseVec;
spaceShipBaseVec.push_back(spaceShip_1);
spaceShipBaseVec.push_back(spaceShip_2);
spaceShipBaseVec.push_back(spaceShip_3);
spaceShipBaseVec.push_back(spaceShip_4);
return spaceShipBaseVec;
}
TAsteroidBaseVec CreateAsteroidBaseVec()
{
std::shared_ptr<CAsteroidBase> asteroid_1 = std::make_shared<CExplodingAsteroid>("exploding asteroid");
std::shared_ptr<CAsteroidBase> asteroid_2 = std::make_shared<CStonyAsteroid>("stony asteroid");
TAsteroidBaseVec asteroidBaseVec;
asteroidBaseVec.push_back(asteroid_1);
asteroidBaseVec.push_back(asteroid_2);
return asteroidBaseVec;
}
int main()
{
TSpaceShipBaseVec spaceShipBaseVec = CreateShipVec();
TAsteroidBaseVec asteroidBaseVec = CreateAsteroidBaseVec();
for(std::shared_ptr<CSpaceShipBase> shipBasePtr : spaceShipBaseVec)
{
for(std::shared_ptr<CAsteroidBase> asteroidBasePtr : asteroidBaseVec)
{
shipBasePtr->ShipCollidesWith(asteroidBasePtr);
}
}
return 0;
}