Newbie Site fault issue C++ -


i'm working on project create bank accounts , able deposit , withdraw. create 2 bank account , 2 people- 1 one stack , other on heap. should deposit , withdraw each twice , balance print name , id , account numbers. @ moment i'm believe site fault , reading or writing protected memory. i've left comments on think errors lie. appreciate help. thanks.

#include <iostream> #include <string> using namespace std;  class bankaccount { private:     double *balance;     int *accountnumber; public:     bankaccount(){//default constructor         *balance = 0.0;/***this says access violation lies*/         *accountnumber = 0;     }      bankaccount(double bal, int acctnum){//constructor         balance = new double(bal);         accountnumber = new int(acctnum);     }     ~bankaccount() {delete balance; delete accountnumber;}     void deposit(double amt) {         *balance = *balance + amt;     }      virtual double getbalance() {         return *balance;     }      virtual double getaccountnumber() {         return *accountnumber;     }      virtual double withdraw(double amt) {         *balance = *balance - amt;         return *balance;     } };  class person {     string *name;     int *id;  public:     person(){//default constructor         *name = "name not yet defined";         *id = 0;     }      person(string namein, int idin){//constructor         name = new string(namein);         id = new int(idin);     }      virtual int getid() {     return *id;     }      virtual string getname() {     return *name;     } };  class namedbankaccount: public bankaccount { private:     person *owner; public:      namedbankaccount(){     }     namedbankaccount(person *p): owner(p){/***this says access violation lies*/         p = new person();     }     ~namedbankaccount(){delete owner;}      person getperson() {         return *owner;     } };  int main() {      person *q = new person("joe", 54321);     cout << q->getname() << endl;     cout << q->getid() << endl;       namedbankaccount nba1;/***this says access violation lies*/     namedbankaccount *nba2 = new namedbankaccount(q);      nba1.deposit(50);     nba1.deposit(50);     nba1.withdraw(25);     cout << nba1.getbalance() <<endl;//should print 75      nba2->deposit(60);     nba2->deposit(60);     nba2->withdraw(20);     cout << nba2->getbalance() << endl;//should print 100      getchar();     return 0; } 

do not use pointers here. have strings , integers member variables. specific problem - haven't allocated memory before assignment in default constructor.

do like:

class bankaccount { private:     double balance;     int accountnumber; public:     bankaccount() :         balance( 0.0 ),         accountnumber( 0 ) {}      // ... 

edit:

couple of more points code:

  • make use of initialization list in constructors instead of assignment member variables - avoids two-step process of first default-initializing members , assigning them
  • base polymorphic classes should have virtual destructors, instances of derived classes destroyed via pointer base
  • polymorphic types need follow the rule of three avoid slicing
  • do not make member functions of base class virtual, want derived classes override
  • think before making type polymorphic - have bank accounts without owners? maybe can value type?
  • make accessor methods const, can information const instances
  • check errors (you sure don't want allow withdrawals 0 or negative balance accounts)

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -