Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 19.05.2017, 01:34   #1
Graduskipeniya
Новичок
Джуниор
 
Регистрация: 19.05.2017
Сообщений: 1
По умолчанию Задача на классы, конструктор с параметром и без.

Необходимо создать класс со следующим содержанием:
Продукт | Категория | Цена | Количество |.
После вывода таблицы необходимо, чтобы выводилось значение полной стоимости за n-ное количество товараю

Не могу понять, как посчитать стоимость продукта за n штук.

Листинг Catalog.h

Код:
#pragma once
#ifndef Catalog_H
#include «stdafx.h»
#include
# include
#include
#define MAX_STRING_LEN 255
using namespace std;
class Catalog
{
protected:
char* product;
char* category;
int price;
int quantity;
int totalprice;
public:
void init();
void show();
void setProduct(char * _product);
void setCategory(char * _category);
void setPrice(int _price);
void setQuantity(int _quantity);
char * getProduct();
char * getCategory();
int getPrice();
int getQuantity();
Catalog();
Catalog(char * _product, char * _category, int _price, int _quantity);
};
#endif


Листинг Catalog.cpp
Код:
#include «stdafx.h»
#include «catalog.h»
#include
void Catalog::init()
{
product = new char[MAX_STRING_LEN];
memset(product, 0, MAX_STRING_LEN);
category = new char[MAX_STRING_LEN];
memset(category, 0, MAX_STRING_LEN);
price = 0;
quantity = 0;
}
Catalog::Catalog()
{
init();
cout << «Constructor with default params\n» << endl;
}
Catalog::Catalog(char* _product, char* _category, int _price, int _quantity)
{
init();
setProduct(_product);
setCategory(_category);
setPrice(_price);
setQuantity(_quantity);
printf(«Constructor with params\n»);
}
//setters
void Catalog::setProduct(char* _product)
{
if (_product == NULL)
{
printf("_product == NULL");
return;
}
strcpy(product, _product);
}
void Catalog::setCategory(char* _category)
{
if (_category == NULL)
{
printf("_product == NULL");
return;
}
strcpy(category, _category);
}
void Catalog::setPrice(int _price)
{
if (_price == NULL)
{
printf("_price ==NULL");
return;
}
price = _price;
}
void Catalog::setQuantity(int _quantity)
{
if (_quantity == NULL)
{
printf("_price ==NULL");
return;
}
quantity = _quantity;
}
//void function other*
//getters
char* Catalog::getProduct()
{
return product;
}
char* Catalog::getCategory()
{
return category;
}
int Catalog::getPrice()
{
return price;
}
int Catalog::getQuantity()
{
return quantity;
}
//implement show method
void Catalog::show()
{
printf(«product = %s | category = %s | price = %d | quantity = %d \n», getProduct(), getCategory(), getPrice(), getQuantity());
}



Листинг Catalog-main.cpp
Код:
// Catalog.cpp: определяет точку входа для консольного приложения.
//
#include «stdafx.h»
[HTML]#include
#include
#include
#include «catalog.h»
int _tmain(int argc, _TCHAR* argv[])
{
//default constructor
Catalog Catalog1;
Catalog1.setProduct(«Drink_H2O»);
Catalog1.setCategory(«beverages»);
Catalog1.setPrice(1);
Catalog1.setQuantity(19);
Catalog1.show();
//constructor with params
Catalog Catalog2(«Oil drill», «Industrial tools», 1000000, 1);
Catalog2.show();
int size;
cout << «Please enter the dimension of the array: »;
cin >> size;
Catalog** catalogs = new Catalog*[size];
char *product = new char[MAX_STRING_LEN];
memset(product, 0, MAX_STRING_LEN);
char *category = new char[MAX_STRING_LEN];
memset(category, 0, MAX_STRING_LEN);
int price = 0;
int quantity = 0;
for (int i = 0; i < size; i++)
{
catalogs[i] = new Catalog();
cout << «Please enter product name: »;
cin >> product;
catalogs[i]->setProduct(product);
cout << «Please enter category: »;
cin >> category;
catalogs[i]->setCategory(category);
cout << «Please enter price: »;
cin >> price;
catalogs[i]->setQuantity(quantity);
cout << «Please enter quantity: »;
cin >> quantity;
cout << "*****************************\n";
}
for (int i = 0; i < size; i++)
catalogs[i]->show();
cout << "\n List and total price: ";
system(«pause»);
}
Ошибки:





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

Код:
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 ==========
Graduskipeniya вне форума Ответить с цитированием
Старый 19.05.2017, 07:15   #2
p51x
Старожил
 
Регистрация: 15.02.2010
Сообщений: 15,709
По умолчанию

ПРОЧИТАЙТЕ, ЧТО ВАМ НАПИСАЛ КОМПИЛЯТОР!
p51x вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Классы (конструктор/деструктор) Satansoft Помощь студентам 5 27.03.2013 14:20
Процедура с параметром и без параметра admin22 Паскаль, Turbo Pascal, PascalABC.NET 2 27.09.2011 15:44
Конструктор с параметром Prestigo C++ Builder 1 14.06.2011 16:04
классы,конструктор копирования,динамический массив экземпляров.Нужна помощь bylynka Общие вопросы C/C++ 1 30.04.2009 21:56