Класс. Конструктор с параметром и без. - вопрос №2460239

Необходимо создать программу, где выводились бы две таблицы (констр. с параметром и без соответственно) с данными наименование продукта, категория, стоимость, количество. Причем сначала должна выводиться таблица без подсчета общей стоимости каждого товара за n-ное количество. Подскажите, как сделать так, чтобы программа работала, чтобы высчитывалась сумма (кол-ва * цена за шт.). Вот, что пока получилось и ошибки, что выходят. Не могу понять, как посчитать стоимость продукта за n штук.

Листинг Catalog.h
  1. #pragma once
  2. #ifndef Catalog_H
  3. #include «stdafx.h»
  4. #include
  5. # include
  6. #include
  7. #define MAX_STRING_LEN 255
  8. using namespace std;
  9. class Catalog
  10. {
  11. protected:
  12. char* product;
  13. char* category;
  14. int price;
  15. int quantity;
  16. int totalprice;
  17. public:
  18. void init();
  19. void show();
  20. void setProduct(char * _product);
  21. void setCategory(char * _category);
  22. void setPrice(int _price);
  23. void setQuantity(int _quantity);
  24. char * getProduct();
  25. char * getCategory();
  26. int getPrice();
  27. int getQuantity();
  28. Catalog();
  29. Catalog(char * _product, char * _category, int _price, int _quantity);
  30. };
  31. #endif




ЛистингCatalog.cpp
  1. #include «stdafx.h»
  2. #include «catalog.h»
  3. #include
  4. void Catalog::init()
  5. {
  6. product = new char[MAX_STRING_LEN];
  7. memset(product, 0, MAX_STRING_LEN);
  8. category = new char[MAX_STRING_LEN];
  9. memset(category, 0, MAX_STRING_LEN);
  10. price = 0;
  11. quantity = 0;
  12. }
  13. Catalog::Catalog()
  14. {
  15. init();
  16. cout << «Constructor with default params\n» << endl;
  17. }
  18. Catalog::Catalog(char* _product, char* _category, int _price, int _quantity)
  19. {
  20. init();
  21. setProduct(_product);
  22. setCategory(_category);
  23. setPrice(_price);
  24. setQuantity(_quantity);
  25. printf(«Constructor with params\n»);
  26. }
  27. //setters
  28. void Catalog::setProduct(char* _product)
  29. {
  30. if (_product == NULL)
  31. {
  32. printf("_product == NULL");
  33. return;
  34. }
  35. strcpy(product, _product);
  36. }
  37. void Catalog::setCategory(char* _category)
  38. {
  39. if (_category == NULL)
  40. {
  41. printf("_product == NULL");
  42. return;
  43. }
  44. strcpy(category, _category);
  45. }
  46. void Catalog::setPrice(int _price)
  47. {
  48. if (_price == NULL)
  49. {
  50. printf("_price ==NULL");
  51. return;
  52. }
  53. price = _price;
  54. }
  55. void Catalog::setQuantity(int _quantity)
  56. {
  57. if (_quantity == NULL)
  58. {
  59. printf("_price ==NULL");
  60. return;
  61. }
  62. quantity = _quantity;
  63. }
  64. //void function other*
  65. //getters
  66. char* Catalog::getProduct()
  67. {
  68. return product;
  69. }
  70. char* Catalog::getCategory()
  71. {
  72. return category;
  73. }
  74. int Catalog::getPrice()
  75. {
  76. return price;
  77. }
  78. int Catalog::getQuantity()
  79. {
  80. return quantity;
  81. }
  82. //implement show method
  83. void Catalog::show()
  84. {
  85. printf(«product = %s | category = %s | price = %d | quantity = %d \n», getProduct(), getCategory(), getPrice(), getQuantity());
  86. }




Листинг Catalog-main.cpp

  1. // Catalog.cpp: определяет точку входа для консольного приложения.
  2. //
  3. #include «stdafx.h»
  4. #include
  5. #include
  6. #include
  7. #include «catalog.h»
  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10. //default constructor
  11. Catalog Catalog1;
  12. Catalog1.setProduct(«Drink_H2O»);
  13. Catalog1.setCategory(«beverages»);
  14. Catalog1.setPrice(1);
  15. Catalog1.setQuantity(19);
  16. Catalog1.show();
  17. //constructor with params
  18. Catalog Catalog2(«Oil drill», «Industrial tools», 1000000, 1);
  19. Catalog2.show();
  20. int size;
  21. cout << «Please enter the dimension of the array: »;
  22. cin >> size;
  23. Catalog** catalogs = new Catalog*[size];
  24. char *product = new char[MAX_STRING_LEN];
  25. memset(product, 0, MAX_STRING_LEN);
  26. char *category = new char[MAX_STRING_LEN];
  27. memset(category, 0, MAX_STRING_LEN);
  28. int price = 0;
  29. int quantity = 0;
  30. for (int i = 0; i < size; i++)
  31. {
  32. catalogs[i] = new Catalog();
  33. cout << «Please enter product name: »;
  34. cin >> product;
  35. catalogs[i]->setProduct(product);
  36. cout << «Please enter category: »;
  37. cin >> category;
  38. catalogs[i]->setCategory(category);
  39. cout << «Please enter price: »;
  40. cin >> price;
  41. catalogs[i]->setQuantity(quantity);
  42. cout << «Please enter quantity: »;
  43. cin >> quantity;
  44. cout << "*****************************\n";
  45. }
  46. for (int i = 0; i < size; i++)
  47. catalogs[i]->show();
  48. cout << "\n List and total price: ";
  49. system(«pause»);
  50. }




Ошибки:
изображение из вопроса




Вывод ошибок:

1>------ Сборка начата: проект: Catalog, Конфигурация: Debug Win32 ------ 1> Catalog.cpp 1>c:\users\ekaterina\documents\visual studio 2015\projects\catalog\catalog\catalog\catalog.cpp(40): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> c:\program files\windows kits\10\include\10.0.10240.0\ucrt\string.h(119): note: см. объявление «strcpy» 1>c:\users\ekaterina\documents\visual studio 2015\projects\catalog\catalog\catalog\catalog.cpp(50): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> c:\program files\windows kits\10\include\10.0.10240.0\ucrt\string.h(119): note: см. объявление «strcpy» ========== Сборка: успешно: 0, с ошибками: 1, без изменений: 0, пропущено: 0 ==========
19.05.17
1 ответ

Ответы

Здравствуйте. Вы используете небезопасную версию функции strcpy, которая в таком виде признана устаревшей. Используйте или безопасный вариант (strcpy_s), или поставьте при компиляции define __CRT_SECURE_NO_WARNINGS
Как соберете программу дальше уже можно смотреть.
Если нужна помощь по коду — пишите в чат, но услуга платная.
20.05.17
Посмотреть всех экспертов из раздела Технологии > C/C++
Пользуйтесь нашим приложением Доступно на Google Play Загрузите в App Store