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 Barn olin{jaysPlace}; // Copy the barn olin.feed(); // Feed all the cows in the new barn Barn hoch{42}; // Create a Barn with a capacity of 42 hoch.addCow("Lulu", 5); // Add a cow to our third barn jaysPlace = hoch; // Overwrite the first barn with a copy of the third jaysPlace.feed(); // Feed all the cows in the first 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); Barn(const Barn& other); // Barn& operator=(const Barn& rhs); // Assignment operator (commented out) Barn& operator=(Barn rhs); // Assignment via copy-and-swap idiom void swap(Barn& other); void feed(); void addCow(std::string cowName, size_t numSpots); void addCow(const Cow& cow); ~Barn(); 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! } /* How to write the copy constructor directly... (commented out) Barn::Barn(const Barn& other) : cowCapacity_{other.cowCapacity_}, cowCount_{other.cowCount_}, residentCows_{new Cow*[cowCapacity_]} { for (size_t i = 0; i < cowCount_; ++i) { Cow* otherCowPtr = other.residentCows_[i]; residentCows_[i] = new Cow{*otherCowPtr}; } } */ /* How to write the copy constructor elegantly by reusing existing code... */ Barn::Barn(const Barn& other) : Barn{other.cowCapacity_} // Delegating constructor { for (size_t i = 0; i < other.cowCount_; ++i) { Cow* otherCowPtr = other.residentCows_[i]; addCow(*otherCowPtr); } } /* How to write the assignment operator directly... (commented out) Barn& Barn::operator=(const Barn& rhs) { // Check for self-assignment, and do nothing if so if (this == &rhs) { return *this; } // Delete the old array of Cow pointers for (size_t i = 0; i < cowCount_; ++i) { delete residentCows_[i]; } delete[] residentCows_; // Copy the new data cowCapacity_ = rhs.cowCapacity_; cowCount_ = rhs.cowCount_; residentCows_ = new Cow*[cowCapacity_]; for (size_t i = 0; i < cowCount_; ++i) { Cow* otherCowPtr = rhs.residentCows_[i]; residentCows_[i] = new Cow{*otherCowPtr}; } // Return a reference to the current object return *this; } */ /* How to write the assignment operator elegantly by reusing existing code via the copy-and-swap idiom... */ Barn& Barn::operator=(Barn other) { // Notice that we've passed a Barn, not a const Barn&, to this function. // This means, when we say barn1 = barn2, other will be a *copy* of barn2. // So, we've duplicated all the heap-allocated data in barn2 into other. // Then, we swap the data in *this with the data in other. swap(other); // *this now has all the data copied from barn2, and other has all the data // from barn1. So, when other goes out of scope, it will delete the // original data from barn1, and we'll be left with a copy of barn2's data // in *this. return *this; } 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::swap(Barn& other) { // Swap the data members of the current object with the data members of // the other object using std::swap; swap(cowCapacity_, other.cowCapacity_); swap(cowCount_, other.cowCount_); swap(residentCows_, other.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