online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include "barn.hpp" void addDaisy(Barn& barn) { Cow daisy{"Daisy", 7}; barn.addCow(daisy); } int main() { Barn jaysPlace; // Create a Barn with the default capacity jaysPlace.addCow("Bessie", 3); jaysPlace.addCow("Mable", 4); jaysPlace.feed(); // Feed all the cows in the barn addDaisy(jaysPlace); // Add Daisy to the barn jaysPlace.feed(); // Feed all the cows in the barn again 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 <cstddef> #include <string> #include "cow.hpp" class Barn { public: Barn(); Barn(size_t numCows); void feed(); void addCow(std::string cowName, size_t numSpots); void addCow(const Cow& cow); ~Barn(); // Disable copying and assignment Barn(const Barn& other) = delete; Barn& operator=(const Barn& rhs) = delete; private: static constexpr size_t DEFAULT_SIZE = 640; size_t cowCapacity_; // The number of Cows the Barn can hold size_t cowCount_; // The number of Cows in the Barn Cow** residentCows_; // Points to an array of Cow*s on the heap }; #endif
#include "barn.hpp" #include "cow.hpp" #include <iostream> #include <utility> #include <string> #include <stdexcept> Barn::Barn() : Barn{DEFAULT_SIZE} { // ^---- This is a delegating constructor. It calls the // parameterized constructor with the default size. // We can put this here instead of a member initialization // list to avoid duplicating code. // Nothing (else) to do here! } Barn::Barn(size_t numCows) : cowCapacity_{numCows}, cowCount_{0}, residentCows_{new Cow*[cowCapacity_]} { // Nothing (else) to do here! } Barn::~Barn() { // Destroy and deallocate all the (heap-allocated) Cow objects in the Barn for (size_t i = 0; i < cowCount_; ++i) { delete residentCows_[i]; } // Destroy and deallocate the (heap-allocated) array of Cow pointers delete[] residentCows_; } void Barn::addCow(std::string cowName, size_t numSpots) { if (cowCount_ >= cowCapacity_) { throw std::length_error("Barn is full!"); } residentCows_[cowCount_] = new Cow{cowName, numSpots}; ++cowCount_; } void Barn::addCow(const Cow& existingCow) { if (cowCount_ >= cowCapacity_) { throw std::length_error("Barn is full!"); } residentCows_[cowCount_] = new Cow{existingCow}; ++cowCount_; } void Barn::feed() { for (size_t i = 0; i < cowCount_; ++i) { residentCows_[i]->feed(); } }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue