10.18 LAB: 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 Cost: 6 Annual: false Color of flowers: white Plant Information: Plant name: Mint Cost: 4
------------------------------------------------------------------------------------------------------------------------------------------------------------
main.ccp (Please edit main)
#include "Plant.h"
#include "Flower.h"
#include <vector>
#include <string>
#include <iostream>
using namespace std;
// TODO: Define a PrintVector function that prints an
vector of plant (or flower) object pointers
void PrintVector(vector<Plant*> myGarden, int vSize){
for(int i=0;i<vSize;i++){
myGarden[i]->PrintInfo();
cout <<endl;
}
}
int main(int argc, char* argv[]) {
// TODO: Declare a vector called myGarden that can hold object of
type plant pointer
vector<Plant *> myGarden;
// TODO: Declare variables - plantName, plantCost,
flowerName, flowerCost,
// colorOfFlowers, isAnnual
string input;
cin >> input;
while(input != "-1") {
// TODO: Check if input is a plant or flower
// Store as a plant object or flower object
// Add to the vector myGarden
//if input=plant
Plant *pl=new Plant();
//get plant name and plant cost
pl->SetPlantName(userPlantName);
pl->SetPlantCost(userPlantCost);
//push_back object in vector myGarden
//if input=flower
//declare the flower object pinter and use new to allocate
memory
//use the set functions
//push_back object in vector myGarden
cin >> input;
}
// TODO: Call the method PrintVector to print myGarden
for (size_t i = 0; i < myGarden.size(); ++i) {
delete myGarden.at(i);
}
return 0;
}
----------------------------------------------------------------------------------------------
Plant.h
#ifndef PLANTH
#define PLANTH
#include <string>
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 <iostream>
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 << "Plant Information:" << endl;
cout << " Plant name: " << plantName <<
endl;
cout << " Cost: " << plantCost << endl;
}
--------------------------------------------------------------------------------------------------------------------------------
Flower.h
#ifndef FLOWERH
#define FLOWERH
#include "Plant.h"
#include <string>
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 <iostream>
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 << "Plant Information:" << endl;
cout << " Plant name: " << plantName <<
endl;
cout << " Cost: " << plantCost << endl;
cout << " Annual: " << boolalpha << isAnnual
<< endl;
cout << " Color of flowers: " << colorOfFlowers
<< endl;
}
Language upon which implemented : Visual C++
IDE/Editor used : Visual Studio
Summary : Hi Student. As per your code and logic, I have developed a nice menu driven Base program in order to add Garden details and print Garden Details. Rest All things are almost same, I have moved the Add Details to a separate method AddToGarden for the sake of modularity.
Code :
Note : You can run the below mentioned Main.cpp (the main logic) file taking your own compiler. But if you want to run this program in visual studio perfectly, yo need to edit the various files. The small changes, done, are discussed below :
0. Add a new file named stdafx.h which has following code :
#pragma once
#include <SDKDDKVer.h>
#include <stdio.h>
#include <string>
#include <tchar.h>
1. Flower.cpp :
Add a simple line #include "stdafx.h" at top of this file
2. Plant.ccp
Add a simple line #include "stdafx.h" at top of this
file
3. Add a new File stdafx.cpp which has followng 1 line code :
#include "stdafx.h"
4. Main.cpp
#include "stdafx.h"
#include "Plant.h"
#include "Flower.h"
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<Plant *> myGarden;
void PrintVector() {
cout << endl << "----------- My Garden
Details -------------" << endl << endl;
for (int i = 0; i < myGarden.size(); i++) {
myGarden[i]->PrintInfo();
cout << endl;
}
cout << endl << "----------- End of Garden
Details -------------" << endl;
}
void AddToGarden() {
cout << "Enter the type : (plant/flower) :
";
string input, plantName, flowerName, flowerCost,
colorOfFlowers;
float plantCost;
char isAnnual;
cin >> input;
transform(input.begin(), input.end(), input.begin(),
::tolower); //input convert to lower case
Plant *pl = new Plant();
Flower *fl = new Flower();
if (input == "plant") { //type of plant
//Inputting the plant details
cout << "Enter Plant Name :
";
cin >> plantName;
cout << "Enter Plant Cost :
";
cin >> plantCost;
pl->SetPlantName(plantName);
pl->SetPlantCost(plantCost);
myGarden.push_back(pl);
cout << input << "
Details stored successfully" << endl;
}
else if (input == "flower") { // type of flower
//Inputting the flower
details
cout << "Enter Plant Name :
";
cin >> plantName;
cout << "Enter Plant Cost :
";
cin >> plantCost;
fl->SetPlantName(plantName);
fl->SetPlantCost(plantCost);
//Enter the Flower color
cout << "Enter the Color of
flower : ";
cin >> colorOfFlowers;
fl->SetColorOfFlowers(colorOfFlowers);
//Enter the Annual
cout << "Is the Plant Annual
? (Y/N) : ";
cin >> isAnnual;
isAnnual = toupper(isAnnual); //
uppercase conversion
fl->SetPlantType(isAnnual == 'Y'
? true : false);
myGarden.push_back(fl);
cout << input << "
Details stored successfully" << endl;
}
else {
cout << "Wrong type of input
given" << endl;
}
}
int main(int argc, char* argv[]) {
cout << "---------------- Welcome to my Garden --------------------" << endl;
//Menu Driven program for adding, print the Garden
Details
while (true) {
cout << endl << "Press
:\n1 for Add Information for My Garden" << endl
<<
"2 for Print Garden Details"
<< endl << "3 for Exit" << endl << "\n\t
Enter your choice : ";
int choice;
cin >> choice;
switch (choice)
{
case 1:
AddToGarden(); break;
case 2:
PrintVector(); break;
case 3: for
(size_t i = 0; i < myGarden.size(); ++i) {
delete
myGarden.at(i);
}
cout << "Press Any Key
to exit ";
getchar();
exit(0);
default: cout
<< "Invalid Option Selected. Please try again"; break;
}
}
return 0;
}
Code Run Snapshot (If any) :
Note: The program is case in-sensitive, so you are free to given any case of input

If you have some query, then feel free to comment.
If you find this answer helpful, consider up-voting this
answer
10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...
10.16 LAB: Plant information (ArrayList)Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods 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 ArrayList, and output each element in myGarden using the printInfo() method.Ex. If the input is:plant Spirea 10 flower Hydrangea 30 false lilac flower Rose 6 false white plant Mint 4...
previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() { //create Animal object Animal animal("Dog","Mammal",4); cout<<"Animal object details"<<endl; cout<<"Name: "<<animal.getName()<<endl; cout<<"Type: "<<animal.getType()<<endl; cout<<"# of Legs: "<<animal.getLegs()<<endl; cout<<endl<<endl; //create Cat object Cat cat("byssinian cat","carnivorous mammal",4,"short...
C++ 15.15 Lab: Abstract base class Base Class The base Character class is located Character.h and cannot be changed. enum HeroType {WARRIOR, ELF, WIZARD}; const double MAX_HEALTH = 100.0; class Character { protected: HeroType type; string name; double health; double attackStrength; public: Character(HeroType type, const string &name, double health, double attackStrength); HeroType getType() const; const string & getName() const; /* Returns the whole number of the health value (static_cast to int). */ int getHealth() const; void setHealth(double h); /* Reduces...
You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library. I've completed the GroceryItem.h...
Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public: static string weekDays[7]; void print() const; string nextDay() const; string prevDay() const; void addDay(int nDays); void setDay(string d); string getDay() const; dayType(); dayType(string d); private: string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...
hello there. can you please help me to complete this code. thank you. Lab #3 - Classes/Constructors Part I - Fill in the missing parts of this code #include<iostream> #include<string> #include<fstream> using namespace std; class classGrades { public: void printlist() const; void inputGrades(ifstream &); double returnAvg() const; void setName(string); void setNumStudents(int); classGrades(); classGrades(int); private: int gradeList[30]; int numStudents; string name; }; int main() { ...
Here is the code from the previous three steps:
#include <iostream>
using namespace std;
class Student
{
private:
//class variables
int ID;
string firstName,lastName;
public:
Student(int ID,string firstName,string lastName)
//constructor
{
this->ID=ID;
this->firstName=firstName;
this->lastName=lastName;
}
int getID() //getter method
{
return ID;
}
virtual string getType() = 0; //pure virtual function
virtual void printInfo() //virtual function to print basic details
of a student
{
cout << "Student type: " << getType() <<
endl;
cout << "Student ID: " << ID...
#include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital, double stArea, int yAdm, int oAdm) { stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission; } void stateData::getStateInfo(string& sName, string& sCapital, double& stArea, int& yAdm, int& oAdm) { sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission; } string stateData::getStateName() { return stateName;...
Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...
1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...