Home > Back-end >  C OOP Abstract Class - Access violation writing location
C OOP Abstract Class - Access violation writing location

Time:11-27

I have an UserAcount class that has an abstract class ContBancar, and other class Banca which reads some users from a file (with method void Banca::citire_conturi()). When it reads the users, I get an error "Access violation writing location" in ContBancar at void setBal(double bal) { _balanta = bal; }. Thx for help !

PS : The file has only one line : 1CBS Dragos 0 dragos12! Gzpvia01= . Also, i want to make a bank account system, with an user class that has an bank account class which inherits 3 types of a bank accounts, and a bank class which reads some users from a file or put them on it.

 class UserAccount
{
private:
    std::string _nume, _user, _pass;
    std::string _cod_us;
    std::shared_ptr <ContBancar> _cont; 

public:
    void setUser(std::string user) { _user = user; }
    void setPass(std::string pass) { _pass = pass; }
    void setNume(std::string nume) { _nume = nume; }
    void setCodUs(std::string cod) { _cod_us = cod; }
    void setContBal(double balanta) { (*_cont).setBal(balanta); }
    std::string getUser() const { return _user; }
    std::string getPass() const { return _pass; }
    std::string getNume() const { return _nume; }
    std::string getCodUs() const { return _cod_us; }
    double getContBal() const { return (*_cont).getBal(); }
    void setContBancar();
};

void UserAccount::setContBancar()
{
    if (_cod_us == "1CBS")
        _cont.reset(new ContBancarSilver());
    else if (_cod_us == "2CBG")
        _cont.reset(new ContBancarGold());
    else
        _cont.reset(new ContBancarDiamond());
}


class ContBancar
{
protected:
    double _balanta;

public:
    void setBal(double bal) { _balanta = bal; }
    double getBal() { return _balanta; }
    virtual bool depozitare(unsigned int) = 0;
    virtual bool retragere(unsigned int) = 0;
};


class Banca
{
private:
    std::vector<UserAccount> vec;
public:
    void citire_conturi();
};

void Banca::citire_conturi()
{
    std::ifstream file;
    file.open("Baza_Date.txt");

    UserAccount temp;
    std::string cod, nume, user, pass;
    double balanta;

    while (file >> cod >> nume >> balanta >> user >> pass)
    {
        temp.setCodUs(cod);
        temp.setNume(nume);
        temp.setContBal(balanta);
        temp.setUser(user);
        temp.setPass(pass);
        vec.push_back(temp);
    }

    file.close();
}


class ContBancarSilver : public ContBancar
{
private:
    static constexpr unsigned int max_balanta = 5000;
    static constexpr unsigned int max_depozitare = 2500;
    static constexpr unsigned int max_retragere = 1000;
    static constexpr double tax_retragere = 0.08;
    static constexpr double bonus_depunere = 0.03;
    static constexpr double bonus_tax_retragere = 0.05;
    static constexpr unsigned int max_depozitari = 1;
    static constexpr unsigned int max_retrageri = 1;

public:
    virtual bool depozitare(unsigned int) override;
    virtual bool retragere(unsigned int) override;
};

CodePudding user response:

Based on available informationyou should fix your code like this:

class UserAccount
{
    .....
    void setCodUs(std::string cod) {
        _cod_us = cod;
        setContBancar();
    }
    void setContBal(double balanta) {
        if (!_cont) setContBancar(); // lazy initialization
        _cont->setBal(balanta);
    }
    ...
};

void UserAccount::setContBancar()
{
    if (_cod_us == "1CBS")
        _cont = std::make_shared<ContBancarSilver>();
    else if (_cod_us == "2CBG")
        _cont = std::make_shared<ContBancarGold>();
    else
        _cont = std::make_shared<ContBancarDiamond>();
}

Note I do not understand what kind of logic you are implementing. This changes just ensured that _cont is initialized and up to date with _cod_us.

Please stop use explicitly new and delete. Everything can be created by std::make_shared and std::make_unique and containers like std::vector.

  • Related