Question

10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LAB: Plant information (ArrayList)

    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...

  • You are given a partial implementation of two classes. GroceryItem is a class that holds the...

    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()....

    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>...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    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...

  • hello there. can you please help me to complete this code. thank you. Lab #3 -...

    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() {          ...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #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...

    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...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • Solve in C++ for Car.h and Car.cpp 8.21 LAB: Car value (classes) Given main, complete the...

    Solve in C++ for Car.h and Car.cpp 8.21 LAB: Car value (classes) Given main, complete the Car class (in files Car hand Car.cpp) with member functions to set and get the purchase price of a car (SetPurchase Price().GetPurchase Price)and to output the car's information (Printinfo). Ex If the input is 2011 18000 2018 where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is Car's information Model year: 2011 Purchase...

  • This is for my c++ class and I would really appreciate the help, Thank you! Complete...

    This is for my c++ class and I would really appreciate the help, Thank you! Complete the definitions of the functions for the ConcessionStand class in the ConcessionStand.cpp file. The class definition and function prototypes are in the provided ConcessionStand.h header file. A testing program is in the provided main.cpp file. You don’t need to change anything in ConcessionStand.h or main.cpp, unless you want to play with different options in the main.cpp program. ___________________ Main.cpp ____________________ #include "ConcessionStand.h" #include <iostream>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT