Advertisement
nikminer4sv

Untitled

Nov 29th, 2022
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class InvalidArgumentException : public std::exception {
  6. private:
  7.     char* message;
  8.  
  9. public:
  10.     InvalidArgumentException(char* msg) : message(msg) {}
  11.     char* what () {
  12.         return message;
  13.     }
  14. };
  15. class Tovar {
  16. private:
  17.     string name;
  18.     int cost, n;
  19. public:
  20.     Tovar() {
  21.         name = "Plates";
  22.         cost = 200;
  23.         n = 4;
  24.     };
  25.     ~Tovar() {
  26.     };
  27.     Tovar(string name, int cost, int n) {
  28.         setname(name);
  29.         setcost(cost);
  30.         setn(n);
  31.     };
  32.     double quality() {
  33.         return cost/n;
  34.     };
  35.     void setname(string sname) {
  36.         if (sname.length() == 0)
  37.             throw InvalidArgumentException("Name can't be empty");
  38.         name = sname;
  39.     };
  40.     void setcost(int scost) {
  41.         if (scost <= 0)
  42.             throw InvalidArgumentException("Cost must be more than 0");
  43.         cost = scost;
  44.     };
  45.     void setn(int sn) {
  46.         if (sn <= 0)
  47.             throw InvalidArgumentException("n must be more than 0");
  48.         n = sn;
  49.     };
  50.     string getname() const {
  51.         return name;
  52.     };
  53.     int getcost() const {
  54.         return cost;
  55.     };
  56.     int getn() const {
  57.         return n;
  58.     };
  59.     string getInfo() {
  60.         string res;
  61.         res = "Наименование товара:" + name + "; " + "Цена:" + to_string(cost) + "; " + "Количество:" + to_string(n) + "; " + "Значение Q:" + to_string(quality());
  62.         return res;
  63.     };
  64. };
  65.  
  66. class Year : Tovar {
  67. private:
  68.     int P, T = 2022;
  69. public:
  70.     Year() :Tovar() {
  71.         P = 2018;
  72.     };
  73.     Year(string name, int cost, int n, int P) : Tovar(name, cost, n) {
  74.         setP(P);
  75.     };
  76.     ~Year() {
  77.  
  78.     };
  79.     void setP(int sP) {
  80.         if (sP <= 0)
  81.             throw InvalidArgumentException("P must be more than 0");
  82.         P = sP;
  83.     };
  84.     int getP() {
  85.         return P;
  86.     };
  87.     double quality1() {
  88.         return quality()+0.5*(T-P);
  89.     };
  90.     string getInfo() {
  91.         string res;
  92.         res = "Наименование товара:" + getname() + "; " + "Цена:" + to_string(getcost()) + "; " + "Количество:" + to_string(getn()) + "; " + "Значение Q:" + to_string(quality1());
  93.         return res;
  94.     };
  95. };
  96.  
  97. int main() {
  98.     setlocale(LC_ALL, "RU");
  99.     try {
  100.         Tovar* obj1;
  101.         obj1 = new Tovar("Folks", 100, 2);
  102.         cout << obj1->getInfo() << "\n";
  103.         Year* obj2;
  104.         obj2 = new Year();
  105.         obj2->setP(2017);
  106.         cout << obj2->getInfo() << "\n";
  107.     } catch (InvalidArgumentException e) {
  108.         cout << e.what() << endl;
  109.     }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement