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
Concepts Of Programming Languages
Starting Out With Visual Basic (7th Edition)
Starting Out with C++: Early Objects (9th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Starting Out with Python (4th Edition)
- Write Code in C# (Console App .NET framework Subject : Object Oriented Programmingarrow_forwardPlease solution in c++arrow_forwardLanguage: C++ Create a class named City. Assume that a city has a name, a number of inhabitants, a mayor and an area (in km²). Then create three instances of this class: Bremen, Paris and London. Provide suitable setter and getter methods for each of these properties. The class declaration has to be placed into City.h, the class definition has to be placed into City.cpp and the test program where the instances are created has to be in testcity.cpp. You can set the needed data from the main () function by initialization or read it from the key- board.arrow_forward
- Perform in C++arrow_forwardWrite a c++ program and explain with comments Create a class called Employee that includes attributes: empid, name, points, group, and avg with data types: “int”, “String”, “double”, “String”, and “double” respectively. Include a constructor with parameters: empid and name. Include another constructor to assign default values to the attributes. Include a function called addPoints that is used to add a given amount to the value of the attribute points. Include a function called upgradePoints that is used to increase the value of theattribute points by a given percentage. Include a function called removePoints that is used to reduce a given amount fromthe value of the attribute points. If the resultant value is negative then the value of the attribute should be set into zero. Include a function called computeGroup that assigns a value to the attribute groupbased on the value of the attribute points as given in the following table. Points Group points < 100 Silver 100…arrow_forwardSUBJECT: OOPPROGRAMMING LANGUAGE: C++ ALSO ADD SCREENSHOTS OF OUTPUT. Write a class Distance to measure distance in meters and kilometers. The class should have appropriate constructors for initializing members to 0 as well as user provided values. The class should have display function to display the data members on screen. Write another class Time to measure time in hours and minutes. The class should have appropriate constructors for initializing members to 0 as well as user provided values. The class should have display function to display the data members on screen. Write another class which has appropriate functions for taking objects of the Distance class and Time class to store time and distance in a file. Make the data members and functions in your program const where applicablearrow_forward
- need help with c++...Please explain the code and the question TOO.arrow_forwardIn C++, Create a class called Date that includes three data members: amonth(type int), a day(type int), and a year(type int). The Date class willhave a constructor with three parameters to initialize the three data members.You may assume the values provided for the year and day are correct, but makesure that the month value is in the range 1-12; if it isn't, set the month to1. Provide set and get functions for each data member. Provide a memberfunction displayDate that displays the date in format MM/DD/YYYY.Write a test program that prompts the user to input the month, the day,and the year and uses your Date class to output the formatted date.arrow_forwardIn c++1. Create a room structure with member-data room dimensions. Write a show () function that displays the room data and an area () function that calculates its face (squaring).In main () create a static array of rooms (Room variables) - the rooms in one apartment. For each of the rooms, display its dimensions and square footage. Determine the total square footage of the apartment.2. Create a Room class with the same member data and member functions:- constructors - by default and with parameters- member-functions set () - to set the corresponding size of the room- member-functions get () - return the corresponding size-show () - outputting a Room object-аrea () - returns the face of the roomIn main (), create:(a) single room objects located in the stack and in dynamic memory, respectively, using all defined constructors(b) 2 arrays of rooms located in the stack and in the dynamic memory (rooms in two apartments), using all defined constructorsFor each of the rooms: set the…arrow_forward
- Problem Description:In this problem, you will develop an employee management system using Python. Here are the brief requirements of the program: 1. Write a class named Employee, that holds information about each employee inattributes such as name, ID number, department, and job title. 2. Once you have written the class, Draw the UML diagram, and write a program thatcreates three objects of employee class, with following information and displays theirdetails. Name ID Number Department Job TitleSusanna Myer 47899 Accounting Vice PresidentMark Joseph 39119 Info Tech ProgrammerJoyce Roberts 81774 Manufacturing Engineer 3. Create a new class named ShiftEmployee, that is a subclass of Employee class. Theshift employee has additional attributes in addition to name, ID number, Departmentand Job Title. The additional attributes are:- Shift number (an integer, such as 1,2, or 3)- Hourly pay rateThe workday is divided into two shifts: day and night. The shift attribute will hold an integer value…arrow_forwardLanguage: C++ Next a thirst level (as double value) should be added to the properties of a critter. Add a new constructor that takes five parameters for setting all properties of a critter. Make also sure that the existing constructors will still work. For the existing constructors, the thirst level should be set to the same level as the hunger level. Your existing testcritter.cpp must still be able to run in its unchanged form. So the already existing constructors need to support the change. Name the files Critter.h, Critter.cpp and testcritter.cpp. Finally, you should adapt the print method for printing on the screen also the value of the thirst level as a double. The client program testcritter.cpp may contain one additional line, where the constructor taking five parameters is being called. You can assume that the setting values are always valid.arrow_forwardUse C++arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr