E
E
Egor Nameles2019-04-22 14:51:28
OOP
Egor Nameles, 2019-04-22 14:51:28

How can I implement sorting and searching by “risk” in my code?

I am writing a small program with OOP and inheritance. There is a base class and five classes are inherited from it - types of insurance. Through the menu you need to display 3 items. 1 - View types of insurance, 2 - Sort by decreasing risk, 3 - Find by risk. It is not possible to sort by risk and search. Can you please tell me how to implement these items?

File Derivative.h
#pragma once
#include <iostream>
#include <conio.h>
#include <string>

class Derivativ
{
public:
  Derivativ();
  Derivativ(const char* n, int t, const char* o, const char* v, int s);
  Derivativ(const Derivativ& t);
  const char* getname() const;
  int getterm()const;
  const char* getoblig() const;
  const char* getvariety() const;
  int getstep()const;
  virtual void Show();
  virtual double Summa();

  virtual ~Derivativ();

protected:
  char* name;
  int term;
  char* oblig;
  char* variety;
  int step;
};


class Live : public Derivativ
{
public:
  Live();
  Live(const char* n, int t, const char* o, const char* v, int s);
  Live(const Derivativ& t);
  void Show() override;
  double Summa() override;

  virtual ~Live();

};

class Medicine : public Derivativ
{
public:
  Medicine();
  Medicine(const char* n, int t, const char* o, const char* v, int s);
  Medicine(const Derivativ& t);
  void Show() override;
  double Summa() override;

  virtual ~Medicine();

};

class Ncase : public Derivativ
{
public:
  Ncase();
  Ncase(const char* n, int t, const char* o, const char* v, int s);
  Ncase(const Derivativ& t);
  void Show() override;
  double Summa() override;

  virtual ~Ncase();
};

class Property : public Derivativ
{
public:
  Property();
  Property(const char* n, int t, const char* o, const char* v, int s);
  Property(const Derivativ& t);
  void Show() override;
  double Summa() override;

  virtual ~Property();
};

class CivilLiab : public Derivativ
{
public:
  CivilLiab();
  CivilLiab(const char* n, int t, const char* o, const char* v, int s);
  CivilLiab(const Derivativ& t);
  void Show() override;
  double Summa() override;

  virtual ~CivilLiab();
};

File Derivative.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Derivativ.h"
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

Derivativ::Derivativ()
{
  name = 0;
  term = 0;
  oblig = 0;
  variety = 0;
  step = 0;
}

Derivativ::Derivativ(const char* n, int t, const char* o, const char* v, int s)
  : name(new char[strlen(n) + 1]), term(t), oblig(new char[strlen(o) + 1]), variety(new char[strlen(v) + 1]), step(s)
{
  strcpy(name, n);
  strcpy(oblig, o);
  strcpy(variety, v);
}

Derivativ::Derivativ(const Derivativ& t)
  : name(new char[strlen(t.name) + 1]), term(t.term), oblig(new char[strlen(t.oblig) + 1]), variety(new char[strlen(t.variety) + 1]), step(t.step)
{
  strcpy(name, t.name);
  strcpy(oblig, t.oblig);
  strcpy(variety, t.variety);
}

void Derivativ::Show()
{ }

double Derivativ::Summa()
{
  return 0;
}

Derivativ::~Derivativ()
{
  delete[] name;
  delete[] oblig;
  delete[] variety;
}

const char* Derivativ::getname() const
{
  return name;
}

int Derivativ::getterm() const
{
  return term;
}

const char* Derivativ::getoblig() const
{
  return oblig;
}

const char* Derivativ::getvariety() const
{
  return variety;
}

int Derivativ::getstep() const
{
  return step;
}



Live::Live() :Derivativ()
{ }

Live::Live(const char* n, int t, const char* o, const char* v, int s) : Derivativ(n, t, o, v, s)
{ }

Live::Live(const Derivativ& t) : Derivativ(t)
{ }

void Live::Show()
{
  cout << "Страхование жизни";
}

double Live::Summa()
{
  return 10000 * term + 518;
}

Live::~Live()
{ }



Medicine::Medicine() :Derivativ()
{ }

Medicine::Medicine(const char* n, int t, const char* o, const char* v, int s) : Derivativ(n, t, o, v, s)
{ }

Medicine::Medicine(const Derivativ& t) : Derivativ(t)
{ }

void Medicine::Show()
{
  cout << "Медицинское страхование";
}

double Medicine::Summa()
{
  return 5000 * term + 482;
}

Medicine::~Medicine()
{ }



Ncase::Ncase() :Derivativ()
{ }

Ncase::Ncase(const char* n, int t, const char* o, const char* v, int s) : Derivativ(n, t, o, v, s)
{ }

Ncase::Ncase(const Derivativ& t) : Derivativ(t)
{ }

void Ncase::Show()
{
  cout << "Страхование от несчастных случаев";
}

double Ncase::Summa()
{
  return 6500 * term + 491;
}

Ncase::~Ncase()
{ }



Property::Property() :Derivativ()
{ }

Property::Property(const char* n, int t, const char* o, const char* v, int s) : Derivativ(n, t, o, v, s)
{ }

Property::Property(const Derivativ& t) : Derivativ(t)
{ }

void Property::Show()
{
  cout << "Страхование имущества";
}

double Property::Summa()
{
  return 7400 * term + 504;
}

Property::~Property()
{ }



CivilLiab::CivilLiab() :Derivativ()
{ }

CivilLiab::CivilLiab(const char* n, int t, const char* o, const char* v, int s) : Derivativ(n, t, o, v, s)
{ }

CivilLiab::CivilLiab(const Derivativ& t) : Derivativ(t)
{ }

void CivilLiab::Show()
{
  cout << "Страхование гражданской ответственности";
}

double CivilLiab::Summa()
{
  return 6900 * term + 498;
}

CivilLiab ::~CivilLiab()
{ }

main.cpp file
#include <iostream>
    #include <conio.h>
    #include "Derivativ.h"
    #include <iomanip>
    using namespace std;
    
    void Menu() {
        cout << "1 - Посмотреть виды страховок" << endl;
        cout << "2 - Отсортировать по уменьшению степени риска" << endl;
        cout << "3 - Найти по степени риска" << endl;
        cout << "0 - Выход" << endl;
    }
    
    int main()
    {
        setlocale(LC_ALL, "Russian");
    
        int c;
    
        do {
            Menu();
            cout << "Ваш выбор: ";
            cin >> c;
            switch (c) {
            case 1:
                system("cls");
                
                Derivativ *ex[5];
                ex[0] = new Live("Страхование жизни", 3, "Добровальное", "Личное", 5);
                ex[1] = new Medicine("Медицинское страхование", 5, "Добровальное", "Личное", 3);
                ex[2] = new Ncase("Страхование от несчастных случаев", 5, "Добровальное", "Личное", 4);
                ex[3] = new Property("Страхование имущества", 1, "Добровальное", "Имущественное", 2);
                ex[4] = new CivilLiab("Страхование гражданской ответственности", 1, "Добровальное", "Имущественное", 1);
    
                for (int i = 0; i < 5; i++)
                {
                    cout << "Номер: " << (i + 1) << endl;
                    //cout << "Тип страховки: ";
                    //ex[i]->Show();
                    cout << endl;
                    cout << "Название: " << ex[i]->getname() << endl;
                    cout << "Срок действия (в годах): " << ex[i]->getterm() << endl;
                    cout << "Добровалиное/обязательное:" << ex[i]->getoblig() << endl;
                    cout << "Разновидность: " << ex[i]->getvariety() << endl;
                    cout << "Степень риска: " << ex[i]->getstep() << endl;
                    cout << "Стоимость страховки: " << ex[i]->Summa() << endl;
                    cout << endl << endl;
    
                }
    
                for (int i = 0; i < 5; ++i)
                    delete ex[i];
    
                break;
            case 2:
                system("cls");
                
                break;
            case 3:
                system("cls");
                
                break;
            case 0:
                break;
            default: cout << "Некорректный вариант выбора!" << endl;
            }
        } while (c != 0);
    
    
        _getch();
        return 0;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2019-04-22
@tsarevfs

1. Sorting function from the standard library: https://en.cppreference.com/w/cpp/algorithm/sort. See the lambda function example. In your case, its parameters will be pointers to Derivativ, and in the body there will be a comparison by the corresponding property.
2. An ordinary array does not have begin() and end(). Use a vector instead of an array or https://en.cppreference.com/w/cpp/iterator/begin
3. An array with insurances is created for you only if 1 is selected. It's a little more logical if it lives for the entire program runtime.
4. Sorting changes the array. Probably "2" should create a copy of the array, sort it and print it.
5. Use std::string to store strings. Life will become a little easier and more fun.
5. DerivativesIt is something derived from something. And you produce the opposite from him. Insurance or BaseInsurance is a more appropriate name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question