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++ (10th 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)
- 4. def modify_data(x, my_list): X = X + 1 my_list.append(x) print(f"Inside the function: x = {x}, my_list = {my_list}") num = 5 numbers = [1, 2, 3] modify_data(num, numbers) print(f"Outside the function: num = {num}, my_list = {numbers}") Classe Classe that lin Thus, A pro is ref inter Ever dict The The output: Inside the function:? Outside the function:?arrow_forwardpython Tasks 5 • Task 1: Building a Library Management system. Write a Book class and a function to filter books by publication year. • Task 2: Create a Person class with name and age attributes, and calculate the average age of a list of people Task 3: Building a Movie Collection system. Each movie has a title, a genre, and a rating. Write a function to filter movies based on a minimum rating. ⚫ Task 4: Find Young Animals. Create an Animal class with name, species, and age attributes, and track the animals' ages to know which ones are still young. • Task 5(homework): In a store's inventory system, you want to apply discounts to products and filter those with prices above a specified amount. 27/04/1446arrow_forwardOf the five primary components of an information system (hardware, software, data, people, process), which do you think is the most important to the success of a business organization? Part A - Define each primary component of the information system. Part B - Include your perspective on why your selection is most important. Part C - Provide an example from your personal experience to support your answer.arrow_forward
- Management Information Systemsarrow_forwardQ2/find the transfer function C/R for the system shown in the figure Re དarrow_forwardPlease original work select a topic related to architectures or infrastructures (Data Lakehouse Architecture). Discussing how you would implement your chosen topic in a data warehouse project Please cite in text references and add weblinksarrow_forward
- Please original work What topic would be related to architectures or infrastructures. How you would implement your chosen topic in a data warehouse project. Please cite in text references and add weblinksarrow_forwardWhat is cloud computing and why do we use it? Give one of your friends with your answer.arrow_forwardWhat are triggers and how do you invoke them on demand? Give one reference with your answer.arrow_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 Learning