vladkomarr

plants

Oct 30th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.36 KB | None | 0 0
  1. //main.cpp
  2. #include <iostream>
  3. #include <multipleplants.h>
  4.  
  5. using namespace std;
  6. int main()
  7. {
  8.     int choice;
  9.     void clrScr();                          // clears the screen
  10.     void menu();                            // shows the menu
  11.     std::vector<Plant*> vec;                // vector of plants
  12.     Plant *temp;
  13.     do {
  14.         menu();
  15.         std::cin >> choice;
  16.         clrScr();
  17.         switch(choice) {
  18.         case HERB:
  19.             temp = new Herb();
  20.             temp->input();
  21.             vec.push_back(temp);
  22.             break;
  23.         case BUSH:
  24.             temp = new Bush();
  25.             temp->input();
  26.             vec.push_back(temp);
  27.             break;
  28.         case TREE:
  29.             temp = new Tree();
  30.             temp->input();
  31.             vec.push_back(temp);
  32.             break;
  33.         case PRINT:
  34.             for(int i = 0; i < vec.size(); i++)
  35.                 vec[i]->output();
  36.             break;
  37.         default:
  38.             std::cout << "Invalid input.\n";
  39.             break;
  40.         }
  41.     } while(choice != EXIT);
  42.  
  43.     return 0;
  44. }
  45.  
  46. //plant.h
  47. #ifndef PLANT_H
  48. #define PLANT_H
  49. #include <iostream>
  50. #include <vector>
  51.  
  52. enum { HERB = 1, BUSH, TREE, PRINT, EXIT };
  53.  
  54. class Plant {
  55. private:
  56.     std::string name;                               // name of plant
  57.     std::string origin;                             // country of origin
  58.     double height;                                  // height in meters
  59. protected:
  60.     static int plantCounter;                        // the num of existing plants
  61. public:
  62.     Plant();                                        // constructor without arguments
  63.     virtual void input() = 0;                       // input info
  64.     virtual void output() = 0;                      // output info
  65.     virtual ~Plant();                               // virtual destructor
  66.  
  67.     static int getPlantsNum();                      // get the value of plantCounter
  68. };
  69.  
  70.  
  71. #endif // PLANT_H
  72.  
  73. //plant.cpp
  74. #include "plant.h"
  75.  
  76. int Plant::plantCounter = 0;
  77. std::vector<Plant*> vec;
  78.  
  79. Plant::Plant() :
  80.     name(""), origin(""), height(0) {
  81.     plantCounter++;
  82. }
  83.  
  84. void Plant::input() {
  85.     std::cout << "Enter the plant info, plx:\n";
  86.     std::cout << "Name: ";   std::cin >> name;
  87.     std::cout << "Origin: "; std::cin >> origin;
  88.     std::cout << "Height: "; std::cin >> height;
  89. }
  90.  
  91. void Plant::output() {
  92.     std::cout << "Name:\t"   << name   << std::endl
  93.               << "Origin:\t" << origin << std::endl
  94.               << "Height:\t" << height << std::endl;
  95. }
  96.  
  97. int Plant::getPlantsNum() { return plantCounter; }
  98.  
  99. Plant::~Plant() {
  100.     plantCounter--;
  101. }
  102.  
  103. //multiplePlants.h
  104. #ifndef MULTIPLEPLANTS_H
  105. #define MULTIPLEPLANTS_H
  106. #include <iostream>
  107. #include <plant.h>
  108. typedef unsigned int UI;
  109.  
  110. class Herb : public Plant {
  111. private:
  112.     std::string petalsColor;                // color of petals
  113.     UI age;                                 // 1..n year aged herbs
  114. public:
  115.     Herb();                                 // constructor without arguments
  116.     void input();                           // input herb info
  117.     void output();                          // output herb info
  118.     ~Herb();                                // destructor
  119. };
  120.  
  121.  
  122. class Tree: public Plant {
  123. private:
  124.     std::string fruit;                      // type of fruit
  125.     std::string type;                       // <evergreen/coniferous/deciduous>
  126. public:
  127.     Tree();                                 // constructor without arguments
  128.     void input();                           // input tree info
  129.     void output();                          // output tree info
  130.     ~Tree();                                // destructor
  131. };
  132.  
  133.  
  134. class Bush: public Plant {
  135. private:
  136.     std::string soil;                       // needed soil
  137.     double tempResistance;                  // temperature resistance
  138. public:
  139.     Bush();                                 // constructor without parameters
  140.     void input();                           // input bush info
  141.     void output();                          // output bush info
  142.     ~Bush();                                // destructor
  143. };
  144.  
  145.  
  146. #endif // MULTIPLEPLANTS_H
  147.  
  148. //multiplePlants.cpp
  149. #include "multipleplants.h"
  150.  
  151. //herb description
  152.  
  153. Herb::Herb() :
  154.     Plant(), petalsColor(""), age(0) {
  155.     std::cout << "Created new herb.\n";
  156. }
  157.  
  158. Herb::~Herb()
  159.     { std::cout << "Deleted herb.\n"; }
  160.  
  161. void Herb::input() {
  162.     Plant::input();
  163.     std::cout << "Petals color: ";   std::cin >> petalsColor;
  164.     std::cout << "Ages: ";           std::cin >> age;
  165. }
  166.  
  167. void Herb::output() {
  168.     std::cout << "-------------------------------\nHerb\n";
  169.     Plant::output();
  170.     std::cout << "Petals color:\t" << petalsColor << std::endl
  171.               << "Ages:\t"         << age         << std::endl;
  172. }
  173.  
  174. //tree description
  175.  
  176. Tree::Tree() :
  177.     Plant(), fruit(""), type("")
  178.     { std::cout << "Created new tree.\n";}
  179.  
  180. Tree::~Tree()
  181.     { std::cout << "Deleted tree.\n";}
  182.  
  183. void Tree::input() {
  184.     Plant::input();
  185.     std::cout << "Type of fruit: ";   std::cin >> fruit;
  186.     std::cout << "Type of tree: ";    std::cin >> type;
  187. }
  188.  
  189. void Tree::output() {
  190.     std::cout << "-------------------------------\nTree\n";
  191.     Plant::output();
  192.     std::cout << "Type of fruit:\t" << fruit << std::endl
  193.               << "Type of tree:\t"  << type  << std::endl;
  194. }
  195.  
  196. // bush description
  197.  
  198. Bush::Bush() :
  199.     Plant(), soil(""), tempResistance(0)
  200.     { std::cout << "Created new bush.\n"; }
  201.  
  202. Bush::~Bush()
  203.     { std::cout << "Deleted bush.\n"; }
  204.  
  205. void Bush::input() {
  206.     Plant::input();
  207.     std::cout << "Needed soil: "; std::cin >> soil;
  208.     std::cout << "Temperature: "; std::cin >> tempResistance;
  209. }
  210.  
  211. void Bush::output() {
  212.     std::cout << "-------------------------------\nBush\n";
  213.     Plant::output();
  214.     std::cout << "Soil:\t"        << soil           << std::endl
  215.               << "Temperature:\t" << tempResistance << std::endl;
  216. }
  217.  
  218. void menu() {
  219.     std::cout << "-------------------------------" << std::endl
  220.               <<  "Number of all elements: "
  221.               << Tree::getPlantsNum() << std::endl;
  222.     std::cout << "1. New herb" << std::endl
  223.               << "2. New bush" << std::endl
  224.               << "3. New tree" << std::endl
  225.               << "4. Print all info" << std::endl
  226.               << "5. Exit" << std::endl;
  227. }
  228.  
  229. void clrScr() {
  230.     std::cout << "\033[2J\033[1;1H";
  231. }
Advertisement
Add Comment
Please, Sign In to add comment