Intro to C++ - Complete the following assignment using C++ Task 1: Create a base
ID: 3757275 • Letter: I
Question
Intro to C++ - Complete the following assignment using C++
Task 1: Create a base class using C++
Create a new project.
Design a class to abstractly model a stable (array) of horses (horse class).
Your stable needs an array to hold a number of horses (Horse class should have one attribute that represents its color). The number of horses should be set in a global constant (use 10 for the time being).
Your stable needs a variable to hold the number of horses currently being cared for in your stable.
Create a default constructor that initializes any values so the stable starts empty.
Your stable needs a function that will add a horse and another to remove a horse. The add function should receive a horse as an input parameter and the remove should return a horse. For the sake of simplicity, assume horses are fungible.
Your stable needs a function that returns the number of horses currently being housed in the stable.
Create the implementation code for the above functions as required.
Include in the submission a description of how you designed your add and remove methods. This description should not be code or pseudo code.
Explanation / Answer
//Horse.h
#include <string>
class Horse
{
private:
std::string color_;
public:
Horse();
~Horse();
//@! brief Returns the color of the horse
/*!
@return color
*/
std::string GetColor();
//@! brief Sets the color of the horse
/*!
@param[in] color: Color of the horse
*/
void SetColor(std::string color);
};
//Horse.cpp
#include "Horse.h"
Horse::Horse(){}
Horse::~Horse(){}
std::string Horse::GetColor()
{
return color_;
}
void Horse::SetColor(std::string color)
{
color_ = std::move(color);
}
//Stable.h
#pragma once
#include "Horse.h"
const size_t NO_OF_HORSES = 10;
class Stable
{
int numOfHorsesCured;
Horse horses[NO_OF_HORSES];
public:
//!@ brief Default Constructor
/*
Initialized the no of horses being cured to 0
*/
Stable()
{
numOfHorsesCured = 0;
}
//!@ breif Virtual function to add a horse
/*!
@param[in] horse: The horse to be added
*/
void AddHorse(const Horse& horse);
//!@ breif Removes the last horse from the list
/*!
@return
*/
Horse RemoveHorse();
//!@ breif Returns the number of horse currently being cured
/*!
@return numOfHorsesCured
*/
size_t GetNoOfHorsesCured();
};
//Stable.cpp
#include "Stable.h"
using ::Stable;
void Stable::AddHorse(const Horse & horse)
{
if (numOfHorsesCured < NO_OF_HORSES)
{
horses[numOfHorsesCured] = horse;
numOfHorsesCured++;
}
}
Horse Stable::RemoveHorse()
{
if (numOfHorsesCured == 0)
return Horse();
numOfHorsesCured--;
return horses[numOfHorsesCured];
}
size_t Stable::GetNoOfHorsesCured()
{
return numOfHorsesCured;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.