In: Computer Science
Using C++. Please number the answers for clarity
1. Create an Account object named as myAccount and initialize it with appropriate data.
2. Change myAccount’s interest rate to 1%
3. display the balance on myAccount.
4. Declare a pointer variable ptr and initialize it with the address of myAccount. Display the interest earned on myAccount by using pointer ptr.
5. Dynamically allocate an array of five Account objects.
6. Suppose getAcctNum() returns the account number of a given Account variable. Write a function that returns a boolean value to find if a given account number appears in an array of Account objects.
Hi,
Please find code below.Output attached.
#include <iostream>
using namespace std;
class Account
{
private:
int accountNum; //Account Number
double balance; //depicts Current Balance of the
account
double interestRate; // depicts interest rate of the
account.
public:
// access specifier is public for below methods.
Account() //Default Constructor (no parameters)
{
accountNum = 0;
balance = 0;
interestRate = 0;
}
Account(int accnum, double bal, double interest)//
parametrised constructor
{
accountNum = accnum;
balance = bal;
interestRate = interest;
}
//setters
void setAccountNum(int x)
{
accountNum = x;
}
void setBalance(double x)
{
balance = x;
}
void setInterestRate(double x)
{
interestRate = x;
}
//Getters
int getAccountNum()
{
return accountNum;
}
double getBalance()
{
return balance;
}
double getInterestRate() // considering this as annual
interest rate.
{
return interestRate;
}
bool withdraw(double withdrawAmount)
{
if (balance >= withdrawAmount)
{
balance -= withdrawAmount;
return true;
}
else
{
return false;
}
}
void deposit(double depositAmount)
{
balance += depositAmount;
}
double getMonthlyInterestEarned()
{
double monthlyInterestRate = interestRate / 12;
double interestEarned = monthlyInterestRate *
balance;
return interestEarned;
}
};
int main()
{
// Step 1: Create an Account object named as myAccount and
initialize it with appropriate data.
Account myAccount(111,100,8);
//Step 2:Change myAccount’s interest rate to 1%
myAccount.setInterestRate(2);
myAccount.deposit(100);
myAccount.withdraw(50);
//step 3. display the balance on myAccount.
cout << "Balance on myAccount is:" <<
myAccount.getBalance() << endl;
//4. Declare a pointer variable ptr and initialize it with the
address of myAccount.
//Display the interest earned on myAccount by using pointer
ptr.
Account *acctPtr = &myAccount;
double intEarned = acctPtr->getMonthlyInterestEarned();
cout << "Monthly interest earned is:" << intEarned
<< endl;
//5. Dynamically allocate an array of five Account objects.
Account **myVar;
myVar = new Account*[5];
myVar[0] = new Account(222,200,9);
myVar[1] = new Account(333,300,7);
myVar[2] = new Account(444,400,6);
myVar[3] = new Account(555,500,5);
myVar[4] = new Account(666,600,4);
//step 6:Suppose getAcctNum() returns the account
number of a given Account variable. Write a function
that //returns a boolean value to find if a given account number
appears in an array of Account objects.
int accountNumber;
bool found = false;
cout << "Enter the account number to be searched in array of
objects" << endl;
cin >> accountNumber;
for(int i=0;i<5;i++)
{
if(myVar[i]->getAccountNum() == accountNumber)
{
found = true;
}
if(found)
break;
}
if(found)
cout << " Given account number :" << accountNumber
<< " found and is available in array of objects" <<
endl;
else
cout << "Sorry account number not found" << endl;
}
Output :
Thanks,
kindly upvote.