C++ Classes help

Is this right? I feel like im doing some silly mistake but can't figure it out. I keep getting diffrent errors.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef BANK_H
#define BANK_H
#include <string>
#include <iostream>
#include "Address.h"

using namespace std;

class Bank
{
    public:
        Bank();
        virtual ~Bank();
        string getBankName();
        string* getBankAdress();
        int getBankID();
        void setBankName(string);
        void setAddress(string, string);
        void setBankID(int id);
        void printInfo();

    protected:
    private:
       Address  *bankAddress;
       string bankName;
       int bankID;
};

#endif // BANK_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <string>
#include <iostream>
#include "Bank.h"
using namespace std;

Bank::Bank()
{
    bankAddress = new Address();
    bankName = "?";
    bankID = 0;
}

Bank::~Bank()
{
    cout << "The bank" << bankName << "is getting destroyed.\n";
}

void Bank::setBankName(string BankName)
{
    bankName = BankName;

}
string Bank::getBankName()
{
    return bankName;
}
void Bank::setAddress(string city, string state)
{
    bankAddress->setCity(city);
    bankAddress->setState(state);
}
Address * Bank ::getBankAdress()
{
  return bankAddress;
}
void Bank::setBankID(int id)
{
    bankID = id;

}
int Bank ::getBankID()
{
      return bankID;
}
void printInfo()
{

    cout << "\nBank Name:\t\t" << getBankName() << endl;
    cout << "Bank ID:\t\t" << getBankID << endl;
    cout << "Bank Address:\t\t" << getBankAdress << endl;
}

You shall delete the allocated bankAddress in the destructor.

It is a bad idea to name a parameter that differs from a data member by one letter. It is even better to name a parameter the same way as the name of a data member and use this for the data member. So instead of

1
2
3
4
5
void Bank::setBankName(string BankName)
{
    bankName = BankName;

}


I would write

1
2
3
4
void Bank::setBankName( string bankName )
{
    this->bankName = bankName;
}


Or instead of this->bankName you can use Bank::bankName

All getters shall be defined with the qualifier const. For example

1
2
3
4
string Bank::getBankName() const
{
    return bankName;
}


Instead of

1
2
3
4
Address * Bank ::getBankAdress()
{
  return bankAddress;
}


I would define

1
2
3
4
Address  Bank ::getBankAdress()
{
  return *bankAddress;
}
Last edited on
Topic archived. No new replies allowed.