Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Plant information (vector) Given a base Plant class and a derived Flower class, complete main( to create a vector called myGarden. The vector should be

Plant information (vector) Given a base Plant class and a derived Flower class, complete main( to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector, that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to the myGarden vector, and output each element in myGarden using the PrintInfo() function. Ex. If the input is: plant Spirea 10 flower Hydrangea 30 false lilac flower Rose 6 false white plant Mint 4 -1 the output is: Plant Information: Plant name: Spirea Cost: 10 Plant Information: Plant name: Hydrengea Cost: 30 Annual: false Color of flowers: lilac Plant Information: Plant name: Rose the output is: Plant Information: Plant name: Spirea Cost: 10 Plant Information: Plant name: Hydrengea Cost: 30 Annual: false Color of flowers: lilac Plant Information: Plant name: Rose Cost: Annual: false Color of flowers: white Plant Information: Plant name: Mint Cost: 4 LAB ACTIVITY 11.15.1: LAB: Plant information (vector) Current file: main.cpp Lo 1 #include \"Plant.h\" main.cpp 2 3 #include \"Flower.h\" Plant.h 4 5 #include \"Plant.cpp\" Plant.cpp 6 7 #include \"Flower.cpp\" 8 Flower.h 9 #include 10 11 #include Flower.cpp 12 13 #include 14 15 using namespace std; 16 17 // TODO: Define a PrintVector function that prints an vector of plant (or flower) object pointers 18 Program errors displayed here main.cpp: In function 'void PrintVector(std::vector)': main.cpp:23:15: warning: comparison of integer expressions of different signedness: 'int' and 'st 23 | for(int i=0; i();>,>,>*>

11.15LAB: Plant information (vector)

Given a basePlantclass and a derivedFlowerclass, complete main() to create a vector calledmyGarden. The vector should be able to store objects that belong to thePlantclass or theFlowerclass. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element inmyGarden. The program should read plants or flowers from input (ending with -1), adding eachPlantorFlowerto themyGardenvector, and output each element inmyGardenusing the PrintInfo() function.

I solved this question to the best of my ability, but I still getting an error. I need your help to fix this error. I will post a pictures with this error. Thank you.


Ex. If the input is:

plant Spirea 10flower Hydrangea 30 false lilacflower Rose 6 false whiteplant Mint 4-1



the output is:

Plant Information: Plant name: Spirea Cost: 10Plant Information: Plant name: Hydrengea Cost: 30 Annual: false Color of flowers: lilacPlant Information: Plant name: Rose Cost: 6 Annual: false Color of flowers: whitePlant Information: Plant name: Mint Cost: 4


--------------------main.cpp

#include \"Plant.h\"

#include \"Flower.h\"

#include \"Plant.cpp\"

#include \"Flower.cpp\"

#include

#include

#include

using namespace std;

// TODO: Define a PrintVector function that prints an vector of plant (or flower) object pointers

void PrintVector(vector myGarden)*>

{

for(int i=0; i();>

{

if(typeid(Flower)==typeid(myGarden[i]))

(dynamic_cast(myGarden[i]))->PrintInfo();*>

else

myGarden[i]->PrintInfo();

cout

}

}

int main(int argc, char* argv[]) {

// TODO: Declare a vector called myGarden that can hold object of type plant pointer

vector myGarden;*>

// TODO: Declare variables - plantName, plantCost, flowerName, flowerCost,

// colorOfFlowers, isAnnual

string plantName, flowerName, colorOfFlowers, isann;

bool isAnnual;

int plantCost, flowerCost;

string input;

Flower *flower;

Plant *plant;

cin >> input;

while(input != \"-1\") {

// TODO: Check if input is a plant or flower

if (input==\"plant\")

{

cin>>plantName;

cin>>plantCost;

plant = new Plant();

plant->SetPlantName(plantName);

plant->SetPlantCost(plantCost);

myGarden.push_back(plant);

}

else if(input==\"flower\")

{

cin>>flowerName;

cin>>flowerCost;

cin>>isann;

isAnnual = (isann==\"true\");

cin>>colorOfFlowers;

flower = new Flower();

flower->SetPlantName(flowerName);

flower->SetPlantCost(flowerCost);

flower->SetPlantType(isAnnual);

flower->SetColorOfFlowers(colorOfFlowers);

myGarden.push_back(flower);

}

// Store as a plant object or flower object

// Add to the vector myGarden

cin >> input;

}

// TODO: Call the method PrintVector to print myGarden

PrintVector(myGarden);

for (size_t i = 0; i

delete myGarden.at(i);

}

return 0;

}


----------------------Plant.h

#ifndef PLANTH #define PLANTH

#include using namespace std;

class Plant { public: virtual ~Plant();

void SetPlantName(string userPlantName);

string GetPlantName() const;

void SetPlantCost(int userPlantCost);

int GetPlantCost() const;

virtual void PrintInfo() const;

protected: string plantName; int plantCost; };

#endif


-------------------------------------Plant.cpp

#include \"Plant.h\" #include

Plant::~Plant() {};

void Plant::SetPlantName(string userPlantName) { plantName = userPlantName; }

string Plant::GetPlantName() const { return plantName; }

void Plant::SetPlantCost(int userPlantCost) { plantCost = userPlantCost; }

int Plant::GetPlantCost() const { return plantCost; }

void Plant::PrintInfo() const { cout cout cout }


-------------------------------------Flower.h

#ifndef FLOWERH #define FLOWERH

#include \"Plant.h\" #include using namespace std;

class Flower : public Plant { public: void SetPlantType(bool userIsAnnual);

bool GetPlantType() const;

void SetColorOfFlowers(string userColorOfFlowers);

string GetColorOfFlowers() const;

void PrintInfo() const;

private: bool isAnnual; string colorOfFlowers; };

#endif


---------------------------------------Flower.cpp

#include \"Flower.h\" #include

void Flower::SetPlantType(bool userIsAnnual) { isAnnual = userIsAnnual; }

bool Flower::GetPlantType() const { return isAnnual; }

void Flower::SetColorOfFlowers(string userColorOfFlowers) { colorOfFlowers = userColorOfFlowers; }

string Flower::GetColorOfFlowers() const { return colorOfFlowers; }

void Flower::PrintInfo() const { cout cout cout cout cout }











Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions