Advertisement
vladkomarr

plants2

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