Step 1: In this experiment you will investigate the implementation of a class. E
ID: 3681518 • Letter: S
Question
Step 1: In this experiment you will investigate the implementation of a class.
Enter, save, compile and execute the following program in MSVS. Call the new project “IntroClassesExp1” and the program “IntroClasses1.cpp”. Answer the questions below:
#include <iostream>
using namespace std;
class Bank_Acct
{
public:
Bank_Acct( );
double Check_Balance( );
void Deposit(double);
void Withdrawal(double);
private:
double balance;
};
Bank_Acct::Bank_Acct()
{
balance = 0;
}
double Bank_Acct::Check_Balance()
{
return balance;
}
void Bank_Acct::Deposit(double amount)
{
balance = balance + amount;
}
void Bank_Acct::Withdrawal(double amount)
{
balance = balance - amount;
}
int main()
{
Bank_Acct my_Acct;
cout<<"My Account Balance = "<<my_Acct.Check_Balance()<<endl;
my_Acct.Deposit(2516.83);
cout<<"My Account Balance = "<<my_Acct.Check_Balance()<<endl;
my_Acct.Withdrawal(25.96);
cout<<"My Account Balance = "<<my_Acct.Check_Balance()<<endl;
return 0;
}
Question 1:Please list the elements that make up the state of the class “Bank_Acct” in the program in Step 1?
Question 2:Please list the element(s) that make up the behavior of the class “Bank_ Acct” in the program in Step 1?
Question 3:What kind of member function is Check_Balance in the program in Step 1?
Question 4:What kind of member function is Withdrawal in the program in Step 1?
Question 5:What kind of member function is Deposit in the program in Step 1?
Question 6:What kind of member function is Bank_Acct in the program in Step 1?
Question 7:Can you describe the operation of the dot operation in the program in Step 1?
Quesion 8. Referring to the first cout statement in the program in Step 1, when was the account balance set to 0? Explain your answer?
Explanation / Answer
Please find the answers for respective question numbers ,
1) Context Class: This class is the one which has the states. The application will be using (interfering with) this class.
The class is responsible to maintain current state.
2) The member function in the class defines the behavior of the class , ie, provide definition
for supporting various operations on data it holds. Here Check_Balance() , Deposit(), Withdrawal()
functions defines the class behavior and it uses the data "Balance" of type Double.
Based on these three function calls the behavior varies for the class
3) The member function Check_Balance() is a public member function that returns data of type double , and has no parameter ,
and it belongs to class Bank_Acct. This functions checks the data balance of type double and returns the same.
4) The member function Deposit() public member function which is is a single paramterized that returns void(no data) and belongs to
class Bank_Acct. This functions adds the amount we deposit to existing balance , hence balance is incremented.
5) The member function Withdrawal() is a public member function is a single paramterized that returns void(no data) and belongs to
class Bank_Acct. This functions deducts the amount we withdraw from balance , hence balance is decremented.
6) Bank_Acct() is a non parametrized constructer. we can see no argumnets passed to the constuctor.
The one which has same name as class name is called constructor .
A constructor is a kind of member function that initializes an instance of its class. A constructor has the same name as the class and no return value.
7) The dot operator is applied to the object which is used to call the class members functions or data( variables).
It is always needed to use dot operator to call the class members using the objects.
8) The Account balance will be set to zero once the program starts to execute , the constructor will
be called whenever we create the object , in main() we have created Bank_Acct my_Acct; at this point the
constructor will be called as mentioned in point 6 , inside the constructor the data initization
to zero(0) takes place, we can see inside construtor balance =0; Once we create the object the balance is
zero(0) , so first cout displays zero , after the member function call like Deposit and withdrwal
the balance increments or decrements.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.