Question

// An Account Class has been created. // Based on the Savings.cpp program on page 42 of Ch09PPT(lab10) using this Account objIm having trouble with this C++ program. Lab 10/object/test files provided

LAB 10

1 //Savings.cpp - displays the account balance at

2 //the end of 1 through 3 years

3 //Created/revised by <your name> on <current date>

4

5 #include <iostream>

6 #include <iomanip>

7 #include <cmath>

8 using namespace std;

9

10 //function prototype

11 double getBalance(int amount, double rate, int y);

12

13 int main()

14 {

15 int deposit = 0;

16 double interestRate = 0.0;

17 double acctBalance = 0.0;

18

19 cout << "Deposit: ";

20 cin >> deposit;

21 cout << "Rate (in decimal form): ";

22 cin >> interestRate;

23

24 cout << fixed << setprecision(2);

25 for (int year = 1; year < 4; year += 1)

26 {

27 acctBalance =

28 getBalance(deposit, interestRate, year);

29 cout << "Year " << year << ": $"

30 << acctBalance << endl;

31 } //end for

32

33 return 0;

34 } //end of main function

35

36 //*****function definitions*****

37 double getBalance(int amount, double rate, int y)

38 {

39 double balance = 0.0;

40 balance = amount * pow((1 + rate), y);

41 return balance;

42 } //end of getBalance function

Object file

#ifndef Account_Header

#include <iostream>

#include <cmath>

using namespace std;

class Account { //class header

private:

double acctBalance_=0.0; //data member

double interestRate_=0.0;

public:

Account() {} //default constructor

Account(double interestRate) {

interestRate_=interestRate;

}

double checkBalance() { //getter

return acctBalance_;

}

double getRate() {

return interestRate_;

}

double getBalance(int year) {

double balance = 0.0;

balance = acctBalance_ * pow((1+interestRate_), year);

return balance;

}

void addBalance(double deposit) { //setter

acctBalance_ += deposit;

}

void setBalance(double balance) {

acctBalance_ = balance;

}

void setRate(double interestRate) {

interestRate_ = interestRate;

}

};

#endif

Test File

#include <iostream>

#include "Account.h"

using namespace std;

int main() {

double deposit = 0;

Account acct = Account();

acct.setRate(0.015);

acct.addBalance(1000);

cout << "Current Balance: $" << acct.checkBalance() << endl;

// add your for loop with function call here

return 0;

}

// Encapsulation: A class encapsulates all of an object’s attributes and

behaviors.

// Inheritance: The relationship between a superclass and an inherited

class is called an “is a” relationship.

// Abstraction: An abstract class cannot be instantiated, but other

classes are derived from it (information hiding).

// Polymorphism: means the ability to take many forms. e.g. Account acct

= new CheckingAccount();

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below::

Account.h

#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#ifndef Account_Header

#include <iostream>

#include <cmath>

using namespace std;

class Account { //class header

private:

   double acctBalance_=0.0; //data member

   double interestRate_=0.0;

public:

   Account() {} //default constructor

   Account(double interestRate) {

       interestRate_=interestRate;

   }

   double checkBalance() { //getter

       return acctBalance_;

   }

   double getRate() {

       return interestRate_;

   }

   double getBalance(int year) {

       double balance = 0.0;

       balance = acctBalance_ * pow((1+interestRate_), year);

       return balance;

   }

   void addBalance(double deposit) { //setter

       acctBalance_ += deposit;

   }

   void setBalance(double balance) {

       acctBalance_ = balance;

   }

   void setRate(double interestRate) {

       interestRate_ = interestRate;

   }

};

#endif

#endif /* ACCOUNT_H_ */

main.cpp

#include <iostream>
#include "Account.h"
#include <iomanip>
using namespace std;
int main() {
   cout << fixed << showpoint << setprecision(2);
   Account acct = Account();
   acct.setRate(0.02);
   acct.addBalance(1000);
   cout << "Current Balance: $" << acct.checkBalance() << endl;
   cout << "Rate (in decimal form ) : " << acct.getRate() << endl;
   // add your for loop with function call here
   for(int i=1;i<=3;i++){
       cout<<"Year "<<i<<" : $"<<acct.getBalance(i)<<endl;
   }
   return 0;
}

output:

Console X <terminated> CPP Workspace.exe [C/C++Application] C:\Use Current Balance $1000.00 Rate (in decimal form: 0.02 Year

Add a comment
Know the answer?
Add Answer to:
Im having trouble with this C++ program. Lab 10/object/test files provided LAB 10 1 //Savings.cpp - displays the account balance at 2 //the end of 1 through 3 years 3 //Created/revised by <your nam...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture...

    MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture an output. polymorphism. Write an application that creates a vector of Account pointers to two SavingsAccount and two CheckingAccountobjects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its...

  • (C++) How do I develop a polymorphic banking program using an already existing Bank-Account hierarchy? For each account...

    (C++) How do I develop a polymorphic banking program using an already existing Bank-Account hierarchy? For each account in the vector, allow the user to specify an amount of money to withdraw from the Bank-Account using member function debit and an amount of money to deposit into the Bank-Account using member function credit. As you process each Bank-Account, determine its type. If a Bank-Account is a Savings, calculate the amount of interest owed to the Bank-Account using member function calculateInterest,...

  • previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print...

    previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() {    //create Animal object    Animal animal("Dog","Mammal",4);    cout<<"Animal object details"<<endl;    cout<<"Name: "<<animal.getName()<<endl;    cout<<"Type: "<<animal.getType()<<endl;    cout<<"# of Legs: "<<animal.getLegs()<<endl;          cout<<endl<<endl;    //create Cat object    Cat cat("byssinian cat","carnivorous mammal",4,"short...

  • Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes....

    Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes. Do not worry about rounding in classes that are instantiated as integer classes, you may just use the default rounding. You will add an additional data member, method, and bank account type that inherits SavingsAc-count ("CD", or certicate of deposit). Deliverables A driver program (driver.cpp) An implementation of Account class (account.h) An implementation of SavingsAccount (savingsaccount.h) An implementation of CheckingAccount (checkingaccount.h) An implementation of...

  • 1. Define an integer variable month with initial value 3 2. Draw Flowchart for the following...

    1. Define an integer variable month with initial value 3 2. Draw Flowchart for the following question 3. Ask user to input one number and output if it is positive or negative 4. For the following program, draw its variable table and analyze its output (variable value changes): • #include <iostream> • #include <cmath> • using namespace std; • double future_value (double ib, doubler, int n) { • double balance = ib. pow(1 + r. n): return balance: • int...

  • C++ Help. PLEASE include detailed comments and explanations. PLEASE follow the drections exactly. Thank you. Define...

    C++ Help. PLEASE include detailed comments and explanations. PLEASE follow the drections exactly. Thank you. Define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member...

  • #include <iostream #include <string> #ifndef ACCOUNT H #define ACCOUNT H class Account { public: Account(std::string, int);...

    #include <iostream #include <string> #ifndef ACCOUNT H #define ACCOUNT H class Account { public: Account(std::string, int); Account(){}; void deposit(int); void withdraw(int); int getBalance() const; 15 private: int balance{@}; std::string name{}; 18 19 20 Verdif - Account.cpp saved #include "Account.h" Account :: Account(std::string accountName, int startingBalance) : name{accountName) 4 5 if (startingBalance > 0) balance = startingBalance; B 10 void Account::deposit(int depositAmount) { if (depositAmount > 0) balance + depositAmount: ) 12 13 14 15 16 void Account::withdraw(int withdrawAmount) { If...

  •       C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for...

          C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for this part Add on to the lab12a solution(THIS IS PROVIDED BELOW). You will add an overload operator function to the operator << and a sort function (in functions.h and functions.cpp)    Print out the customer’s name, address, account number and balance using whatever formatting you would like    Sort on account balance (hint: you can overload the < operator and use sort from the...

  • I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instruction...

    I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...

  • I am having trouble understanding how this code is able to use the contents of the...

    I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide comments in the main code to describe what is happening? (especially on the bool isNumber) THE MAIN CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) {    if(!isdigit (s[0]))    {        if(s[0] != '-')        return false;               else if(s.length() == 1)        return false;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT