Advertisement
Infiniti_Inter

классы (стр 35 задание 7)

May 21st, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. ifstream fin("input.txt");
  11.  
  12. class Goods {
  13. protected:
  14.     string name;
  15.     double cost;
  16.     string made;
  17.     int age;
  18.     int type;
  19.  
  20. public:
  21.     virtual void show() = 0;
  22.     void printType()
  23.     {
  24.         if (type == 1)
  25.             cout << "Toy\n";
  26.         if (type == 2)
  27.             cout << "Book\n";
  28.         if (type == 3)
  29.             cout << "Sports Equipment\n";
  30.  
  31.     }
  32.     Goods(string name, double cost, string made, int age)
  33.         : name(name), cost(cost), made(made), age(age) {};
  34.  
  35. };
  36. //Toy(название, цена, производитель, материал,возраст, на который рассчитана),
  37. class Toy : public Goods
  38. {
  39.     string material;
  40. public:
  41.  
  42.     Toy(string name, double cost, string made, int age, string material)
  43.         :Goods(name, cost, made, age), material(material) {
  44.         type = 1;
  45.     };
  46.     void show() override
  47.     {
  48.         cout << name << ' ' << cost << ' ' << made << ' ' << age
  49.             << ' ' << material << endl << endl;
  50.     }
  51.  
  52. };
  53. //, Book (название, автор, цена, издательство, возраст, на который рассчитана)
  54. class Book : public Goods {
  55.     string autor;
  56. public:
  57.     Book(string name, double cost, string made, int age, string autor)
  58.         :Goods(name, cost, made, age), autor(autor) {
  59.         type = 2;
  60.     };
  61.     void show() override
  62.     {
  63.         cout << name << ' ' << cost << ' ' << made << ' ' << age
  64.             << ' ' << autor << endl << endl;
  65.     }
  66. };
  67. // SportsEquipment (название, цена, производитель, возраст, на который рассчитан).
  68.  
  69. class SportsEquipment : public Goods {
  70. public:
  71.     SportsEquipment(string name, double cost, string made, int age)
  72.         :Goods(name, cost, made, age) {
  73.         type = 3;
  74.     };
  75.     void show() override
  76.     {
  77.         cout << name << ' ' << cost << ' ' << made << ' ' << age
  78.             << endl;
  79.     }
  80. };
  81. const int MAXSIZE = 100;
  82. int main()
  83. {
  84.     /*
  85.     3
  86.     1 MyFistToy 666 Russia 18 LaTeX
  87.     2 MyFirstMatAnTime 228 USA 50 Sahno
  88.     3 MyFirstSki 333 France 4
  89.     */
  90.     int n;
  91.     fin >> n;
  92.     Goods* a[MAXSIZE];
  93.     for (int i = 0; i < n; ++i)
  94.     {
  95.         int type;
  96.  
  97.         fin >> type;
  98.         string name, made;
  99.         double cost;
  100.         int age;
  101.         string feature;
  102.         fin >> name >> cost >> made >> age;
  103.         if (type != 3)
  104.             fin >> feature;
  105.         if (type == 1)
  106.             a[i] = new Toy(name, cost, made, age, feature);
  107.         if (type == 2)
  108.             a[i] = new Book(name, cost, made, age, feature);
  109.         if (type == 3)
  110.             a[i] = new SportsEquipment(name, cost, made, age);
  111.     }
  112.     for (int i = 0; i < n; ++i)
  113.     {
  114.         a[i]->show();
  115.         cout << "It's a ";
  116.         a[i]->printType();
  117.         cout << endl;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement