Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Destructor help in Derived Class of Account Class - CDAccount My original code for my Destructor functions in my Account.cpp and CDAccount.cpp files were

C++ Destructor help in Derived Class of Account Class - CDAccount

My original code for my Destructor functions in my Account.cpp and CDAccount.cpp files were printing out correctly so I continued to work on the rest of my code and now they are not printing correctly anymore, even though I made no changes to them. I don't understand what I did wrong. I have posted my code below and any help is greatly appreciated, thank you.

Sample output:

image text in transcribed

my output:

image text in transcribed

My code :

ACCOUNT.H

#ifndef Account_h

#define Account_h

#include

using namespace std;

class Account

{

const int id;

double balance;

double static annualInterestRate;

static int NumberOfObjects;

double setBalance(double);

friend ostream &operator

public:

Account(const int = 0, double = 0.0); // Constructor

~Account(); // Destructor

// set function

void static setAnnualInterestRate(double); // set the initial value as 5%

// get functions

unsigned int getID() const;

double getBalance() const;

static double getAnnualInterestRate();

static int getNumberofObjects();

double getMonthlyInterestRate() const;

double getMonthlyInterest() const;

void withdraw(double);

void deposit(double);

};

#endif // Account_h

----------------------------------------

CDACCOUNT.H

#ifndef CDAccount_h

#define CDAccount_h

#include "Account.h"

using namespace std;

class CDAccount : public Account

{

int duration; // 3-month to 15-months

double CDInterestRate;

static int NumberOfObjects;

static int getNumberofObjects();

public:

CDAccount(int = 0, double = 0.0, int = 0); // Constructor

~CDAccount(); // Destructor

void setDuration(int);

int getDuration() const;

void setCDAnnaualInterestRate(double);

double getCDAnnualInterstRate() const;

double getMonthlyInterestRate() const;

double getMonthlyInterest() const;

double getMatureBalance() const;

};

#endif // CDAccount_h

----------------------------------------

ACCOUNT.CPP

#include

#include

#include

#include "Account.h"

using namespace std;

double Account::annualInterestRate = 0.05;

int Account::NumberOfObjects = 0;

Account::Account(const int newID, double newBalance) // constructor initialises each data member

: id(newID), balance(newBalance)

{

cout

NumberOfObjects++;

cout

}

Account::~Account() // destructor (Need help here)

{

cout

--NumberOfObjects;

cout

}

double Account::setBalance(double Newbalance)

{

this->balance = Newbalance;

return 0;

}

void Account::setAnnualInterestRate(double newannualInterestRate)

{

annualInterestRate = newannualInterestRate;

}

unsigned int Account::getID() const

{

return id;

}

double Account::getBalance() const

{

return balance;

}

double Account::getAnnualInterestRate()

{

return annualInterestRate;

}

int Account::getNumberofObjects()

{

return NumberOfObjects;

}

double Account::getMonthlyInterestRate() const

{

return (annualInterestRate / 12);

}

double Account::getMonthlyInterest() const

{

return balance*(annualInterestRate / 12);

}

void Account::withdraw(double withdrawAmount)

{

if (balance >= withdrawAmount)

{

balance -= withdrawAmount; //balance = balance - withdrawAmount

}

else

throw invalid_argument("No sufficient balance");

}

void Account::deposit(double depositAmount)

{

balance += depositAmount; //balance = balance + depositAmount

}

ostream &operator

{

output

return output;

}

----------------------------------------

CDACCOUNT.CPP

include

#include

#include

#include "CDAccount.h"

using namespace std;

int CDAccount::NumberOfObjects = 0;

CDAccount::CDAccount(int newID, double newBalance, int newDuration)// Constructor

:Account (newID,newBalance)

{

setDuration(newDuration);

cout

NumberOfObjects++;

cout

}

CDAccount::~CDAccount() // Destructor (Need help here)

{

cout

--NumberOfObjects;

cout

}

int CDAccount::getNumberofObjects()

{

return NumberOfObjects;

}

void CDAccount::setDuration(int newDuration)

{

duration = newDuration;

}

int CDAccount::getDuration() const

{

return duration;

}

void CDAccount::setCDAnnaualInterestRate(double newCDannualInterestRate)

{

CDInterestRate = newCDannualInterestRate + getAnnualInterestRate(); //

}

double CDAccount::getCDAnnualInterstRate() const

{

return CDInterestRate;

}

double CDAccount::getMonthlyInterestRate() const

{

return (CDInterestRate / 12.0); //Returning monthly interest rate by dividing annual rate by 12

}

double CDAccount::getMonthlyInterest() const

{

double monthlyInterest = getMonthlyInterestRate() / 100; //

return monthlyInterest;

}

double CDAccount::getMatureBalance() const

{

double balance = getBalance();

double maturebalance = balance * pow(1 + (getMonthlyInterestRate() / 100), duration);

return maturebalance;

}

----------------------------------------

ACCOUNTMAIN.CPP

#include

#include

#include

#include "CDAccount.h"

using namespace std;

int main()

{

Account::setAnnualInterestRate(5.00);

CDAccount CDAccount1(1000, 1000.00,3);

CDAccount CDAccount2(2000, 2000.00,6);

CDAccount CDAccount3(3000, 3000.00,9);

CDAccount CDAccount4(4000, 4000.00,12);

CDAccount CDAccount5(5000, 5000.00,15);

CDAccount CDAccount[5] = { CDAccount1,CDAccount2,CDAccount3,CDAccount4,CDAccount5 }; //Create array of 5 CDAccounts

double interesetRateIncrement = 0.5; //Interest rate increment for each 3 months

for (int i = 0; i

{

cout

cout

cout

CDAccount[i].setCDAnnaualInterestRate(interesetRateIncrement);

interesetRateIncrement += 0.5; //Increase interesetRateIncrement by 0.5 for each CD Account

cout

cout

double MonthlyBal = CDAccount[i].getBalance()*(1 + CDAccount[i].getMonthlyInterest()); // Calculating balance after applying monthly interest

for (int j = 0; j

{

cout

MonthlyBal = MonthlyBal*(1 + CDAccount[i].getMonthlyInterest()); //Calculate balance for next month

}

}

cout

return 0;

}

Account ID 180e Was Created Number of Account objects 1 Existing CDAc count ID 1eee Was Created Number of CDAccount objects 1 Existing Account ID = 200e Was Created Number of Account objects 2 Existing CDAccount ID 20ee was created Number of CDAccount objects 2 Existing Account ID 3e0e was created, Number of Account Objects 3 Existing CDAc count ID 3eee Was Created, Number of CDAccount objects 3 Existing Account ID 400e Was Created Number of Account objects 4 Existing CDAccount ID4800 Was Created Number of CDAccount objects 4 Existing Account ID = 500e was created Number of Account Objects 5 Existing CDAccount ID 5000 Was Created Number of CDAccount objects 5 Existing Account ID Annual Rate(%) Duration(months) Start Balance($) End Balance($) 1013.81 1013.81 Account ID Annual Rate(%) Duration(months) Start Balance($) End Balance($) 2020.05 2030.15 2e5e.5e Account ID Annual Rate(%) Duration(months) Start Balance($) End Balance($) 3016.25 3032.59 3049.01 3082.13 3e98.83 3115.62 3132.49 Account ID Annual Rate(%) Duration ( months) Start Balance($) End Balanc@($) 4023.33 4118.84 4142.06 419e.52 4214.97 4239.55 Month 11 Account ID Annual Rate(%) Duration (months) Start Balance($) End Balance($) 5031.25 5862.79 5126.18 5168.22 519e , 45 5222.89 5255.54 6288.38 5354.79 5388 . 16 5421.84 5468.73 5489.82 Month 12 Month 13 Month 14 Month 15 CDAc count ID = 5000 Was Destroyed Number of CDAccount Objects 4 Existing Account ID = 5000 Was Destroyed Number of Account Objects 4 Existing CDAccount ID = 4000 Was Destroyed Number of CDAccount Objects 3 Existing Account ID4080 Was Destroyed Number of Account objects 3 Existing CDAc count ID = 3e9e was Destroyed Number of CDAccount objects 2 Existing Account ID = 3eee was Destroyed Number of Account Objects 2 Existing CDAc count ID = 2eee was Destroyed Number of CDAccount Objects 1 Existing Account ID = 2eee Was Destroyed Number of Account Objects 1 Existing CDAc count ID = 1000 Was Destroyed Number of CDAccount Objects e Existing Account ID = 1000 Was Destroyed Number of Account Objects e Existing

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions