Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class InvalidArgumentException : public std::exception {
- private:
- char* message;
- public:
- InvalidArgumentException(char* msg) : message(msg) {}
- char* what () {
- return message;
- }
- };
- class Tovar {
- private:
- string name;
- int cost, n;
- public:
- Tovar() {
- name = "Plates";
- cost = 200;
- n = 4;
- };
- ~Tovar() {
- };
- Tovar(string name, int cost, int n) {
- setname(name);
- setcost(cost);
- setn(n);
- };
- double quality() {
- return cost/n;
- };
- void setname(string sname) {
- if (sname.length() == 0)
- throw InvalidArgumentException("Name can't be empty");
- name = sname;
- };
- void setcost(int scost) {
- if (scost <= 0)
- throw InvalidArgumentException("Cost must be more than 0");
- cost = scost;
- };
- void setn(int sn) {
- if (sn <= 0)
- throw InvalidArgumentException("n must be more than 0");
- n = sn;
- };
- string getname() const {
- return name;
- };
- int getcost() const {
- return cost;
- };
- int getn() const {
- return n;
- };
- string getInfo() {
- string res;
- res = "Наименование товара:" + name + "; " + "Цена:" + to_string(cost) + "; " + "Количество:" + to_string(n) + "; " + "Значение Q:" + to_string(quality());
- return res;
- };
- };
- class Year : Tovar {
- private:
- int P, T = 2022;
- public:
- Year() :Tovar() {
- P = 2018;
- };
- Year(string name, int cost, int n, int P) : Tovar(name, cost, n) {
- setP(P);
- };
- ~Year() {
- };
- void setP(int sP) {
- if (sP <= 0)
- throw InvalidArgumentException("P must be more than 0");
- P = sP;
- };
- int getP() {
- return P;
- };
- double quality1() {
- return quality()+0.5*(T-P);
- };
- string getInfo() {
- string res;
- res = "Наименование товара:" + getname() + "; " + "Цена:" + to_string(getcost()) + "; " + "Количество:" + to_string(getn()) + "; " + "Значение Q:" + to_string(quality1());
- return res;
- };
- };
- int main() {
- setlocale(LC_ALL, "RU");
- try {
- Tovar* obj1;
- obj1 = new Tovar("Folks", 100, 2);
- cout << obj1->getInfo() << "\n";
- Year* obj2;
- obj2 = new Year();
- obj2->setP(2017);
- cout << obj2->getInfo() << "\n";
- } catch (InvalidArgumentException e) {
- cout << e.what() << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement