Определить пользовательский класс.
Определить в классе методы для просмотра и установки атрибутов класса.
Определить в классе метод для вывода информации.
Написать демонстрационную программу, в которой реализовать следующие
действия: инициализировать массив из 5 элементов, каждый элемент которого
является объектом класса, заполнить массив значениями, введенными с клавиатуры,
вывести на экран информацию, содержащуюся в атрибутах класса, используя
инкапсулированный метод вывода информации, вывести на экран информацию о
свойствах класса, используя инкапсулированные методы.
название класса : Изделие
Атрибуты
название – char[20]
шифр – char[20]
количество – int
C/C++
С ++ Написать демонстрационную программу, в которой реализовать следующие действия:
#include <iostream>
#include <array>
#include <cstring>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;
class Product {
public:
Product() {
clear();
}
int quantity()const {
return q;
}
void quantity(const int value) {
q = value;
}
const char* title()const {
return t;
}
void title(const char* src) {
memset(t, 0, length);
auto len = strlen(src);
if (len >= length) len = length - 1;
strncpy(t, src, len);
}
const char* cipher()const {
return c;
}
void cipher(const char* src) {
memset(c, 0, length);
auto len = strlen(src);
if (len >= length) len = length - 1;
strncpy(c, src, len);
}
void show()const {
cout
<< setw(length) << left << c
<< setw(length) << left << t
<< q << '\n';
}
void clear() {
q = 0;
memset(t, 0, length);
memset(c, 0, length);
}
private:
static const auto length = 20U;
int q;
char t[length];
char c[length];
};
void flush() {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
Product input_product() {
Product product;
string buffer;
cout << "Cipher: ";
cin >> buffer;
flush();
product.cipher(buffer.c_str());
cout << "Title: ";
getline(cin, buffer);
product.title(buffer.c_str());
cout << "Quantity: ";
int value;
cin >> value;
flush();
product.quantity(value);
puts("");
return product;
}
int main() {
array<Product, 5> products;
for (auto& product : products) product = input_product();
puts("");
for (const auto& product : products) product.show();
puts("");
cout
<< products[2].cipher() << "\t "
<< products[2].title() << "\t "
<< products[2].quantity() << '\n';
}
#include <array>
#include <cstring>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;
class Product {
public:
Product() {
clear();
}
int quantity()const {
return q;
}
void quantity(const int value) {
q = value;
}
const char* title()const {
return t;
}
void title(const char* src) {
memset(t, 0, length);
auto len = strlen(src);
if (len >= length) len = length - 1;
strncpy(t, src, len);
}
const char* cipher()const {
return c;
}
void cipher(const char* src) {
memset(c, 0, length);
auto len = strlen(src);
if (len >= length) len = length - 1;
strncpy(c, src, len);
}
void show()const {
cout
<< setw(length) << left << c
<< setw(length) << left << t
<< q << '\n';
}
void clear() {
q = 0;
memset(t, 0, length);
memset(c, 0, length);
}
private:
static const auto length = 20U;
int q;
char t[length];
char c[length];
};
void flush() {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
Product input_product() {
Product product;
string buffer;
cout << "Cipher: ";
cin >> buffer;
flush();
product.cipher(buffer.c_str());
cout << "Title: ";
getline(cin, buffer);
product.title(buffer.c_str());
cout << "Quantity: ";
int value;
cin >> value;
flush();
product.quantity(value);
puts("");
return product;
}
int main() {
array<Product, 5> products;
for (auto& product : products) product = input_product();
puts("");
for (const auto& product : products) product.show();
puts("");
cout
<< products[2].cipher() << "\t "
<< products[2].title() << "\t "
<< products[2].quantity() << '\n';
}
Это простая но длинная и скучная программа. Вряд ли кто-то захочет ее писать забесплатно.
Похожие вопросы
- Как реализовать программу, в которой массив заполняется рандомно, а сумма его элементов всегда будет равна 1? С++
- С++, Сделайте класс Triangle наследником класса Figure. Напишите программу, в которой будет считываться ..
- Напишите простую программу на с++ Т.з внизу
- Написать простую программу на языке с++
- Помогите пожалуйста. Нужно чтоб эта программа, автоматически создавала txt. файл. На C++. Напишите рабочую программу.
- Можно ли написать крутую программу использую только базовые элементы?
- СРОЧНО Напишите пожалуйста программу на Си
- Как написать такую программу на C (си)?
- Напишите пожалуйста программу на СИ, массив стат или динам
- Написать код программы на с++