C/C++ C++ kodunda arrow pointer call notation yapılmıyor

SubhanXd

Centipat
Katılım
5 Kasım 2022
Mesajlar
161
Selamlar. İki farklı class kullanarak simple bank account system yapmaya calışıyorum:
Create a program that simulates a simple bank account system.
The program should have two classes: BankAccount and Bank.
1. BankAccount class should have the following private members:
int accountNumber: An integer representing the account number.
double balance: A double representing the current balance.
It should also have the following public member functions:
BankAccount(int accNumber): A constructor that takes an account number as a parameter and initializes the balance to 0.
void deposit(double amount): A function that takes an amount and adds it to the account's balance.
void withdraw(double amount): A function that takes an amount and subtracts it from the account's balance.
void displayBalance(): A function that displays the current balance of the account.
2. Bank class should have the following private members:
BankAccount* accounts: A pointer to an array of BankAccount objects.
int numAccounts: An integer representing the number of accounts in the bank.
It should also have the following public member functions:
Bank(int num): A constructor that takes the number of accounts as a parameter and dynamically allocates memory for the accounts.
~Bank(): A destructor that frees the dynamically allocated memory for the accounts.
void performTransactions(): A function that performs some transactions (e.g., deposits and withdrawals) on the bank accounts.
void displayAllBalances(): A function that displays the balances of all the bank accounts.
Instructions:
1. Implement the member functions of the BankAccount class.
2. Implement the constructor and destructor of the Bank class.
3. Implement the performTransactions function in the Bank class,
which simulates some transactions(ex: accounts[0]->deposit(1000)
accounts[1]->deposit(500)
accounts[2]->deposit(200)
accounts[2]->withdraw(50)) on the bank accounts.
4. In the main function, create an instance of the Bank class with a specific number of accounts.
5. Call the performTransactions function on the Bank object to simulate transactions.
6. Finally, call the displayAllBalances function to display the balances of all bank accounts.
7. Make sure to deallocate any dynamically allocated memory in the appropriate locations.
Benim yazdığım kod:
C++:
#include <iostream>
#include <string>

using namespace std;

class BankAccount {
    private:
    int accountNumber;
    double balance;

    public:

    BankAccount() = default ;

    BankAccount(const int number) {
        accountNumber = number ;
        balance = 0.0 ;
    }

    void deposit(double amount) {
        balance += amount ;
    }

    void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount ;
        } else {
            cout << "Insufficient amount.Enter the different amount" << endl ;
        }
    }

    void displayBalance() const {
         cout << "Your balance is: " << balance << " manat" << endl ;
    }
};

class Bank {
  private:
  BankAccount* accounts;
  int numAccounts ;

  public:

  Bank(int num) {
        numAccounts = num;
        accounts = new BankAccount[numAccounts];
    }

    ~Bank() {
        delete[] accounts;
    }

    void performTransactions() {
        accounts[0]->deposit(1000);
        accounts[1]->deposit(500);
        accounts[2]->deposit(200);
        accounts[2]->withdraw(50);
    }

    void displayAllBalances() const {
        for (int i = 0; i < numAccounts; ++i) {
            accounts[i]->displayBalance();
        }
    }
};

int main() {

    /*      Example usage:     */
    Bank bank(3);

    bank.performTransactions();
    bank.displayAllBalances();

    return 0;
}

Kodda hatalı olan ve takıldığım kısım şurası:
void performTransactions() {
accounts[0]->deposit(1000);
accounts[1]->deposit(500);
accounts[2]->deposit(200);
accounts[2]->withdraw(50);
}
Bu sorun sanırım bu kısımda hata yaptığımdan dolayı oluşuyor:
Bank(int num) {
numAccounts = num;
accounts = new BankAccount[numAccounts];
}
Bu kısmı "performTransactions" fonskiyonunun doğru çalışsması için nasıl yazmam gerek?
 
Son düzenleyen: Moderatör:
Selamlar. İki farklı class kullanarak simple bank account system yapmaya calışıyorum:
Create a program that simulates a simple bank account system.
The program should have two classes: BankAccount and Bank.
1. BankAccount class should have the following private members:
int accountNumber: An integer representing the account number.
double balance: A double representing the current balance.
It should also have the following public member functions:
BankAccount(int accNumber): A constructor that takes an account number as a parameter and initializes the balance to 0.
void deposit(double amount): A function that takes an amount and adds it to the account's balance.
void withdraw(double amount): A function that takes an amount and subtracts it from the account's balance.
void displayBalance(): A function that displays the current balance of the account.
2. Bank class should have the following private members:
BankAccount* accounts: A pointer to an array of BankAccount objects.
int numAccounts: An integer representing the number of accounts in the bank.
It should also have the following public member functions:
Bank(int num): A constructor that takes the number of accounts as a parameter and dynamically allocates memory for the accounts.
~Bank(): A destructor that frees the dynamically allocated memory for the accounts.
void performTransactions(): A function that performs some transactions (e.g., deposits and withdrawals) on the bank accounts.
void displayAllBalances(): A function that displays the balances of all the bank accounts.
Instructions:
1. Implement the member functions of the BankAccount class.
2. Implement the constructor and destructor of the Bank class.
3. Implement the performTransactions function in the Bank class,
which simulates some transactions(ex: accounts[0]->deposit(1000)
accounts[1]->deposit(500)
accounts[2]->deposit(200)
accounts[2]->withdraw(50)) on the bank accounts.
4. In the main function, create an instance of the Bank class with a specific number of accounts.
5. Call the performTransactions function on the Bank object to simulate transactions.
6. Finally, call the displayAllBalances function to display the balances of all bank accounts.
7. Make sure to deallocate any dynamically allocated memory in the appropriate locations.
Benim yazdığım kod:
C++:
#include <iostream>
#include <string>

using namespace std;

class BankAccount {
    private:
    int accountNumber;
    double balance;

    public:

    BankAccount() = default ;

    BankAccount(const int number) {
        accountNumber = number ;
        balance = 0.0 ;
    }

    void deposit(double amount) {
        balance += amount ;
    }

    void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount ;
        } else {
            cout << "Insufficient amount.Enter the different amount" << endl ;
        }
    }

    void displayBalance() const {
         cout << "Your balance is: " << balance << " manat" << endl ;
    }
};

class Bank {
  private:
  BankAccount* accounts;
  int numAccounts ;

  public:

  Bank(int num) {
        numAccounts = num;
        accounts = new BankAccount[numAccounts];
    }

    ~Bank() {
        delete[] accounts;
    }

    void performTransactions() {
        accounts[0]->deposit(1000);
        accounts[1]->deposit(500);
        accounts[2]->deposit(200);
        accounts[2]->withdraw(50);
    }

    void displayAllBalances() const {
        for (int i = 0; i < numAccounts; ++i) {
            accounts[i]->displayBalance();
        }
    }
};

int main() {

    /*      Example usage:     */
    Bank bank(3);

    bank.performTransactions();
    bank.displayAllBalances();

    return 0;
}

Kodda hatalı olan ve takıldığım kısım şurası:
void performTransactions() {
accounts[0]->deposit(1000);
accounts[1]->deposit(500);
accounts[2]->deposit(200);
accounts[2]->withdraw(50);
}
Bu sorun sanırım bu kısımda hata yaptığımdan dolayı oluşuyor:
Bank(int num) {
numAccounts = num;
accounts = new BankAccount[numAccounts];
}
Bu kısmı "performTransactions" fonskiyonunun doğru çalışsması için nasıl yazmam gerek?

Kod:
 void performTransactions() {
        accounts[0].deposit(1000);
        accounts[1].deposit(500);
        accounts[2].deposit(200);
        accounts[2].withdraw(50);
    }
bu şekil denediniz mi ? Denemedim ama böyle yaparsanız çalışır gibime geldi.

bu şekil mi çalışması gerekiyor kodunun ?
 

Dosya Ekleri

  • Ekran görüntüsü 2024-02-29 170553.png
    Ekran görüntüsü 2024-02-29 170553.png
    4,1 KB · Görüntüleme: 15
Kod:
 void performTransactions() {
        accounts[0].deposit(1000);
        accounts[1].deposit(500);
        accounts[2].deposit(200);
        accounts[2].withdraw(50);
    }
bu şekil denediniz mi ? Denemedim ama böyle yaparsanız çalışır gibime geldi.

bu şekil mi çalışması gerekiyor kodunun ?
Çalışıyor ama exercisede benim dediğim gibi yapılması isteniyor.
 
Çalışıyor ama exercisede benim dediğim gibi yapılması isteniyor.
Tamam hatayı çözdüm hocam.
C++:
#include <iostream>
#include <string>


using namespace std;


class BankAccount {
private:
    int accountNumber;
    double balance;


public:
    BankAccount() = default;


    BankAccount(const int number) {
        accountNumber = number;
        balance = 0.0;
    }


    void deposit(double amount) {
        balance += amount;
    }


    void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
        } else {
            cout << "Insufficient amount. Enter a different amount" << endl;
        }
    }


    void displayBalance() const {
        cout << "Your balance is: " << balance << " manat" << endl;
    }
};


class Bank {
private:
    BankAccount** accounts;
    int numAccounts;


public:
    Bank(int num) {
        numAccounts = num;
        accounts = new BankAccount*[numAccounts];
        for (int i = 0; i < numAccounts; ++i) {
            accounts[i] = new BankAccount;
        }
    }


    ~Bank() {
        for (int i = 0; i < numAccounts; ++i) {
            delete accounts[i];
        }
        delete[] accounts;
    }


    void performTransactions() {
        accounts[0]->deposit(1000);
        accounts[1]->deposit(500);
        accounts[2]->deposit(200);
        accounts[2]->withdraw(50);
    }


    void displayAllBalances() const {
        for (int i = 0; i < numAccounts; ++i) {
            accounts[i]->displayBalance();
        }
    }
};


int main() {
    Bank bank(3);


    bank.performTransactions();
    bank.displayAllBalances();


    return 0;
}
bu şekilde kabul ederler mi ?
 
Tamam hatayı çözdüm hocam.
C++:
#include <iostream>
#include <string>


using namespace std;


class BankAccount {
private:
    int accountNumber;
    double balance;


public:
    BankAccount() = default;


    BankAccount(const int number) {
        accountNumber = number;
        balance = 0.0;
    }


    void deposit(double amount) {
        balance += amount;
    }


    void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
        } else {
            cout << "Insufficient amount. Enter a different amount" << endl;
        }
    }


    void displayBalance() const {
        cout << "Your balance is: " << balance << " manat" << endl;
    }
};


class Bank {
private:
    BankAccount** accounts;
    int numAccounts;


public:
    Bank(int num) {
        numAccounts = num;
        accounts = new BankAccount*[numAccounts];
        for (int i = 0; i < numAccounts; ++i) {
            accounts[i] = new BankAccount;
        }
    }


    ~Bank() {
        for (int i = 0; i < numAccounts; ++i) {
            delete accounts[i];
        }
        delete[] accounts;
    }


    void performTransactions() {
        accounts[0]->deposit(1000);
        accounts[1]->deposit(500);
        accounts[2]->deposit(200);
        accounts[2]->withdraw(50);
    }


    void displayAllBalances() const {
        for (int i = 0; i < numAccounts; ++i) {
            accounts[i]->displayBalance();
        }
    }
};


int main() {
    Bank bank(3);


    bank.performTransactions();
    bank.displayAllBalances();


    return 0;
}
bu şekilde kabul ederler mi ?
Yani kabul etmeleri şart değil hocam sadece ödev gibi yapıyorum bunu.Sizin yazdığınız kodda double pointer kullanılmış ama accounts normal pointer olmalı.
 

Geri
Yukarı