#include "barn.hpp"
int main() {
Barn jaysPlace{1}; // Create a barn with one cow
jaysPlace.feed(); // Feed all the cows in the barn
return 0;
}
#ifndef COW_HPP_INCLUDED
#define COW_HPP_INCLUDED
#include <string>
#include <cstddef>
class Cow {
public:
Cow(); // Default constructor
Cow(std::string name, size_t numSpots); // Parameterized constructor
Cow(const Cow& other); // Copy constructor
~Cow(); // Destructor
void feed(); // Feed the cow
// Disable assignment
Cow& operator=(const Cow& rhs) = delete;
private:
std::string name_;
size_t numSpots_;
bool hasBeenFed_;
};
#endif
#include "cow.hpp"
#include <iostream>
using namespace std;
Cow::Cow() : name_{"(unnamed)"}, numSpots_{0}, hasBeenFed_{false} {
cout << "Created Cow " << name_ << " (default constructor)" << endl;
}
Cow::Cow(string name, size_t numSpots)
: name_{name}, numSpots_{numSpots}, hasBeenFed_{false} {
cout << "Created Cow " << name_ << " (parameterized constructor)" << endl;
}
Cow::Cow(const Cow& other)
: name_{"Copy of " + other.name_},
numSpots_{other.numSpots_},
hasBeenFed_{other.hasBeenFed_} {
cout << "Created Cow " << name_ << " (copy constructor)" << endl;
}
Cow::~Cow() {
cout << "Destroyed Cow " << name_ << endl;
}
void Cow::feed() {
hasBeenFed_ = true;
cout << "Fed Cow " << name_ << endl;
}
#ifndef BARN_HPP_INCLUDED
#define BARN_HPP_INCLUDED
#include "cow.hpp"
#include <cstddef>
#include <string>
class Barn {
public:
Barn();
Barn(size_t numCows);
void feed();
// Disable copying and assignment
Barn(const Barn& other) = delete;
Barn& operator=(const Barn& rhs) = delete;
private:
size_t cowCapacity_;
Cow residentCow_;
};
#endif
#include "barn.hpp"
#include "cow.hpp"
#include <iostream>
#include <utility>
#include <stdexcept>
Barn::Barn() : cowCapacity_{1}, residentCow_{"Bessie", 42} {
// Nothing (else) to do here!
}
Barn::Barn(size_t numCows) : cowCapacity_{numCows}, residentCow_{"Bessie", 42} {
if (cowCapacity_ != 1) {
throw std::invalid_argument("Barns can only hold one cow!");
}
}
void Barn::feed() {
residentCow_.feed();
}