From 3f2153b4663aacdf88e5eb6a1af88127af0d586b Mon Sep 17 00:00:00 2001 From: hobwell <11163233+hobwell@users.noreply.github.com> Date: Wed, 4 Dec 2024 21:32:32 +0000 Subject: [PATCH 01/10] replit files updated --- .replit | 4 ++-- replit.nix | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 replit.nix diff --git a/.replit b/.replit index 24d1d17e..f234ec34 100644 --- a/.replit +++ b/.replit @@ -1,3 +1,3 @@ language = "cpp" -run = "clang++ -std=c++17 -Wno-deprecated -Wno-padded -Weverything -Wno-c++98-compat -Wno-missing-prototypes main.cpp && ./a.out" -hidden = ["ccls"] +run = "clang++ -std=c++17 -Weverything -Wno-padded -Wno-c++98-compat -Wno-missing-prototypes main.cpp && ./a.out" +hidden = ["ccls"] \ No newline at end of file diff --git a/replit.nix b/replit.nix new file mode 100644 index 00000000..9f3477b0 --- /dev/null +++ b/replit.nix @@ -0,0 +1,8 @@ +{ pkgs }: { + deps = [ + pkgs.clang_12 + pkgs.ccls + pkgs.gdb + pkgs.gnumake + ]; +} \ No newline at end of file From da1ddfc1be33ab7f9a8265e5d09cc1a9937394b3 Mon Sep 17 00:00:00 2001 From: hobwell <11163233+hobwell@users.noreply.github.com> Date: Tue, 10 Dec 2024 01:30:21 +0000 Subject: [PATCH 02/10] Completed Part 1 Copied 3 UDTs from Project 3 (along with two contained UDTs) Copied the main function from Project 3 removed non-working code moved the include for iostream Added two new UDTs comprised of the 5 copied UDTs Added calls to the two new UDTs methods --- .replit | 4 +- main.cpp | 751 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 751 insertions(+), 4 deletions(-) diff --git a/.replit b/.replit index f234ec34..4b37de6a 100644 --- a/.replit +++ b/.replit @@ -1,3 +1,3 @@ language = "cpp" -run = "clang++ -std=c++17 -Weverything -Wno-padded -Wno-c++98-compat -Wno-missing-prototypes main.cpp && ./a.out" -hidden = ["ccls"] \ No newline at end of file +run = "clang++ -std=c++17 -Weverything -Wno-padded -Wno-deprecated -Wno-c++98-compat -Wno-missing-prototypes main.cpp && ./a.out" +hidden = ["ccls"] diff --git a/main.cpp b/main.cpp index 47dd53a6..66a99696 100644 --- a/main.cpp +++ b/main.cpp @@ -70,29 +70,611 @@ write 3 UDTs below that EACH have: You will use those in Part 3 of this project. */ - +#include /* copied UDT 1: */ +struct Treadmill +{ + Treadmill (float weightAllowance = 150.0f); + ~Treadmill(); + + float currentSpeedKph = 0.0f; + float currentInclinationDegrees = 0.0f; + float totalDistanceSimulatedKm = 0.0f; + float sessionDistanceSimulatedKm = 0.0f; + float maximumWeightAllowanceKg; + + struct ValueDisplay + { + ValueDisplay (float val, std::string valName, std::string valUnit); + ~ValueDisplay(); + + std::string name = ""; + std::string unit = ""; + float value = 0.0f; + std::string colour = "green"; + std::string font = "Arial"; + int fontSize = 12; + + void updateValue (float newValue); + void changeFont (std::string newFont = "Arial", int newFontSize = 18); + void changeColour (std::string newColor = "green"); + int growFontSize (int numSteps = 1); + int shrinkFontSize (int numSteps = 1); + }; + + ValueDisplay speedDisplay{ 0.0f, "speed", "km/h" }; + ValueDisplay inclineDisplay{ 0.0f, "incline", "degrees" }; + + void rotateBelt (float speeedKph); + void incline (float inclinationDegrees); + void display (ValueDisplay displayValue); + float run (int numSteps, float strideLength = 0.00065f); +}; + +Treadmill::Treadmill (float weightAllowance) +{ + maximumWeightAllowanceKg = weightAllowance; + inclineDisplay.colour = "red"; + std::cout << "A Treadmill has been constructed!" << std::endl; +} + +Treadmill::~Treadmill() +{ + std::cout << "A treadmill has been destructed." << std::endl; +} + +void Treadmill::rotateBelt (float speedKph) +{ + std::cout << "Current Session distance: " << sessionDistanceSimulatedKm << " km!" << std::endl; + currentSpeedKph = speedKph; + speedDisplay.updateValue (currentSpeedKph); + std::cout << "Away we go (at " << currentSpeedKph << " km/h)!" << std::endl; +} + +void Treadmill::incline (float inclinationDegrees) +{ + currentInclinationDegrees = inclinationDegrees; + inclineDisplay.updateValue (currentInclinationDegrees); + std::cout << "Prepare to climb!" << std::endl; +} + +void Treadmill::display (ValueDisplay displayValue) +{ + std::cout << displayValue.name << ": " << displayValue.value << " " << displayValue.unit << std::endl; +} + +float Treadmill::run (int numSteps, float strideLength) +{ + for (int i = 0; i < numSteps; ++i) + { + if (i % 2 == 0) + std::cout << "Huff"<< std::endl; + else + std::cout << "Puff" << std::endl; + sessionDistanceSimulatedKm += strideLength; + } + + return sessionDistanceSimulatedKm; +} + + +Treadmill::ValueDisplay::ValueDisplay (float val, std::string valName, std::string valUnit) +{ + value = val; + name = valName; + unit = valUnit; + std::cout << "A ValueDisplay has been constructed!" << std::endl; +} + +Treadmill::ValueDisplay::~ValueDisplay() +{ + std::cout << "A ValueDisplay has been destructed!" << std::endl; +} + +void Treadmill::ValueDisplay::updateValue (float newValue) +{ + std::cout << "Changing " << name << " from " << value << " to " << newValue << " " << unit << std::endl; + value = newValue; +} + +void Treadmill::ValueDisplay::changeFont (std::string newFont, int newFontSize) +{ + std::cout << "Changing font from " << font << " " << fontSize << "pt to " << newFont << " " << newFontSize << "pt" << std::endl; + font = newFont; + fontSize = newFontSize; +} + +void Treadmill::ValueDisplay::changeColour (std::string newColor) +{ + colour = newColor; +} + +int Treadmill::ValueDisplay::growFontSize (int numSteps) +{ + if (numSteps > 0) + { + std::cout << "Growing font size "; + + for (int i = 0; i < numSteps; ++i) + { + fontSize += 1; + std::cout << "."; + } + + std::cout << std::endl; + } + else + { + std::cout << "numSteps must be greater than 0 for growFontSize to work." << std::endl; + } + + return fontSize; +} + +int Treadmill::ValueDisplay::shrinkFontSize (int numSteps) +{ + if (numSteps > 0) + { + std::cout << "Shrinking font size "; + int i = 0; + + while (i < numSteps && fontSize > 0) + { + fontSize -= 1; + std::cout << "."; + + if (fontSize < 1) + { + std::cout << std::endl << "Font size can't shrink any further!"; + } + + i += 1; + } + + std::cout << std::endl; + } + else + { + std::cout << "numSteps must be greater than 0 for shrinkFontSize to work." << std::endl; + } + + return fontSize; +} + /* copied UDT 2: */ +struct Cat +{ + Cat (std::string pattern, std::string colour); + ~Cat(); + + std::string furPattern = "tabby"; + std::string furColour = "orange"; + std::string eyeColour = "green"; + char sex = 'F'; + int age = 3; + + bool hunt (std::string creature); + void eat (float amountOfFoodKg); + void purr (float volumeDb); + int unrollToiletPaper (int numSwipes = 4, int squaresRemaining = 400); +}; + +Cat::Cat (std::string pattern, std::string colour) +{ + furPattern = pattern; + furColour = colour; + std::cout << "A Cat has been constructed!" << std::endl; +} + +Cat::~Cat() +{ + std::cout << "A Cat has been destructed." << std::endl; +} + +bool Cat::hunt (std::string creature) +{ + std::cout << "The " << eyeColour << "-eyed cat is hunting a " << creature << "!" << std::endl; + + if (creature == "mouse") + { + std::cout << "The cat pounces and catches a mouse!" << std::endl; + return true; + } + + if (creature == "bird") + { + std::cout << "The cat leaps into the air, snagging a bird with its paw." << std::endl; + return true; + } + + std::cout << "The cat chases the " << creature << ", but is unable to catch it." << std::endl; + + return false; +} + +void Cat::eat (float amountOfFoodKg) +{ + if (amountOfFoodKg > 0.0f) + std::cout << "nom nom nom" << std::endl; + else + std::cout << "meow" << std::endl; +} + +void Cat::purr (float volumeDb) +{ + if (volumeDb <= 0.0f) + { + std::cout << std::endl; + } + else if (volumeDb <= 50.0f) + { + std::cout << "purrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr" << std::endl; + } + else + { + std::cout << "PURRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR" << std::endl; + } +} + +int Cat::unrollToiletPaper (int numSwipes, int squaresRemaining) +{ + if (numSwipes > 0) + { + while (numSwipes > 0 && squaresRemaining > 0) + { + std::cout << "*bat* "; + numSwipes -= 1; + squaresRemaining -= 10; + } + + std::cout << std::endl; + } + else + { + std::cout << "Luckily, cats haven't figured out how to take negatve swipes yet." << std::endl; + } + + return squaresRemaining; +} + /* copied UDT 3: */ +struct Fruit +{ + Fruit(); + ~Fruit(); + + float endospermLevel = 0.05f; + float hydrationLevel = 0.05f; + float epicarpThicknessCm = 0.05f; + float mesocarpThicknessCm = 0.05f; + + struct Seed + { + Seed(int germinationDays); + ~Seed(); + + float weightGrams = 0.05f; + float coatIntegrity = 1.0f; + int daysToGerminate = 15; + float storedEnergy = 0.0f; + int daysDormant = 0; + int totalLeaves = 0; + + bool growRoot (bool germinated = false); + bool growStem (bool germinated = false); + int growLeaves (bool germinated = false, int numLeaves = 2); + bool germinate (int days); + }; + + Seed seed{15}; + + void protectSeed (float increment); + Seed disperseSeed (float distanceKm); + float feedSeed (float energy); + float decay (int days); +}; + +Fruit::Fruit() +{ + std::cout << "A Fruit has been constructed!" << std::endl; +} + +Fruit::~Fruit() +{ + std::cout << "A Fruit has been destructed." << std::endl; +} + +void Fruit::protectSeed (float increment) +{ + std::cout << "Hydration level: " << hydrationLevel << std::endl; + epicarpThicknessCm += increment; + mesocarpThicknessCm += increment / 2.0f; + seed.weightGrams += increment / 10.0f; +} + +Fruit::Seed Fruit::disperseSeed (float distanceKm) +{ + seed.daysDormant = 0; + seed.coatIntegrity -= distanceKm / 100.0f; + + return seed; +} + +float Fruit::feedSeed (float energy) +{ + seed.storedEnergy += energy; + + return seed.storedEnergy; +} + +float Fruit::decay (int days) +{ + float integrity = (endospermLevel + hydrationLevel + epicarpThicknessCm + mesocarpThicknessCm) / 4.0f; + + for (int i = 0; i < days; ++i) + { + endospermLevel *= 0.95f; + hydrationLevel *= 0.95f; + epicarpThicknessCm *= 0.95f; + mesocarpThicknessCm *= 0.95f; + feedSeed(0.005f); + + if (integrity < 0.5f) + { + std::cout << "Fruit has decayed." << std::endl; + return integrity; + } + } + + return integrity; +} + + +Fruit::Seed::Seed(int germinationDays) +{ + daysToGerminate = germinationDays; + std::cout << "A Seed has been constructed!" << std::endl; +} + +Fruit::Seed::~Seed() +{ + std::cout << "A Seed has been destructed." << std::endl; +} + +bool Fruit::Seed::growRoot (bool germinated) +{ + if (germinated) + { + std::cout << "Coat Integrity: " << coatIntegrity << std::endl; + if (coatIntegrity < 0.75f) + { + storedEnergy -= 0.04f; + coatIntegrity -= 0.05f; + std::cout << "Root grows!" << std::endl; + + return true; + } + + coatIntegrity -= 0.03f; + } + + return false; +} + +bool Fruit::Seed::growStem (bool germinated) +{ + if (germinated) + { + std::cout << "Coat Integrity: " << coatIntegrity << std::endl; + if (coatIntegrity < 0.5f) + { + storedEnergy -= 0.07f; + coatIntegrity -= 0.075f; + std::cout << "Stem grows!" << std::endl; + + return true; + } + + coatIntegrity -= 0.04f; + } + + return false; +} + +int Fruit::Seed::growLeaves (bool germinated, int numLeaves) +{ + if (germinated) + { + std::cout << "Coat Integrity: " << coatIntegrity << std::endl; + + if (coatIntegrity < 0.25f) + { + storedEnergy -= 0.04f; + coatIntegrity = 0; + totalLeaves += numLeaves; + std::cout << totalLeaves << " leaves are growing!" << std::endl; + } + + coatIntegrity -= 0.02f; + } + + return totalLeaves; +} + +bool Fruit::Seed::germinate (int days) +{ + bool germinated = false; + bool enoughEnergy = true; + + if (days > 0) + { + for (int i = 0; i < days; ++i) + { + storedEnergy -= 0.001f; + daysDormant += days; + std::cout << "Germinating..." << std::endl; + + if (storedEnergy < 0.75f) + { + std::cout << "Seed is no longer viable. Not enough energy." << std::endl; + i = days; + enoughEnergy = false; + } + } + + germinated = enoughEnergy && daysDormant > daysToGerminate; + } + else + { + std::cout << "Days must be positive." << std::endl; + } + + if (germinated) + std::cout << "Seed germinated!" << std::endl; + + return germinated; +} /* new UDT 4: with 2 member functions */ +struct Person +{ + Person(); + ~Person(); + + Cat dali{"tabby", "orange"}; + Cat geisha{"tabby", "grey"}; + Treadmill treadmill{300.0f}; + Treadmill::ValueDisplay temperatureDisplay{23.0f, "temperature", "C"}; + void feedCat (float amountOfFoodKg, Cat cat); + float excercise (int timeInMinutes, float strideLength = 0.65f); +}; + +Person::Person() +{ + geisha.eyeColour = "blue"; + std::cout << std::endl << "A person appears!" << std::endl; + std::cout << "Cats haven't ever been fed!" << std::endl; + feedCat (0.125f, dali); + feedCat (0.125f, geisha); + std::cout << "Cats are now fed! (not really)" << std::endl; +} + +Person::~Person() +{ + std::cout << std::endl << "As their last act, the cats have a bit of fun." << std::endl; + geisha.hunt("mouse"); + dali.unrollToiletPaper(11, 400); +} + +void Person::feedCat (float amountOfFoodKg, Cat cat) // not using pointers yet... +{ + cat.eat (amountOfFoodKg); +} + +float Person::excercise (int timeInMinutes, float strideLength) +{ + std::cout << std::endl << "Let's cool things off before we excersise!" << std::endl; + temperatureDisplay.updateValue (20.5f); + int numSteps = timeInMinutes * 27; + treadmill.rotateBelt (2.0f); + treadmill.display (treadmill.speedDisplay); + float distance = treadmill.run (numSteps, strideLength); + std::cout << "Great job! Time to excercise the cats!" << std::endl; + dali.hunt ("red dot"); + geisha.hunt ("colorful feather"); + + return distance; +} /* new UDT 5: with 2 member functions */ +struct TrashBin +{ + TrashBin(); + ~TrashBin(); + + Cat niceCat{"calico", "white-orange-black"}; + Cat alleyCat{"solid", "black"}; + Cat smellyCat{"bi-color", "white-black"}; + Treadmill treadmill{100.0f}; + Fruit avocado; + Fruit watermelon; + Fruit::Seed seed; + + bool attractCat (float smellIntensity, Cat cat); + float fester(); +}; + +TrashBin::TrashBin() : seed{15} +{ + std::cout << std::endl << "A TrashBin has popped into existence, complete with trash (and cats)!" << std::endl; + std::cout << "The smell is attacting some cats!" << std::endl; + + Cat randoCat{"tabby", "white"}; + attractCat (10.0f, randoCat); +} +TrashBin::~TrashBin() +{ + std::cout << std::endl << "The cats feast!" << std::endl; + niceCat.eat (0.125f); + alleyCat.eat (0.125f); + smellyCat.eat (0.125f); + std::cout << "A trash collector comes to reap a TrashBin." << std::endl; + std::cout << "The trash collector takes the cats with them." << std::endl; +} + +bool TrashBin::attractCat (float smellIntensity, Cat cat) +{ + std::cout << std::endl; + + if (smellIntensity >= 100.0f) + { + std::cout << "The smell has become too intense for the " << smellyCat.furColour << " " << smellyCat.furPattern << " cat!" << std::endl; + smellyCat = cat; + } + else if (smellIntensity >= 10.0f) + { + std::cout << "The smell has become too intense for the " << alleyCat.furColour << " " << alleyCat.furPattern << " cat!" << std::endl; + alleyCat = cat; + } + else if (smellIntensity >= 1.0f) + { + std::cout << "The smell has become too intense for the " << niceCat.furColour << " " << niceCat.furPattern << " cat!" << std::endl; + niceCat = cat; + } + else + { + std::cout << "The smell was not interesting enough to retain or repel any cats."; + return false; + } + + std::cout << "A " << cat.furColour << " " << cat.furPattern << " cat takes their place." << std::endl; + + return true; // a cat was attracted +} + +float TrashBin::fester() +{ + float smellIntensity = 0.0f; + smellIntensity += 10.0f / avocado.decay(1); + smellIntensity += 10.0f / watermelon.decay(1); + + return smellIntensity; +} /* MAKE SURE YOU ARE NOT ON THE MASTER BRANCH @@ -107,8 +689,173 @@ write 3 UDTs below that EACH have: Wait for my code review. */ -#include + int main() { + Treadmill::ValueDisplay maxWeightDisplay{ 200.0f, "max weight", "kg" }; + maxWeightDisplay.changeFont ("Times New Roman", 24); + maxWeightDisplay.changeColour ("Blue"); + std::cout << maxWeightDisplay.name << " (" << maxWeightDisplay.font << ", " << maxWeightDisplay.fontSize << "pt, " << maxWeightDisplay.colour << "): " << maxWeightDisplay.value << " " << maxWeightDisplay.unit << std::endl; + + std::cout << std::endl; + + maxWeightDisplay.changeColour ("Orange"); + maxWeightDisplay.updateValue (250.0f); + maxWeightDisplay.changeFont ("Helvetica", 18); + + std::cout << std::endl; + + auto newFontSize = maxWeightDisplay.shrinkFontSize (-1); + std::cout << "New font size: " << maxWeightDisplay.fontSize << std::endl; + newFontSize = maxWeightDisplay.shrinkFontSize (maxWeightDisplay.fontSize); + std::cout << "New font size: " << maxWeightDisplay.fontSize << std::endl; + newFontSize = maxWeightDisplay.growFontSize (12); + std::cout << "New font size: " << maxWeightDisplay.fontSize << std::endl; + newFontSize = maxWeightDisplay.growFontSize (-6); + std::cout << "New font size: " << maxWeightDisplay.fontSize << std::endl; + + std::cout << std::endl; + + Treadmill treadmill{ maxWeightDisplay.value }; + std::cout << "Treadmill: maximumWeightAllowanceKg: " << treadmill.maximumWeightAllowanceKg << std::endl; + std::cout << "Treadmill: currentSpeedKph: " << treadmill.currentSpeedKph << std::endl; + std::cout << "Treadmill: currentInclinationDegrees: " << treadmill.currentInclinationDegrees << std::endl; + std::cout << "Treadmill: totalDistanceSimulatedKm: " << treadmill.totalDistanceSimulatedKm << std::endl; + std::cout << "Treadmill: sessionDistanceSimulatedKm: " << treadmill.sessionDistanceSimulatedKm << std::endl; + + std::cout << std::endl; + + treadmill.rotateBelt (10.0f); + treadmill.incline (5.0f); + treadmill.display (treadmill.speedDisplay); + treadmill.display (treadmill.inclineDisplay); + treadmill.display (maxWeightDisplay); + + std::cout << std::endl; + + auto distanceRun = treadmill.run (4); + std::cout << "Distance run: " << distanceRun << std::endl << std::endl; + distanceRun = treadmill.run (10); + std::cout << "Distance run: " << distanceRun << std::endl; + + std::cout << std::endl; + + Cat tuxedoCat{ "tuxedo", "black" }; + std::cout << "Cat: furPattern: " << tuxedoCat.furPattern << std::endl; + std::cout << "Cat: furColour: " << tuxedoCat.furColour << std::endl; + std::cout << "Cat: eyeColour: " << tuxedoCat.eyeColour << std::endl; + std::cout << "Cat: age: " << tuxedoCat.age << std::endl; + std::cout << "Cat: sex: " << tuxedoCat.sex << std::endl; + + std::cout << std::endl; + + if (tuxedoCat.hunt ("mouse")) + std::cout << "The cat caught the mouse!" << std::endl; + + if (!tuxedoCat.hunt ("red dot")) + std::cout << "The cat is still chasing the red dot." << std::endl; + + tuxedoCat.eat (0.5f); + tuxedoCat.purr (50.0f); + + std::cout << std::endl; + + std::cout << "The cat has found a roll of toilet paper." << std::endl; + int squaresLeft = tuxedoCat.unrollToiletPaper (3); + std::cout << "Squares left: " << squaresLeft << std::endl; + squaresLeft = tuxedoCat.unrollToiletPaper (5, squaresLeft); + std::cout << "Squares left: " << squaresLeft << std::endl; + + std::cout << std::endl; + + Fruit fruit; + std::cout << "Fruit: endospermLevel: " << fruit.endospermLevel << std::endl; + std::cout << "Fruit: hydrationLevel: " << fruit.hydrationLevel << std::endl; + std::cout << "Fruit: epicarpThicknessCm: " << fruit.epicarpThicknessCm << std::endl; + std::cout << "Fruit: mesocarpThicknessCm: " << fruit.mesocarpThicknessCm << std::endl; + + std::cout << std::endl; + + fruit.hydrationLevel = 2.0f; + fruit.protectSeed (1.0f); + std::cout << std::endl << fruit.feedSeed (1.5f) << " total energy stored." << std::endl; + + std::cout << std::endl; + + float fruitIntegrity = fruit.decay (5); + std::cout << "Fruit integrity: " << fruitIntegrity << std::endl; + fruitIntegrity = fruit.decay (10); + std::cout << "Fruit integrity: " << fruitIntegrity << std::endl; + fruitIntegrity = fruit.decay (7); + std::cout << "Fruit integrity: " << fruitIntegrity << std::endl; + + std::cout << std::endl; + + Fruit::Seed newSeed = fruit.disperseSeed (10.0f); + std::cout << "Seed: weightGrams: " << newSeed.weightGrams << std::endl; + std::cout << "Seed: coatIntegrity: " << newSeed.coatIntegrity << std::endl; + std::cout << "Seed: daysToGerminate: " << newSeed.daysToGerminate << std::endl; + std::cout << "Seed: storedEnergy: " << newSeed.storedEnergy << std::endl; + std::cout << "Seed: daysDormant: " << newSeed.daysDormant << std::endl; + std::cout << "Seed: totalLeaves: " << newSeed.totalLeaves << std::endl; + + std::cout << std::endl; + + Fruit::Seed seed{20}; + seed.storedEnergy = 1.0f; + std::cout << "Seed: weightGrams: " << seed.weightGrams << std::endl; + std::cout << "Seed: coatIntegrity: " << seed.coatIntegrity << std::endl; + std::cout << "Seed: daysToGerminate: " << seed.daysToGerminate << std::endl; + std::cout << "Seed: storedEnergy: " << seed.storedEnergy << std::endl; + std::cout << "Seed: daysDormant: " << seed.daysDormant << std::endl; + std::cout << "Seed: totalLeaves: " << seed.totalLeaves << std::endl; + + std::cout << std::endl; + + auto germinated = seed.germinate (25.0f); + + std::cout << std::endl; + + while (germinated && !seed.growRoot (germinated)) + { + std::cout << "Seed root is not yet ready." << std::endl; + std::cout << "Time passes..." << std::endl; + } + + while (germinated && !seed.growStem (germinated)) + { + std::cout << "Seed stem is not yet ready." << std::endl; + std::cout << "Time passes..." << std::endl; + } + + while (germinated && seed.growLeaves (germinated, 10) < 10) + { + std::cout << "Seed leaves are not yet ready." << std::endl; + std::cout << "Time passes..." << std::endl; + } + + seed.growLeaves (germinated, 10); + + std::cout << std::endl; + + Person someone; + float distance = someone.excercise (10, 0.00124f); + std::cout << "someone ran " << distance << " km." << std::endl; + std::cout << "someone feeds the neighborhood stray." << std::endl; + someone.feedCat (0.125f, tuxedoCat); + + std::cout << std::endl; + + TrashBin trashBin; + + float smellIntensity = trashBin.fester(); + bool catAttracted = trashBin.attractCat (smellIntensity, tuxedoCat); + while (! catAttracted) + { + smellIntensity = trashBin.fester(); + catAttracted = trashBin.attractCat (smellIntensity, tuxedoCat); + } + std::cout << std::endl; + std::cout << "good to go!" << std::endl; } From c02ccf0cfaca03aae64ad9c539aae07a7242b2b1 Mon Sep 17 00:00:00 2001 From: hobwell <11163233+hobwell@users.noreply.github.com> Date: Tue, 10 Dec 2024 01:34:45 +0000 Subject: [PATCH 03/10] Fixed formatting --- main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.cpp b/main.cpp index 66a99696..f2996912 100644 --- a/main.cpp +++ b/main.cpp @@ -850,11 +850,13 @@ int main() float smellIntensity = trashBin.fester(); bool catAttracted = trashBin.attractCat (smellIntensity, tuxedoCat); + while (! catAttracted) { smellIntensity = trashBin.fester(); catAttracted = trashBin.attractCat (smellIntensity, tuxedoCat); } + std::cout << std::endl; std::cout << "good to go!" << std::endl; From a28d60c44c4672d820faeb8350ac73cd56e804bf Mon Sep 17 00:00:00 2001 From: matkatmusic Date: Mon, 9 Dec 2024 18:00:26 -0800 Subject: [PATCH 04/10] Added part 2 --- main.cpp | 125 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 60 deletions(-) diff --git a/main.cpp b/main.cpp index f2996912..4ffd7bb6 100644 --- a/main.cpp +++ b/main.cpp @@ -1,75 +1,80 @@ /* -Project 5: Part 1 / 4 - video Chapter 2 - Part 12 +Project 5: Part 2 / 4 + video: Chapter 3 Part 1 - Create a branch named Part1 +Create a branch named Part2 -Purpose: This project continues developing Project3. - you will learn how to take code from existing projects and migrate only what you need to new projects - you will learn how to write code that doesn't leak as well as how to refactor. - -NOTE: there are 2 sets of instructions: - One for if you completed project 3 - One for if you skipped project 3. - - Destructors - -=============================================================== - If you completed Project 3: + The 'this' keyword + + The purpose of this project part is to show you how accessing member variables of objects INSIDE member functions is very similar to accessing member variables of objects OUTSIDE of member functions, via the 'this' keyword and arrow (->) operator and via the '.' operator. + This project part will break the D.R.Y. rule, but that is fine for the purpose of this project part. - 1) Copy 3 of your user-defined types (the ones with constructors and for()/while() loops from Project 3) here - Choose the classes that contained nested classes. Include the nested classes when you copy them over. - - 2) move all of your implementations of all functions to OUTSIDE of the class. - - 3) add destructors - make the destructors do something like print out the name of the class. -=============================================================== -If you skipped Project 3: -write 3 UDTs below that EACH have: - 5 member variables - the member variable names and types should be relevant to the work the UDT will perform. - pick properties that can be represented with 'int float double bool char std::string' - 3 member functions with an arbitrary number of parameters - give some of those parameters default values. - constructors that initialize some of these member variables - the remaining member variables should be initialized in-class - for() or while() loops that modify member variables - 1) 2 of your 3 UDTs need to have a nested UDT. - this nested UDT should fulfill the same requirements as above: - 5 member variables - 3 member functions - constructors and loops. + Instructions: + 1) if you don't have any std::cout statements in main() that access member variables of your U.D.Ts + write some. + You can copy some from your Project3's main() if needed. + + 2) Do the following for EVERY std::cout statement in main() that uses the UDT member variables and functions: + a) write a member function that prints the same thing out, but uses the proper techniques inside the member functions to access the same member variables/functions. + b) be explicit with your use of 'this->' in those member functions so we see how you're accessing/calling those member variables and functions *inside* + c) call that member function AFTER your std::cout statement in main. + NOTE: if your member functions being called in main() use std::cout statements, you don't need to create duplicates of these functions. you only need to create member functions for the std::cout statements that exist in main(). - 2) Define your implementations of all functions OUTSIDE of the class. - NO IN-CLASS IMPLEMENTATION ALLOWED - 3) add destructors to all of your UDTs - make the destructors do something like print out the name of the class. -=============================================================== - - 4) add 2 new UDTs that use only the types you copied above as member variables. + 3) you should see 2 (almost) identical messages in the program output for each member function you add: + one for the std::cout line, and one for the member function's output + + 4) After you finish, click the [run] button. Clear up any errors or warnings as best you can. + */ - 5) add 2 member functions that use your member variables to each of these new UDTs +/* + example: + */ +#include +namespace Example +{ + //a User-Defined Type + struct MyFoo + { + MyFoo(); + ~MyFoo(); + + void printDetailedMemberInfo(); + + int returnValue() { return 3; } + float memberVariable = 3.14f; + }; - 6) Add constructors and destructors to these 2 new types that do stuff. - maybe print out the name of the class being destructed, or call a member function of one of the members. be creative - - 7) copy over your main() from the end of Project3 - get it to compile with the types you copied over. - remove any code in main() that uses types you didn't copy over. - - 8) Instantiate your 2 UDT's from step 4) in the main() function at the bottom. - call their member functions. - - 9) After you finish, click the [run] button. Clear up any errors or warnings as best you can. + MyFoo::MyFoo() { std::cout << "creating MyFoo" << std::endl; } + MyFoo::~MyFoo() { std::cout << "destroying MyFoo" << std::endl; } + + // 2a) the member function whose function body is almost identical to the std::cout statement in main. + //Remember to NAME FUNCTIONS WHAT THEY DO. + void MyFoo::printDetailedMemberInfo() //function name contains a verb!!! + { + // 2b) explicitly using 'this' inside this member function. + std::cout << "MyFoo returnValue(): " << this->returnValue() << " and MyFoo memberVariable: " << this->memberVariable << std::endl; + } + + int main() + { + //an instance of the User-Defined Type named mf + MyFoo mf; + + // 1) a std::cout statement that uses mf's member variables + std::cout << "mf returnValue(): " << mf.returnValue() << " and mf memberVariable: " << mf.memberVariable << std::endl; + + // 2c) calling mf's member function. the member function's body is almost identical to the cout statement above. + mf.printDetailedMemberInfo(); + return 0; + } +} - you can resolve any [-Wdeprecated] warnings by adding -Wno-deprecated to list of compiler arguments in the .replit file. all of the "-Wno" in that file are compiler arguments. - You can resolve any [-Wpadded] warnings by adding -Wno-padded to the list of compiler arguments in the .replit file. all of the "-Wno" in that file are compiler arguments. +/* Ignore the Atomic.h and LeakedObjectDetector.h files for now. You will use those in Part 3 of this project. +*/ - */ #include /* copied UDT 1: From 893011e905e3f1d4c3365cd2531a1511d634dd29 Mon Sep 17 00:00:00 2001 From: hobwell <11163233+hobwell@users.noreply.github.com> Date: Wed, 11 Dec 2024 01:01:43 +0000 Subject: [PATCH 05/10] Part 2 Added member methods to each UDT to mimic console output that appeared in the main method. --- main.cpp | 165 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 119 insertions(+), 46 deletions(-) diff --git a/main.cpp b/main.cpp index 4ffd7bb6..fb4cee5d 100644 --- a/main.cpp +++ b/main.cpp @@ -107,7 +107,10 @@ struct Treadmill void changeFont (std::string newFont = "Arial", int newFontSize = 18); void changeColour (std::string newColor = "green"); int growFontSize (int numSteps = 1); + void growFontSizeAndReportNewFontSize(int numSteps = 1); int shrinkFontSize (int numSteps = 1); + void shrinkFontSizeAndReportNewFontSize(int numSteps = 1); + void printFontInfo(); }; ValueDisplay speedDisplay{ 0.0f, "speed", "km/h" }; @@ -117,6 +120,7 @@ struct Treadmill void incline (float inclinationDegrees); void display (ValueDisplay displayValue); float run (int numSteps, float strideLength = 0.00065f); + void printMembers(); }; Treadmill::Treadmill (float weightAllowance) @@ -165,6 +169,15 @@ float Treadmill::run (int numSteps, float strideLength) return sessionDistanceSimulatedKm; } +void Treadmill::printMembers() +{ + std::cout << "Treadmill: maximumWeightAllowanceKg: " << this->maximumWeightAllowanceKg << std::endl; + std::cout << "Treadmill: currentSpeedKph: " << this->currentSpeedKph << std::endl; + std::cout << "Treadmill: currentInclinationDegrees: " << this->currentInclinationDegrees << std::endl; + std::cout << "Treadmill: totalDistanceSimulatedKm: " << this->totalDistanceSimulatedKm << std::endl; + std::cout << "Treadmill: sessionDistanceSimulatedKm: " << this->sessionDistanceSimulatedKm << std::endl; +} + Treadmill::ValueDisplay::ValueDisplay (float val, std::string valName, std::string valUnit) { @@ -249,6 +262,21 @@ int Treadmill::ValueDisplay::shrinkFontSize (int numSteps) return fontSize; } +void Treadmill::ValueDisplay::growFontSizeAndReportNewFontSize(int numSteps) +{ + std::cout << "New font size: " << this->growFontSize(numSteps) << std::endl; +} + +void Treadmill::ValueDisplay::shrinkFontSizeAndReportNewFontSize(int numSteps) +{ + std::cout << "New font size: " << this->shrinkFontSize(numSteps) << std::endl; +} + +void Treadmill::ValueDisplay::printFontInfo() +{ + std::cout << this->name << " (" << this->font << ", " << this->fontSize << "pt, " << this->colour << "): " << this->value << " " << this->unit << std::endl; +} + /* copied UDT 2: */ @@ -268,6 +296,7 @@ struct Cat void eat (float amountOfFoodKg); void purr (float volumeDb); int unrollToiletPaper (int numSwipes = 4, int squaresRemaining = 400); + void printMembers(); }; Cat::Cat (std::string pattern, std::string colour) @@ -348,6 +377,15 @@ int Cat::unrollToiletPaper (int numSwipes, int squaresRemaining) return squaresRemaining; } +void Cat::printMembers() +{ + std::cout << "Cat: furPattern: " << this->furPattern << std::endl; + std::cout << "Cat: furColour: " << this->furColour << std::endl; + std::cout << "Cat: eyeColour: " << this->eyeColour << std::endl; + std::cout << "Cat: age: " << this->age << std::endl; + std::cout << "Cat: sex: " << this->sex << std::endl; +} + /* copied UDT 3: */ @@ -377,6 +415,7 @@ struct Fruit bool growStem (bool germinated = false); int growLeaves (bool germinated = false, int numLeaves = 2); bool germinate (int days); + void printMembers(); }; Seed seed{15}; @@ -385,6 +424,8 @@ struct Fruit Seed disperseSeed (float distanceKm); float feedSeed (float energy); float decay (int days); + void feedSeedAndPrintSeedEnergy (float energy); + void printMembers(); }; Fruit::Fruit() @@ -420,6 +461,11 @@ float Fruit::feedSeed (float energy) return seed.storedEnergy; } +void Fruit::feedSeedAndPrintSeedEnergy (float energy) +{ + std::cout << std::endl << this->feedSeed (energy) << " total energy stored." << std::endl; +} + float Fruit::decay (int days) { float integrity = (endospermLevel + hydrationLevel + epicarpThicknessCm + mesocarpThicknessCm) / 4.0f; @@ -442,6 +488,12 @@ float Fruit::decay (int days) return integrity; } +void Fruit::printMembers() +{ + std::cout << "Fruit: endospermLevel: " << this->endospermLevel << std::endl; + std::cout << "Fruit: hydrationLevel: " << this->hydrationLevel << std::endl; + std::cout << "Fruit: epicarpThicknessCm: " << this->epicarpThicknessCm << std::endl; + std::cout << "Fruit: mesocarpThicknessCm: " << this->mesocarpThicknessCm << std::endl; } Fruit::Seed::Seed(int germinationDays) { @@ -548,6 +600,16 @@ bool Fruit::Seed::germinate (int days) return germinated; } +void Fruit::Seed::printMembers() +{ + std::cout << "Seed: weightGrams: " << this->weightGrams << std::endl; + std::cout << "Seed: coatIntegrity: " << this->coatIntegrity << std::endl; + std::cout << "Seed: daysToGerminate: " << this->daysToGerminate << std::endl; + std::cout << "Seed: storedEnergy: " << this->storedEnergy << std::endl; + std::cout << "Seed: daysDormant: " << this->daysDormant << std::endl; + std::cout << "Seed: totalLeaves: " << this->totalLeaves << std::endl; +} + /* new UDT 4: with 2 member functions @@ -700,8 +762,10 @@ int main() Treadmill::ValueDisplay maxWeightDisplay{ 200.0f, "max weight", "kg" }; maxWeightDisplay.changeFont ("Times New Roman", 24); maxWeightDisplay.changeColour ("Blue"); + std::cout << maxWeightDisplay.name << " (" << maxWeightDisplay.font << ", " << maxWeightDisplay.fontSize << "pt, " << maxWeightDisplay.colour << "): " << maxWeightDisplay.value << " " << maxWeightDisplay.unit << std::endl; - + maxWeightDisplay.printFontInfo(); + std::cout << std::endl; maxWeightDisplay.changeColour ("Orange"); @@ -709,25 +773,29 @@ int main() maxWeightDisplay.changeFont ("Helvetica", 18); std::cout << std::endl; - - auto newFontSize = maxWeightDisplay.shrinkFontSize (-1); - std::cout << "New font size: " << maxWeightDisplay.fontSize << std::endl; - newFontSize = maxWeightDisplay.shrinkFontSize (maxWeightDisplay.fontSize); - std::cout << "New font size: " << maxWeightDisplay.fontSize << std::endl; - newFontSize = maxWeightDisplay.growFontSize (12); - std::cout << "New font size: " << maxWeightDisplay.fontSize << std::endl; - newFontSize = maxWeightDisplay.growFontSize (-6); - std::cout << "New font size: " << maxWeightDisplay.fontSize << std::endl; + + std::cout << "New font size: " << maxWeightDisplay.shrinkFontSize (-1) << std::endl; + maxWeightDisplay.shrinkFontSizeAndReportNewFontSize (-1); + + std::cout << "New font size: " << maxWeightDisplay.shrinkFontSize (maxWeightDisplay.fontSize) << std::endl; + maxWeightDisplay.shrinkFontSizeAndReportNewFontSize (maxWeightDisplay.fontSize); + + std::cout << "New font size: " << maxWeightDisplay.growFontSize (12) << std::endl; + maxWeightDisplay.growFontSizeAndReportNewFontSize (12); + + std::cout << "New font size: " << maxWeightDisplay.growFontSize (-6) << std::endl; + maxWeightDisplay.growFontSizeAndReportNewFontSize (-6); std::cout << std::endl; Treadmill treadmill{ maxWeightDisplay.value }; - std::cout << "Treadmill: maximumWeightAllowanceKg: " << treadmill.maximumWeightAllowanceKg << std::endl; - std::cout << "Treadmill: currentSpeedKph: " << treadmill.currentSpeedKph << std::endl; - std::cout << "Treadmill: currentInclinationDegrees: " << treadmill.currentInclinationDegrees << std::endl; - std::cout << "Treadmill: totalDistanceSimulatedKm: " << treadmill.totalDistanceSimulatedKm << std::endl; - std::cout << "Treadmill: sessionDistanceSimulatedKm: " << treadmill.sessionDistanceSimulatedKm << std::endl; - + std::cout << "treadmill: maximumWeightAllowanceKg: " << treadmill.maximumWeightAllowanceKg << std::endl; + std::cout << "treadmill: currentSpeedKph: " << treadmill.currentSpeedKph << std::endl; + std::cout << "treadmill: currentInclinationDegrees: " << treadmill.currentInclinationDegrees << std::endl; + std::cout << "treadmill: totalDistanceSimulatedKm: " << treadmill.totalDistanceSimulatedKm << std::endl; + std::cout << "treadmill: sessionDistanceSimulatedKm: " << treadmill.sessionDistanceSimulatedKm << std::endl; + treadmill.printMembers(); + std::cout << std::endl; treadmill.rotateBelt (10.0f); @@ -746,12 +814,13 @@ int main() std::cout << std::endl; Cat tuxedoCat{ "tuxedo", "black" }; - std::cout << "Cat: furPattern: " << tuxedoCat.furPattern << std::endl; - std::cout << "Cat: furColour: " << tuxedoCat.furColour << std::endl; - std::cout << "Cat: eyeColour: " << tuxedoCat.eyeColour << std::endl; - std::cout << "Cat: age: " << tuxedoCat.age << std::endl; - std::cout << "Cat: sex: " << tuxedoCat.sex << std::endl; - + std::cout << "tuxedoCat: furPattern: " << tuxedoCat.furPattern << std::endl; + std::cout << "tuxedoCat: furColour: " << tuxedoCat.furColour << std::endl; + std::cout << "tuxedoCat: eyeColour: " << tuxedoCat.eyeColour << std::endl; + std::cout << "tuxedoCat: age: " << tuxedoCat.age << std::endl; + std::cout << "tuxedoCat: sex: " << tuxedoCat.sex << std::endl; + tuxedoCat.printMembers(); + std::cout << std::endl; if (tuxedoCat.hunt ("mouse")) @@ -774,47 +843,51 @@ int main() std::cout << std::endl; Fruit fruit; - std::cout << "Fruit: endospermLevel: " << fruit.endospermLevel << std::endl; - std::cout << "Fruit: hydrationLevel: " << fruit.hydrationLevel << std::endl; - std::cout << "Fruit: epicarpThicknessCm: " << fruit.epicarpThicknessCm << std::endl; - std::cout << "Fruit: mesocarpThicknessCm: " << fruit.mesocarpThicknessCm << std::endl; - + std::cout << "fruit: endospermLevel: " << fruit.endospermLevel << std::endl; + std::cout << "fruit: hydrationLevel: " << fruit.hydrationLevel << std::endl; + std::cout << "fruit: epicarpThicknessCm: " << fruit.epicarpThicknessCm << std::endl; + std::cout << "fruit: mesocarpThicknessCm: " << fruit.mesocarpThicknessCm << std::endl; + fruit.printMembers(); + std::cout << std::endl; fruit.hydrationLevel = 2.0f; fruit.protectSeed (1.0f); std::cout << std::endl << fruit.feedSeed (1.5f) << " total energy stored." << std::endl; - + fruit.feedSeedAndPrintSeedEnergy (1.5f); + std::cout << std::endl; float fruitIntegrity = fruit.decay (5); - std::cout << "Fruit integrity: " << fruitIntegrity << std::endl; + std::cout << "fruit integrity: " << fruitIntegrity << std::endl; fruitIntegrity = fruit.decay (10); - std::cout << "Fruit integrity: " << fruitIntegrity << std::endl; + std::cout << "fruit integrity: " << fruitIntegrity << std::endl; fruitIntegrity = fruit.decay (7); - std::cout << "Fruit integrity: " << fruitIntegrity << std::endl; + std::cout << "fruit integrity: " << fruitIntegrity << std::endl; std::cout << std::endl; Fruit::Seed newSeed = fruit.disperseSeed (10.0f); - std::cout << "Seed: weightGrams: " << newSeed.weightGrams << std::endl; - std::cout << "Seed: coatIntegrity: " << newSeed.coatIntegrity << std::endl; - std::cout << "Seed: daysToGerminate: " << newSeed.daysToGerminate << std::endl; - std::cout << "Seed: storedEnergy: " << newSeed.storedEnergy << std::endl; - std::cout << "Seed: daysDormant: " << newSeed.daysDormant << std::endl; - std::cout << "Seed: totalLeaves: " << newSeed.totalLeaves << std::endl; - + std::cout << "newSeed: weightGrams: " << newSeed.weightGrams << std::endl; + std::cout << "newSeed: coatIntegrity: " << newSeed.coatIntegrity << std::endl; + std::cout << "newSeed: daysToGerminate: " << newSeed.daysToGerminate << std::endl; + std::cout << "newSeed: storedEnergy: " << newSeed.storedEnergy << std::endl; + std::cout << "newSeed: daysDormant: " << newSeed.daysDormant << std::endl; + std::cout << "newSeed: totalLeaves: " << newSeed.totalLeaves << std::endl; + newSeed.printMembers(); + std::cout << std::endl; - + Fruit::Seed seed{20}; seed.storedEnergy = 1.0f; - std::cout << "Seed: weightGrams: " << seed.weightGrams << std::endl; - std::cout << "Seed: coatIntegrity: " << seed.coatIntegrity << std::endl; - std::cout << "Seed: daysToGerminate: " << seed.daysToGerminate << std::endl; - std::cout << "Seed: storedEnergy: " << seed.storedEnergy << std::endl; - std::cout << "Seed: daysDormant: " << seed.daysDormant << std::endl; - std::cout << "Seed: totalLeaves: " << seed.totalLeaves << std::endl; - + std::cout << "seed: weightGrams: " << seed.weightGrams << std::endl; + std::cout << "seed: coatIntegrity: " << seed.coatIntegrity << std::endl; + std::cout << "seed: daysToGerminate: " << seed.daysToGerminate << std::endl; + std::cout << "seed: storedEnergy: " << seed.storedEnergy << std::endl; + std::cout << "seed: daysDormant: " << seed.daysDormant << std::endl; + std::cout << "seed: totalLeaves: " << seed.totalLeaves << std::endl; + seed.printMembers(); + std::cout << std::endl; auto germinated = seed.germinate (25.0f); From d9dd3651bcbd07bc441cfb99e8e498becb1a15e4 Mon Sep 17 00:00:00 2001 From: hobwell <11163233+hobwell@users.noreply.github.com> Date: Wed, 11 Dec 2024 01:06:26 +0000 Subject: [PATCH 06/10] Fixed formatting --- main.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index fb4cee5d..e9ff71f9 100644 --- a/main.cpp +++ b/main.cpp @@ -107,9 +107,9 @@ struct Treadmill void changeFont (std::string newFont = "Arial", int newFontSize = 18); void changeColour (std::string newColor = "green"); int growFontSize (int numSteps = 1); - void growFontSizeAndReportNewFontSize(int numSteps = 1); + void growFontSizeAndReportNewFontSize (int numSteps = 1); int shrinkFontSize (int numSteps = 1); - void shrinkFontSizeAndReportNewFontSize(int numSteps = 1); + void shrinkFontSizeAndReportNewFontSize (int numSteps = 1); void printFontInfo(); }; @@ -262,14 +262,14 @@ int Treadmill::ValueDisplay::shrinkFontSize (int numSteps) return fontSize; } -void Treadmill::ValueDisplay::growFontSizeAndReportNewFontSize(int numSteps) +void Treadmill::ValueDisplay::growFontSizeAndReportNewFontSize (int numSteps) { - std::cout << "New font size: " << this->growFontSize(numSteps) << std::endl; + std::cout << "New font size: " << this->growFontSize (numSteps) << std::endl; } -void Treadmill::ValueDisplay::shrinkFontSizeAndReportNewFontSize(int numSteps) +void Treadmill::ValueDisplay::shrinkFontSizeAndReportNewFontSize (int numSteps) { - std::cout << "New font size: " << this->shrinkFontSize(numSteps) << std::endl; + std::cout << "New font size: " << this->shrinkFontSize (numSteps) << std::endl; } void Treadmill::ValueDisplay::printFontInfo() From 8e4895e56cb18fde108638454e50ee302aa2ebe7 Mon Sep 17 00:00:00 2001 From: matkatmusic Date: Wed, 11 Dec 2024 21:48:51 -0800 Subject: [PATCH 07/10] Added part 3 --- main.cpp | 128 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 69 insertions(+), 59 deletions(-) diff --git a/main.cpp b/main.cpp index e9ff71f9..61353a61 100644 --- a/main.cpp +++ b/main.cpp @@ -1,73 +1,83 @@ /* -Project 5: Part 2 / 4 - video: Chapter 3 Part 1 + Project 5: Part 3 / 4 + video: Chapter 3 Part 4: -Create a branch named Part2 +Create a branch named Part3 - The 'this' keyword + the 'new' keyword + + 1) add #include "LeakedObjectDetector.h" to main - The purpose of this project part is to show you how accessing member variables of objects INSIDE member functions is very similar to accessing member variables of objects OUTSIDE of member functions, via the 'this' keyword and arrow (->) operator and via the '.' operator. - This project part will break the D.R.Y. rule, but that is fine for the purpose of this project part. + 2) Add 'JUCE_LEAK_DETECTOR(OwnerClass)' at the end of your UDTs. - Instructions: - 1) if you don't have any std::cout statements in main() that access member variables of your U.D.Ts - write some. - You can copy some from your Project3's main() if needed. - - 2) Do the following for EVERY std::cout statement in main() that uses the UDT member variables and functions: - a) write a member function that prints the same thing out, but uses the proper techniques inside the member functions to access the same member variables/functions. - b) be explicit with your use of 'this->' in those member functions so we see how you're accessing/calling those member variables and functions *inside* - c) call that member function AFTER your std::cout statement in main. - NOTE: if your member functions being called in main() use std::cout statements, you don't need to create duplicates of these functions. you only need to create member functions for the std::cout statements that exist in main(). - - 3) you should see 2 (almost) identical messages in the program output for each member function you add: - one for the std::cout line, and one for the member function's output + 3) write the name of your class where it says "OwnerClass" - 4) After you finish, click the [run] button. Clear up any errors or warnings as best you can. + 4) write wrapper classes for each type similar to how it was shown in the video + + 5) update main() + replace your objects with your wrapper classes, which have your UDTs as pointer member variables. + + This means if you had something like the following in your main() previously: +*/ +#if false + Axe axe; + std::cout << "axe sharpness: " << axe.sharpness << "\n"; + #endif + /* + you would update that to use your wrappers: + */ +#if false +AxeWrapper axWrapper( new Axe() ); +std::cout << "axe sharpness: " << axWrapper.axPtr->sharpness << "\n"; +#endif +/* +notice that the object name has changed from 'axe' to 'axWrapper' +You don't have to do this, you can keep your current object name and just change its type to your Wrapper class + +6) If you have a class that has a nested class in it, and an instantiation of that nested class as a member variable, + - you do not need to write a Wrapper for that nested class + - you do not need to replace that nested instance with a wrapped instance. + If you want an explanation, message me in Slack + +7) If you were using any UDTs as function arguments like this: +*/ +#if false +void someMemberFunction(Axe axe); +#endif +/* + Pass those arguments by Reference now that you know what references are (Project 6 Part 2). +*/ +#if false +void someMemberFunction(Axe& axe); +#endif +/* +If you aren't modifying the passed-in object inside the function, pass by 'const reference'. +Marking a function parameter as 'const' means that you are promising that the parameter will not be modified. +Additionally, you can mark class member functions as 'const' +If you do this, you are promising that the member function will not modify any member variables. + +Mark every member function that is not modifying any member variables as 'const' +*/ +#if false +//a function where the argument is passed by const-ref +void someMemberFunction(const Axe& axe); + +//a member function that is marked const, meaning it will not modify any member variables of the 'Axe' class. +void Axe::aConstMemberFunction() const { } +#endif /* - example: + 8) After you finish, click the [run] button. Clear up any errors or warnings as best you can. + + see here for an example: https://repl.it/@matkatmusic/ch3p04example + + Clear any warnings about exit-time-destructors. + Suppress them by adding -Wno-exit-time-destructors to the .replit file with the other warning suppression flags */ -#include -namespace Example -{ - //a User-Defined Type - struct MyFoo - { - MyFoo(); - ~MyFoo(); - - void printDetailedMemberInfo(); - - int returnValue() { return 3; } - float memberVariable = 3.14f; - }; - MyFoo::MyFoo() { std::cout << "creating MyFoo" << std::endl; } - MyFoo::~MyFoo() { std::cout << "destroying MyFoo" << std::endl; } - - // 2a) the member function whose function body is almost identical to the std::cout statement in main. - //Remember to NAME FUNCTIONS WHAT THEY DO. - void MyFoo::printDetailedMemberInfo() //function name contains a verb!!! - { - // 2b) explicitly using 'this' inside this member function. - std::cout << "MyFoo returnValue(): " << this->returnValue() << " and MyFoo memberVariable: " << this->memberVariable << std::endl; - } - - int main() - { - //an instance of the User-Defined Type named mf - MyFoo mf; - - // 1) a std::cout statement that uses mf's member variables - std::cout << "mf returnValue(): " << mf.returnValue() << " and mf memberVariable: " << mf.memberVariable << std::endl; - - // 2c) calling mf's member function. the member function's body is almost identical to the cout statement above. - mf.printDetailedMemberInfo(); - return 0; - } -} + + /* From 93982efc27c7057648aaf4f9f92f6d61fd7322a9 Mon Sep 17 00:00:00 2001 From: hobwell <11163233+hobwell@users.noreply.github.com> Date: Fri, 13 Dec 2024 04:00:13 +0000 Subject: [PATCH 08/10] Part 3 Added leak detection to UDTs Created wrapper classes Updated pointers to references, where possible Updated function arguments that were UDTs to references added const to UDT arguments that aren't modified inside member methods added const to methods that do not modify member variables. replaced UDTs in main with wrapper equivalents (excluding nested UDTs) Updated .replit to exclude exit-time-destructors warnings --- .replit | 2 +- main.cpp | 234 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 156 insertions(+), 80 deletions(-) diff --git a/.replit b/.replit index 4b37de6a..abf18be5 100644 --- a/.replit +++ b/.replit @@ -1,3 +1,3 @@ language = "cpp" -run = "clang++ -std=c++17 -Weverything -Wno-padded -Wno-deprecated -Wno-c++98-compat -Wno-missing-prototypes main.cpp && ./a.out" +run = "clang++ -std=c++17 -Weverything -Wno-padded -Wno-deprecated -Wno-c++98-compat -Wno-missing-prototypes -Wno-exit-time-destructors main.cpp && ./a.out" hidden = ["ccls"] diff --git a/main.cpp b/main.cpp index 61353a61..1e2b9f02 100644 --- a/main.cpp +++ b/main.cpp @@ -86,6 +86,7 @@ void Axe::aConstMemberFunction() const { } */ #include +#include "LeakedObjectDetector.h" /* copied UDT 1: */ @@ -120,7 +121,9 @@ struct Treadmill void growFontSizeAndReportNewFontSize (int numSteps = 1); int shrinkFontSize (int numSteps = 1); void shrinkFontSizeAndReportNewFontSize (int numSteps = 1); - void printFontInfo(); + void printFontInfo() const; + + JUCE_LEAK_DETECTOR (ValueDisplay) }; ValueDisplay speedDisplay{ 0.0f, "speed", "km/h" }; @@ -128,9 +131,23 @@ struct Treadmill void rotateBelt (float speeedKph); void incline (float inclinationDegrees); - void display (ValueDisplay displayValue); + void display (const ValueDisplay& displayValue) const; float run (int numSteps, float strideLength = 0.00065f); - void printMembers(); + void printMembers() const; + + JUCE_LEAK_DETECTOR (Treadmill) +}; + +struct TreadmillWrapper +{ + TreadmillWrapper (Treadmill* t) : treadmill (t) {} + + ~TreadmillWrapper() + { + delete treadmill; + } + + Treadmill* treadmill = nullptr; }; Treadmill::Treadmill (float weightAllowance) @@ -160,7 +177,7 @@ void Treadmill::incline (float inclinationDegrees) std::cout << "Prepare to climb!" << std::endl; } -void Treadmill::display (ValueDisplay displayValue) +void Treadmill::display (const ValueDisplay& displayValue) const { std::cout << displayValue.name << ": " << displayValue.value << " " << displayValue.unit << std::endl; } @@ -179,7 +196,7 @@ float Treadmill::run (int numSteps, float strideLength) return sessionDistanceSimulatedKm; } -void Treadmill::printMembers() +void Treadmill::printMembers() const { std::cout << "Treadmill: maximumWeightAllowanceKg: " << this->maximumWeightAllowanceKg << std::endl; std::cout << "Treadmill: currentSpeedKph: " << this->currentSpeedKph << std::endl; @@ -282,7 +299,7 @@ void Treadmill::ValueDisplay::shrinkFontSizeAndReportNewFontSize (int numSteps) std::cout << "New font size: " << this->shrinkFontSize (numSteps) << std::endl; } -void Treadmill::ValueDisplay::printFontInfo() +void Treadmill::ValueDisplay::printFontInfo() const { std::cout << this->name << " (" << this->font << ", " << this->fontSize << "pt, " << this->colour << "): " << this->value << " " << this->unit << std::endl; } @@ -302,11 +319,25 @@ struct Cat char sex = 'F'; int age = 3; - bool hunt (std::string creature); - void eat (float amountOfFoodKg); - void purr (float volumeDb); - int unrollToiletPaper (int numSwipes = 4, int squaresRemaining = 400); - void printMembers(); + bool hunt (std::string creature) const; + void eat (float amountOfFoodKg) const; + void purr (float volumeDb) const; + int unrollToiletPaper (int numSwipes = 4, int squaresRemaining = 400) const; + void printMembers() const; + + JUCE_LEAK_DETECTOR (Cat) +}; + +struct CatWrapper +{ + CatWrapper (Cat* c) : cat (c) {} + + ~CatWrapper() + { + delete cat; + } + + Cat* cat = nullptr; }; Cat::Cat (std::string pattern, std::string colour) @@ -318,10 +349,10 @@ Cat::Cat (std::string pattern, std::string colour) Cat::~Cat() { - std::cout << "A Cat has been destructed." << std::endl; + std::cout << "A " << this->furColour << " " << this->furPattern << " Cat has been destructed." << std::endl; } -bool Cat::hunt (std::string creature) +bool Cat::hunt (std::string creature) const { std::cout << "The " << eyeColour << "-eyed cat is hunting a " << creature << "!" << std::endl; @@ -342,7 +373,7 @@ bool Cat::hunt (std::string creature) return false; } -void Cat::eat (float amountOfFoodKg) +void Cat::eat (float amountOfFoodKg) const { if (amountOfFoodKg > 0.0f) std::cout << "nom nom nom" << std::endl; @@ -350,7 +381,7 @@ void Cat::eat (float amountOfFoodKg) std::cout << "meow" << std::endl; } -void Cat::purr (float volumeDb) +void Cat::purr (float volumeDb) const { if (volumeDb <= 0.0f) { @@ -366,7 +397,7 @@ void Cat::purr (float volumeDb) } } -int Cat::unrollToiletPaper (int numSwipes, int squaresRemaining) +int Cat::unrollToiletPaper (int numSwipes, int squaresRemaining) const { if (numSwipes > 0) { @@ -387,7 +418,7 @@ int Cat::unrollToiletPaper (int numSwipes, int squaresRemaining) return squaresRemaining; } -void Cat::printMembers() +void Cat::printMembers() const { std::cout << "Cat: furPattern: " << this->furPattern << std::endl; std::cout << "Cat: furColour: " << this->furColour << std::endl; @@ -425,7 +456,9 @@ struct Fruit bool growStem (bool germinated = false); int growLeaves (bool germinated = false, int numLeaves = 2); bool germinate (int days); - void printMembers(); + void printMembers() const; + + JUCE_LEAK_DETECTOR (Seed) }; Seed seed{15}; @@ -435,7 +468,21 @@ struct Fruit float feedSeed (float energy); float decay (int days); void feedSeedAndPrintSeedEnergy (float energy); - void printMembers(); + void printMembers() const; + + JUCE_LEAK_DETECTOR (Fruit) +}; + +struct FruitWrapper +{ + FruitWrapper (Fruit* f) : fruit (f) {} + + ~FruitWrapper() + { + delete fruit; + } + + Fruit* fruit = nullptr; }; Fruit::Fruit() @@ -498,7 +545,7 @@ float Fruit::decay (int days) return integrity; } -void Fruit::printMembers() +void Fruit::printMembers() const { std::cout << "Fruit: endospermLevel: " << this->endospermLevel << std::endl; std::cout << "Fruit: hydrationLevel: " << this->hydrationLevel << std::endl; @@ -610,7 +657,7 @@ bool Fruit::Seed::germinate (int days) return germinated; } -void Fruit::Seed::printMembers() +void Fruit::Seed::printMembers() const { std::cout << "Seed: weightGrams: " << this->weightGrams << std::endl; std::cout << "Seed: coatIntegrity: " << this->coatIntegrity << std::endl; @@ -634,8 +681,22 @@ struct Person Treadmill treadmill{300.0f}; Treadmill::ValueDisplay temperatureDisplay{23.0f, "temperature", "C"}; - void feedCat (float amountOfFoodKg, Cat cat); + void feedCat (float amountOfFoodKg, const Cat& cat) const; float excercise (int timeInMinutes, float strideLength = 0.65f); + + JUCE_LEAK_DETECTOR (Person) +}; + +struct PersonWrapper +{ + PersonWrapper (Person* p) : person (p) {} + + ~PersonWrapper() + { + delete person; + } + + Person* person = nullptr; }; Person::Person() @@ -655,14 +716,14 @@ Person::~Person() dali.unrollToiletPaper(11, 400); } -void Person::feedCat (float amountOfFoodKg, Cat cat) // not using pointers yet... +void Person::feedCat (float amountOfFoodKg, const Cat& cat) const { cat.eat (amountOfFoodKg); } float Person::excercise (int timeInMinutes, float strideLength) { - std::cout << std::endl << "Let's cool things off before we excersise!" << std::endl; + std::cout << std::endl << "Let's cool things off before we excercise!" << std::endl; temperatureDisplay.updateValue (20.5f); int numSteps = timeInMinutes * 27; treadmill.rotateBelt (2.0f); @@ -680,7 +741,7 @@ float Person::excercise (int timeInMinutes, float strideLength) */ struct TrashBin { - TrashBin(); + TrashBin (Cat& nearbyCat); ~TrashBin(); Cat niceCat{"calico", "white-orange-black"}; @@ -691,17 +752,30 @@ struct TrashBin Fruit watermelon; Fruit::Seed seed; - bool attractCat (float smellIntensity, Cat cat); + bool attractCat (float smellIntensity, const Cat& cat); float fester(); + + JUCE_LEAK_DETECTOR (TrashBin) +}; + +struct TrashBinWrapper +{ + TrashBinWrapper (TrashBin* tb) : trashBin (tb) {} + + ~TrashBinWrapper() + { + delete trashBin; + } + + TrashBin* trashBin = nullptr; }; -TrashBin::TrashBin() : seed{15} +TrashBin::TrashBin(Cat& nearbyCat) : seed{15} { std::cout << std::endl << "A TrashBin has popped into existence, complete with trash (and cats)!" << std::endl; std::cout << "The smell is attacting some cats!" << std::endl; - Cat randoCat{"tabby", "white"}; - attractCat (10.0f, randoCat); + attractCat (10.0f, nearbyCat); } TrashBin::~TrashBin() @@ -714,7 +788,7 @@ TrashBin::~TrashBin() std::cout << "The trash collector takes the cats with them." << std::endl; } -bool TrashBin::attractCat (float smellIntensity, Cat cat) +bool TrashBin::attractCat (float smellIntensity, const Cat& cat) { std::cout << std::endl; @@ -798,86 +872,86 @@ int main() std::cout << std::endl; - Treadmill treadmill{ maxWeightDisplay.value }; - std::cout << "treadmill: maximumWeightAllowanceKg: " << treadmill.maximumWeightAllowanceKg << std::endl; - std::cout << "treadmill: currentSpeedKph: " << treadmill.currentSpeedKph << std::endl; - std::cout << "treadmill: currentInclinationDegrees: " << treadmill.currentInclinationDegrees << std::endl; - std::cout << "treadmill: totalDistanceSimulatedKm: " << treadmill.totalDistanceSimulatedKm << std::endl; - std::cout << "treadmill: sessionDistanceSimulatedKm: " << treadmill.sessionDistanceSimulatedKm << std::endl; - treadmill.printMembers(); + TreadmillWrapper treadmillWrapper (new Treadmill{ maxWeightDisplay.value }); + std::cout << "treadmill: maximumWeightAllowanceKg: " << treadmillWrapper.treadmill->maximumWeightAllowanceKg << std::endl; + std::cout << "treadmill: currentSpeedKph: " << treadmillWrapper.treadmill->currentSpeedKph << std::endl; + std::cout << "treadmill: currentInclinationDegrees: " << treadmillWrapper.treadmill->currentInclinationDegrees << std::endl; + std::cout << "treadmill: totalDistanceSimulatedKm: " << treadmillWrapper.treadmill->totalDistanceSimulatedKm << std::endl; + std::cout << "treadmill: sessionDistanceSimulatedKm: " << treadmillWrapper.treadmill->sessionDistanceSimulatedKm << std::endl; + treadmillWrapper.treadmill->printMembers(); std::cout << std::endl; - treadmill.rotateBelt (10.0f); - treadmill.incline (5.0f); - treadmill.display (treadmill.speedDisplay); - treadmill.display (treadmill.inclineDisplay); - treadmill.display (maxWeightDisplay); + treadmillWrapper.treadmill->rotateBelt (10.0f); + treadmillWrapper.treadmill->incline (5.0f); + treadmillWrapper.treadmill->display (treadmillWrapper.treadmill->speedDisplay); + treadmillWrapper.treadmill->display (treadmillWrapper.treadmill->inclineDisplay); + treadmillWrapper.treadmill->display (maxWeightDisplay); std::cout << std::endl; - auto distanceRun = treadmill.run (4); + auto distanceRun = treadmillWrapper.treadmill->run (4); std::cout << "Distance run: " << distanceRun << std::endl << std::endl; - distanceRun = treadmill.run (10); + distanceRun = treadmillWrapper.treadmill->run (10); std::cout << "Distance run: " << distanceRun << std::endl; std::cout << std::endl; - Cat tuxedoCat{ "tuxedo", "black" }; - std::cout << "tuxedoCat: furPattern: " << tuxedoCat.furPattern << std::endl; - std::cout << "tuxedoCat: furColour: " << tuxedoCat.furColour << std::endl; - std::cout << "tuxedoCat: eyeColour: " << tuxedoCat.eyeColour << std::endl; - std::cout << "tuxedoCat: age: " << tuxedoCat.age << std::endl; - std::cout << "tuxedoCat: sex: " << tuxedoCat.sex << std::endl; - tuxedoCat.printMembers(); + CatWrapper catWrapper (new Cat{ "tuxedo", "black" }); + std::cout << "tuxedoCat: furPattern: " << catWrapper.cat->furPattern << std::endl; + std::cout << "tuxedoCat: furColour: " << catWrapper.cat->furColour << std::endl; + std::cout << "tuxedoCat: eyeColour: " << catWrapper.cat->eyeColour << std::endl; + std::cout << "tuxedoCat: age: " << catWrapper.cat->age << std::endl; + std::cout << "tuxedoCat: sex: " << catWrapper.cat->sex << std::endl; + catWrapper.cat->printMembers(); std::cout << std::endl; - if (tuxedoCat.hunt ("mouse")) + if (catWrapper.cat->hunt ("mouse")) std::cout << "The cat caught the mouse!" << std::endl; - if (!tuxedoCat.hunt ("red dot")) + if (!catWrapper.cat->hunt ("red dot")) std::cout << "The cat is still chasing the red dot." << std::endl; - tuxedoCat.eat (0.5f); - tuxedoCat.purr (50.0f); + catWrapper.cat->eat (0.5f); + catWrapper.cat->purr (50.0f); std::cout << std::endl; std::cout << "The cat has found a roll of toilet paper." << std::endl; - int squaresLeft = tuxedoCat.unrollToiletPaper (3); + int squaresLeft = catWrapper.cat->unrollToiletPaper (3); std::cout << "Squares left: " << squaresLeft << std::endl; - squaresLeft = tuxedoCat.unrollToiletPaper (5, squaresLeft); + squaresLeft = catWrapper.cat->unrollToiletPaper (5, squaresLeft); std::cout << "Squares left: " << squaresLeft << std::endl; std::cout << std::endl; - Fruit fruit; - std::cout << "fruit: endospermLevel: " << fruit.endospermLevel << std::endl; - std::cout << "fruit: hydrationLevel: " << fruit.hydrationLevel << std::endl; - std::cout << "fruit: epicarpThicknessCm: " << fruit.epicarpThicknessCm << std::endl; - std::cout << "fruit: mesocarpThicknessCm: " << fruit.mesocarpThicknessCm << std::endl; - fruit.printMembers(); + FruitWrapper fruitWrapper(new Fruit); + std::cout << "fruit: endospermLevel: " << fruitWrapper.fruit->endospermLevel << std::endl; + std::cout << "fruit: hydrationLevel: " << fruitWrapper.fruit->hydrationLevel << std::endl; + std::cout << "fruit: epicarpThicknessCm: " << fruitWrapper.fruit->epicarpThicknessCm << std::endl; + std::cout << "fruit: mesocarpThicknessCm: " << fruitWrapper.fruit->mesocarpThicknessCm << std::endl; + fruitWrapper.fruit->printMembers(); std::cout << std::endl; - fruit.hydrationLevel = 2.0f; - fruit.protectSeed (1.0f); - std::cout << std::endl << fruit.feedSeed (1.5f) << " total energy stored." << std::endl; - fruit.feedSeedAndPrintSeedEnergy (1.5f); + fruitWrapper.fruit->hydrationLevel = 2.0f; + fruitWrapper.fruit->protectSeed (1.0f); + std::cout << std::endl << fruitWrapper.fruit->feedSeed (1.5f) << " total energy stored." << std::endl; + fruitWrapper.fruit->feedSeedAndPrintSeedEnergy (1.5f); std::cout << std::endl; - float fruitIntegrity = fruit.decay (5); + float fruitIntegrity = fruitWrapper.fruit->decay (5); std::cout << "fruit integrity: " << fruitIntegrity << std::endl; - fruitIntegrity = fruit.decay (10); + fruitIntegrity = fruitWrapper.fruit->decay (10); std::cout << "fruit integrity: " << fruitIntegrity << std::endl; - fruitIntegrity = fruit.decay (7); + fruitIntegrity = fruitWrapper.fruit->decay (7); std::cout << "fruit integrity: " << fruitIntegrity << std::endl; std::cout << std::endl; - Fruit::Seed newSeed = fruit.disperseSeed (10.0f); + Fruit::Seed newSeed = fruitWrapper.fruit->disperseSeed (10.0f); std::cout << "newSeed: weightGrams: " << newSeed.weightGrams << std::endl; std::cout << "newSeed: coatIntegrity: " << newSeed.coatIntegrity << std::endl; std::cout << "newSeed: daysToGerminate: " << newSeed.daysToGerminate << std::endl; @@ -926,23 +1000,25 @@ int main() std::cout << std::endl; - Person someone; - float distance = someone.excercise (10, 0.00124f); + PersonWrapper someoneWrapper (new Person); + float distance = someoneWrapper.person->excercise (10, 0.00124f); std::cout << "someone ran " << distance << " km." << std::endl; std::cout << "someone feeds the neighborhood stray." << std::endl; - someone.feedCat (0.125f, tuxedoCat); + someoneWrapper.person->feedCat (0.125f, *(catWrapper.cat)); std::cout << std::endl; - TrashBin trashBin; + CatWrapper randoCatWrapper (new Cat{"tabby", "white"}); + + TrashBinWrapper trashBinWrapper (new TrashBin(*(randoCatWrapper.cat))); - float smellIntensity = trashBin.fester(); - bool catAttracted = trashBin.attractCat (smellIntensity, tuxedoCat); + float smellIntensity = trashBinWrapper.trashBin->fester(); + bool catAttracted = trashBinWrapper.trashBin->attractCat (smellIntensity, *(catWrapper.cat)); while (! catAttracted) { - smellIntensity = trashBin.fester(); - catAttracted = trashBin.attractCat (smellIntensity, tuxedoCat); + smellIntensity = trashBinWrapper.trashBin->fester(); + catAttracted = trashBinWrapper.trashBin->attractCat (smellIntensity, *(catWrapper.cat)); } std::cout << std::endl; From dca095a042d25f1d76ca52022765436043d6d9e7 Mon Sep 17 00:00:00 2001 From: hobwell <11163233+hobwell@users.noreply.github.com> Date: Fri, 13 Dec 2024 04:11:24 +0000 Subject: [PATCH 09/10] Fixed formatting --- main.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/main.cpp b/main.cpp index 1e2b9f02..8f206f58 100644 --- a/main.cpp +++ b/main.cpp @@ -140,14 +140,14 @@ struct Treadmill struct TreadmillWrapper { - TreadmillWrapper (Treadmill* t) : treadmill (t) {} + TreadmillWrapper (Treadmill* t) : treadmill (t) {} ~TreadmillWrapper() - { + { delete treadmill; - } + } - Treadmill* treadmill = nullptr; + Treadmill* treadmill = nullptr; }; Treadmill::Treadmill (float weightAllowance) @@ -330,14 +330,14 @@ struct Cat struct CatWrapper { - CatWrapper (Cat* c) : cat (c) {} + CatWrapper (Cat* c) : cat (c) {} ~CatWrapper() - { + { delete cat; - } + } - Cat* cat = nullptr; + Cat* cat = nullptr; }; Cat::Cat (std::string pattern, std::string colour) @@ -475,14 +475,14 @@ struct Fruit struct FruitWrapper { - FruitWrapper (Fruit* f) : fruit (f) {} + FruitWrapper (Fruit* f) : fruit (f) {} ~FruitWrapper() - { + { delete fruit; - } + } - Fruit* fruit = nullptr; + Fruit* fruit = nullptr; }; Fruit::Fruit() @@ -689,14 +689,14 @@ struct Person struct PersonWrapper { - PersonWrapper (Person* p) : person (p) {} + PersonWrapper (Person* p) : person (p) {} ~PersonWrapper() - { + { delete person; - } + } - Person* person = nullptr; + Person* person = nullptr; }; Person::Person() @@ -760,17 +760,17 @@ struct TrashBin struct TrashBinWrapper { - TrashBinWrapper (TrashBin* tb) : trashBin (tb) {} + TrashBinWrapper (TrashBin* tb) : trashBin (tb) {} ~TrashBinWrapper() - { + { delete trashBin; - } + } - TrashBin* trashBin = nullptr; + TrashBin* trashBin = nullptr; }; -TrashBin::TrashBin(Cat& nearbyCat) : seed{15} +TrashBin::TrashBin (Cat& nearbyCat) : seed{15} { std::cout << std::endl << "A TrashBin has popped into existence, complete with trash (and cats)!" << std::endl; std::cout << "The smell is attacting some cats!" << std::endl; From 53530722d6b8ba0009ba9198f2d76724ca92850e Mon Sep 17 00:00:00 2001 From: matkatmusic Date: Fri, 13 Dec 2024 13:57:10 -0800 Subject: [PATCH 10/10] Added part 4 --- main.cpp | 113 ++++++++++++++++++++++--------------------------------- 1 file changed, 46 insertions(+), 67 deletions(-) diff --git a/main.cpp b/main.cpp index 8f206f58..aa277069 100644 --- a/main.cpp +++ b/main.cpp @@ -1,90 +1,69 @@ /* - Project 5: Part 3 / 4 - video: Chapter 3 Part 4: + Project 5: Part 4 / 4 + video: Chapter 3 Part 7 -Create a branch named Part3 - - the 'new' keyword - - 1) add #include "LeakedObjectDetector.h" to main + Create a branch named Part4 - 2) Add 'JUCE_LEAK_DETECTOR(OwnerClass)' at the end of your UDTs. + Don't #include what you don't use - 3) write the name of your class where it says "OwnerClass" + 1) Your task is to refactor your Chapter 3 Part 4 task into separate source and header files. + Add files via the pane on the left. - 4) write wrapper classes for each type similar to how it was shown in the video + 2) Put all of your declarations for each class in .h files + One header file per class ( i.e. Raider.h for a class named "Raider" ) + Don't forget the '#pragma once' - 5) update main() - replace your objects with your wrapper classes, which have your UDTs as pointer member variables. - - This means if you had something like the following in your main() previously: -*/ -#if false - Axe axe; - std::cout << "axe sharpness: " << axe.sharpness << "\n"; - #endif - /* - you would update that to use your wrappers: - - */ - -#if false -AxeWrapper axWrapper( new Axe() ); -std::cout << "axe sharpness: " << axWrapper.axPtr->sharpness << "\n"; -#endif -/* -notice that the object name has changed from 'axe' to 'axWrapper' -You don't have to do this, you can keep your current object name and just change its type to your Wrapper class - -6) If you have a class that has a nested class in it, and an instantiation of that nested class as a member variable, - - you do not need to write a Wrapper for that nested class - - you do not need to replace that nested instance with a wrapped instance. - If you want an explanation, message me in Slack - -7) If you were using any UDTs as function arguments like this: -*/ -#if false -void someMemberFunction(Axe axe); -#endif -/* - Pass those arguments by Reference now that you know what references are (Project 6 Part 2). -*/ -#if false -void someMemberFunction(Axe& axe); -#endif -/* -If you aren't modifying the passed-in object inside the function, pass by 'const reference'. -Marking a function parameter as 'const' means that you are promising that the parameter will not be modified. -Additionally, you can mark class member functions as 'const' -If you do this, you are promising that the member function will not modify any member variables. + 3) Put all of your implementations in .cpp files. + one cpp file per class ( i.e. Raider.cpp for a class named "Raider" ) + + 4) Put all of your Wrapper classes in a single Wrappers.h file + if you implemented your wrapper class functions in-class, you'll need to move them to Wrappers.cpp, which goes along with instruction 5): + + 5) NO IN-CLASS IMPLEMENTATION ALLOWED. + the only exception is the existing Atomic.h and LeakedObjectDetector.h + Chances are extremely high that you implemented your + wrapper class functions in-class, because that is what + everyone does after watching the video where I implemented + them in-class. + + 6) for every .cpp file you have to make, insert it into the .replit file after 'main.cpp'. Don't forget the spaces between file names. + If you need help with this step, send me a DM. -Mark every member function that is not modifying any member variables as 'const' + 7) When you add the #include statements for your refactored source files into main.cpp: + '#include "Wrappers.h"' should be the first file that you include after LeakedObjectDetector.h + + 8) Go through every cpp file and header file. check all function implementations for the following: + Can this function argument be declared as 'const'? + Can this function be declared as 'const'? + You learned about 'const' function arguments and 'const' functions in Project 5 Part 3 + As a refresher: + If you aren't modifying the passed-in object inside the function, pass by 'const reference'. + Marking a function parameter as 'const' means that you are promising that the parameter will not be modified. + Additionally, you can mark class member functions as 'const' + If you do this, you are promising that the member function will not modify any member variables. + + Mark every member function that is not modifying any member variables as 'const' + Mark every function parameter that is not modified inside the function as 'const' */ #if false -//a function where the argument is passed by const-ref -void someMemberFunction(const Axe& axe); +//a function where the argument is passed by const-ref. +void Foo::someMemberFunction(const Axe& axe); //a member function that is marked const, meaning it will not modify any member variables of the 'Axe' class. void Axe::aConstMemberFunction() const { } #endif /* - 8) After you finish, click the [run] button. Clear up any errors or warnings as best you can. + 9) click the [run] button. Clear up any errors or warnings as best you can. - see here for an example: https://repl.it/@matkatmusic/ch3p04example - - Clear any warnings about exit-time-destructors. - Suppress them by adding -Wno-exit-time-destructors to the .replit file with the other warning suppression flags + Remember, your Chapter 3 Part 4 task worked when it was all in one file. so you shouldn't need to edit any of it. + just split it up into source files and provide the appropriate #include directives. + tip: you cannot forward-declare nested types! + The file that a nested type is defined in MUST be included wherever that nested type is written. */ -/* - - Ignore the Atomic.h and LeakedObjectDetector.h files for now. - You will use those in Part 3 of this project. -*/ - #include #include "LeakedObjectDetector.h" /*