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++, Student Value Edition Plus MyLab Programming with Pearson eText - Access Card Package (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)
- Consider the definition of the following class: (1, 2, 3, 5, 7) a. Give the line number containing the constructor that is executed in each of the following declarations. b. Write the definition of the constructor in Line 4 so that the instance variables are initialized to " ", " ", " ", 0, 0.0, and 0.0, respectively. c Write the definition of the constructor in Line 5 so that the string instance variables are initialized to"", and the other instance variables are initialized according to the parameters. Instance variables quantitieslnStock, price, and discount must be nonnegative. d. Write the definition of the constructor in Line 6 so that the instance variables productName and manufacturer are initialized to the empty string, instance variable id is initialized according to the first parameter, and the remaining instance variables are initialized according to the last three parameters. Instance variables quantitieslnStock, price, and discount must be nonnegative. e. Write the definition of the constructor in Line 7 so that the instance variables are initialized according to the parameters. Instance variables quantitieslnStock, price, and discount must be nonnegative.arrow_forwardWrite a program to model a student class with roll number and name as the sensitive information. Initialize default values for the sensitive information using a default constructor, use a copy constructor to copy from another object, use a member function to take user input and update the name and roll number and display the same using another member function. b.Add constructor overloading to the above code and enable initialization of name, roll number via constructor arguments. NAME ROHI ROLLNO 18arrow_forwardWrite a program IN C++ to model a student class with roll number and name as the sensitive information. Initialize default values for the sensitive information using a default constructor, use a copy constructor to copy from another object, use a member function to take user input and update the name and roll number and display the same using another member function. b.Add constructor overloading to the above code and enable initialization of name, roll number via constructor arguments. NAME ROHI ROLLNO 18arrow_forward
- Write a program IN C++ to model a student class with roll number and name as the sensitive information. Initialize default values for the sensitive information using a default constructor, use a copy constructor to copy from another object, use a member function to take user input and update the name and roll number and display the same using another member function. b.Add constructor overloading to the above code and enable initialization of name, roll number via constructor arguments. NAME KOHLI ROLLNO 18arrow_forwardWrite the program of object oriented programming c++. Declared Rectangle(Data Members: Length and width) as a friend class of Square(Data Member: int side). Thus now, all the functions of Rectangle can directly access any private member of Square.In the main function, the first statement created an object 'square' of the class Square, thus calling its constructor and assigning 5 to its data member side.The second statement in the main function created an object 'rectangle' of the class Rectangle.In the third statement, 'rectangle' called the function shape() with the object of the class Square passed as its argument. In the 'shape()' function, the value of 'side' (private data member of Square) was assigned to length and breadth. The fourth statement called the function 'getArea' of the class Rectangle.arrow_forwardWrite a C++ program to get Fixed account details from the user and display the details using multilevel inheritance with the following classes. Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, member variable names, and function names should be the same as specified in the problem statement. Consider a class Account with the following protected member variables. Datatype Variable String accountHolderName String accountNumber double balance Consider a derived class SavingAccount derived from Account class with the following protected member variable. Datatype Variable double minimumBalance Consider a derived class FixedAcocunt derived from SavingAccount class with the following private member variable, Data type Member Name Integer lockingPeriod Include the following member function in FixedAcocunt class Member Function Description void display() This function is used to display the…arrow_forward
- Q2: Consider the following statements: public class YClass { private int a; private int b; public void one() { } public void two(int x, int y); { } public YClass() { } } class XClass extends YClass { private int z; public void one() { } public XClass() { } } YClass yObject; XClass xObject; Write the definition of the default constructor of YClass so that the instance variables of YClass are initialized to 0. Write the definition of the default constructor of XClass so that the instance variables of XClass are initialized to 0. Write the definition of the method two of YClass so that the instance variable a is initialized to the value of the first parameter of two and the instance variable b is initialized to the value of the second parameter of two. please I want solution :(arrow_forwardKindly Solve this C++ question as per the instructions. Thank you for your help! Instructions: 1- Put the class definition in Flight.h and the implementation of the constructors and functions in Flight.cpp Implement the Flight.h and Flight.cpp so that class Flight contains: 4 private instance variables: Name of data type string, which describe the three electric vehicle charger types (AC975, DL521, and AC863). Destination of data type string that shows the final destination of that flight. Hour and Minute of data type int that show the departure time. A default constructor which sets all of the numeric instance variables to zero and the String instance variables to null. A constructor with 4 parameters that sets the 4 instance variables to the corresponding values passed. Implement an accessor method for each information (Flight name, Flight destination, and Flight Time) that will return the value of the instance variable. For example, the getX() method for the instance variables…arrow_forwardWrite a program in the C++ language to create a class Rectangle with two member integer variables length and breadth. Write the parameterized constructor for the class and also add a member function that displays the area of the rectangle in the output.arrow_forward
- Consider the following declarations: (1, 2, 5, 7) How many members does class foodType have? How many private members does class foodType have? How many constructors does class foodType have? How many constant functions does class foodType have? Which constructor is used to initialize the object fruit1? Which constructor is used to initialize the object fruit2? Rewrite the definition of the class foodType so that each data member can be set individually. Write a C++ statement to replace the definition of the constructors with a constructor with default parameter.arrow_forwardWrite the following program in C++. 1. Create a class called Employee that includes attributes: empid, name, points,group, and avg with data types: “int”, “String”, “double”, “String”, and “double”respectively.2. Include a constructor with parameters: empid and name.3. Include another constructor to assign default values to the attributes.4. Include a function called addPoints that is used to add a given amount to the valueof the attribute points.5. Include a function called upgradePoints that is used to increase the value of theattribute points by a given percentage.6. 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 ofthe attribute should be set into zero.7. 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_forwardWrite an OOP program in C++, which contains three, classes i.e. Parent Class, Child1 Class & Child2 Class. Parent Class inherited within Child1 Class & Child2 Class. Parent Class has one member function that is Public: void Show( );. Child1 & Child2 Class will follow the signature of the Parent Class member function; each class will have different function definitions. And during running time a message will be displayed that which class object you want to create to call their member function as shown in the output below. For Example: if enter 1 then it will call parent class function If enter 2 then it will call child1 class function If enter 3 then it will call child2 class function. Hint: Solve this Program using Run Time Polymorphism.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