Concept explainers
Solution to Practice
Redefine CDAccount from Display 10.1 so that it is a class rather than a structure. Use the same member variables as in Display 10.1 but make them private. Include member functions for each of the following: one to return the initial balance, one to return the balance at maturity, one to return the interest rate, and one to return the term. Include a constructor that sets all of the member variables to any specified values, as well as a default constructor. Embed your class definition in a test program.
Redefine CDAccount
Program Plan:
- Include the necessary libraries.
- Use namespace.
- Define a class.
- Declare the member functions as public.
- Declare the variables as private.
- Define the main method.
- Declare the variables that are required for the program.
- Call the function.
- Display the messages.
- Add member functions
- Default constructor.
- Constructor to set specified values.
- To return interest rate.
- To return initial balance.
- To return balance at maturity.
- To return the term.
- input function (istream&);
- output function (ostream&);
Program Description:
The following C++ program redefines CDAccount from Display 10.1 to be a class rather than struct.
Explanation of Solution
Program:
//Include libraries
#include <iostream>
//Use namespace
using namespace std;
//Define class
class CDAccount
{
//Public specifiers
public:
//Declare member functions
CDAccount();
CDAccount(double bal, double intRate, int T);
double InterestRate();
double InitialBalance();
double BalanceAtMaturity();
int Term();
void input(istream&);
void output(ostream&);
//Private specifiers
private:
//Declare variables
double balance;
double interestRate; // in PER CENT
int term; // months to maturity;
};
//Main function
int main()
{
//Declare variables
double balance; double intRate;
int term;
//Constructor
CDAccount account = CDAccount(100.0, 10.0, 6);
//Display message
cout << "CD Account interest rate: "
<< account.InterestRate() << endl;
//Display message
cout << "CD Account initial balance: "
<< account.InitialBalance() << endl;
//Display message
cout << "CD Account balance at maturity is: "
<< account.BalanceAtMaturity() << endl;
//Display message
cout << "CD Account term is: " << account.Term()
<< " months" << endl;
account.output(cout);
//Display message
cout << "Enter CD initial balance, interest rate, "
<< " and term: " << endl;
account.input(cin);
//Display message
cout << "CD Account interest rate: "
<< account.InterestRate() << endl;
//Display message
cout << "CD Account initial balance: "
<< account.InitialBalance() << endl;
//Display message
cout << "CD Account balance at maturity is: "
<< account.BalanceAtMaturity() << endl;
//Display message
cout << "CD Account term is: " << account.Term()
<< " months" << endl;
account.output(cout);
//Newline character
cout << endl;
//Pause console window
system("pause");
}
//Default constructor
CDAccount::CDAccount() { /* do nothing */ }
//Constructor to set specified values
CDAccount::CDAccount(double bal, double intRate, int T)
{
balance = bal;
interestRate = intRate;
term = T;
}
//Function to return interest rate
double CDAccount::InterestRate()
{
//Return value
return interestRate;
}
//Function to return initial balance
double CDAccount::InitialBalance()
{
//Return value
return balance;
}
//Function to return the balance at maturity
double CDAccount::BalanceAtMaturity()
{
//Return value
return balance * (1 + (interestRate / 100)*(term / 12.0));
}
//Function to return the term
int CDAccount::Term()
{
//Return value
return term;
}
//Input Function
void CDAccount::input(istream& inStream)
{
//Store value
inStream >> balance;
inStream >> interestRate;
inStream >> term;
}
//Output function
void CDAccount::output(ostream& outStream)
{
//Display values
outStream.setf(ios::fixed);
outStream.setf(ios::showpoint);
outStream.precision(2);
//Display message
outStream << "when your CD matures in " << term
<< " months" << endl
<< "it will have a balance of "
<< BalanceAtMaturity() << endl;
}
Output:
CD Account interest rate: 10
CD Account initial balance: 100
CD Account balance at maturity is: 105
CD Account term is: 6 months
when your CD matures in 6 months
it will have a balance of 105.00
Enter CD initial balance, interest rate, and term:
100
10
12
CD Account interest rate: 10.00
CD Account initial balance: 100.00
CD Account balance at maturity is: 110.00
CD Account term is: 12 months
when your CD matures in 12 months
it will have a balance of 110.00
Press any key to continue . . .
Want to see more full solutions like this?
Chapter 10 Solutions
Problem Solving with C++ (9th Edition)
Additional Engineering Textbook Solutions
Mechanics of Materials (10th Edition)
Database Concepts (8th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Starting Out with Python (4th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Electric Circuits. (11th Edition)
- can u solve this questionarrow_forward1. Unsigned Integers If we have an n-digit unsigned numeral dn-1d n-2...do in radix (or base) r, then the value of that numeral is n−1 r² di Σi=0 which is basically saying that instead of a 10's or 100's place we have an r's or r²'s place. For binary, decimal, and hex r equals 2, 10, and 16, respectively. Just a reminder that in order to write down a large number, we typically use the IEC or SI prefixing system: IEC: Ki = 210, Mi = 220, Gi = 230, Ti = 240, Pi = 250, Ei = 260, Zi = 270, Yi = 280; SI: K=103, M = 106, G = 109, T = 10¹², P = 1015, E = 10¹8, Z = 1021, Y = 1024. 1.1 Conversions a. (15 pts) Write the following using IEC prefixes: 213, 223, 251, 272, 226, 244 21323 Ki8 Ki 223 23 Mi 8 Mi b. (15 pts) Write the following using SI prefixes: 107, 10¹7, 10¹¹, 1022, 1026, 1015 107 10¹ M = 10 M = 1017102 P = 100 P c. (10 pts) Write the following with powers of 10: 7 K, 100 E, 21 G 7 K = 7*10³arrow_forwardanswer shoul avoid using AI and should be basic and please explainarrow_forward
- Node A is connected to node B by a 2000km fiber link having a bandwidth of 100Mbps. What is the total latency time (transmit + propagation) required to transmit a 4000 byte file using packets that include 1000 Bytes of data plus 40 Bytes of header.arrow_forwardanswer should avoid using AI and should be basic and explain pleasearrow_forwardasnwer should avoid using AIarrow_forward
- answer should avoid using AI (such as ChatGPT), do not any answer directly copied from AI would and explain codearrow_forwardWrite a c++ program that will count from 1 to 10 by 1. The default output should be: 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10 There should be only a newline after the last number. Each number except the last should be followed by a comma and a space. To make your program more functional, you should parse command line arguments and change behavior based on their values. Argument Parameter Action -f, --first yes, an integer Change place you start counting -l, --last yes, an integer Change place you end counting -s, --skip optional, an integer, 1 if not specified Change the amount you add to the counter each iteration -h, —help none Print a help message including these instructions. -j, --joke none Tell a number based joke. So, if your program is called counter, counter -f 10 --last 4 --skip 2 should produce 10, 8, 6, 4 Please use the last supplied argument. If your code is called counter, counter -f 4 -f 5 -f 6 should count from 6. You should…arrow_forwardshow workarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning