C++ Declaration Syntax class Class_name { public: constructors destructor member
ID: 3603205 • Letter: C
Question
C++
Declaration Syntax
class Class_name
{
public:
constructors
destructor
member functions
accessors
mutators
public data
private:
helper functions
data
};
Example:
class Bank_Transaction
{
public:
Bank_Transaction( ); //initialize the state
double Check_Balance( ); //return the dollar amount of balance
void Deposit(double); //increase balance by a dollar amount
void Withdrawal(double); //decrease balance by a dollar amount
private:
double balance;
};
More information on classes can be found in your course textbook and on the web.
Experiments
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
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.Deposit(2516.83);
cout<<"My Account Balance = "<
my_Acct.Withdrawal(25.96);
cout<<"My Account Balance = "<
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?
Question 8:Referring to the first cout statement in the program in Step 1, when was the account balance set to 0? Explain your answer?
Step 2: Enter, save, compile and execute the following program in MSVS. Call the new project “IntroClassesExp2” and the program “IntroClasses2.cpp”. Answer the questions below:
#include
using namespace std;
class Bank_Transaction
{
public:
Bank_Transaction( ); //default constructor
Bank_Transaction(double);
double Check_Balance( );
void Deposit(double);
void Withdrawal(double);
private:
double balance;
};
Bank_Transaction::Bank_Transaction()
{
balance = 0;
}
Bank_Transaction::Bank_Transaction(double amount)
{
balance = amount;
}
double Bank_Transaction::Check_Balance()
{
return balance;
}
void Bank_Transaction::Deposit(double amount)
{
balance = balance + amount;
}
void Bank_Transaction::Withdrawal(double amount)
{
balance = balance - amount;
}
int main()
{
Bank_Transaction my_Acct;
Bank_Transaction your_Acct(10340.85);
cout<<"Your Account Balance = "<
your_Acct.Deposit(512.30);
cout<<"Your Account Balance = "<
your_Acct.Withdrawal(8284.56);
cout<<"Your Account Balance = "<
cout<<"My Account Balance = "<
my_Acct.Deposit(2516.83);
cout<<"My Account Balance = "<
my_Acct.Withdrawal(25.96);
cout<<"My Account Balance = "<
return 0;
}
Question 9:Write the statement(ie. the actual line of code) in the program in Step 2 that initializes the balance of the object “your_Acct”.
Question 10:Give the full name of the function(This is also referred to as the function header) and state the type of the constructor that initialized the object “your_Acct”.
Question 11:What happens if you add the statement “my_Acct.Balance = 0;” to the main function of the program in Step 2 after the object declarations? Explain your answer.
Question 12: What do we mean when we use the following phases?
a. Inside the class
b. Outside the class
PLEASE ANSWER QUESTIONS 1-12 5 stars if answers comes within 4 hours
C++, any incorrect/spam answers; otherwise, a 5 stars and much more appreciation :D
Explanation / Answer
Multiple questions were asked i had answered for Step-1 please raise step 2 as separate question as per chegg rules:
Step-1:
Question 1: Please list the elements that make up the state of the class “Bank_Acct” in the program in Step 1?
Ans: balance
Question 2: Please list the element(s) that make up the behavior of the class “Bank_ Acct” in the program in Step 1?
Ans: Check_Balance
Deposit
Withdrawal
Question 3: What kind of member function is Check_Balance in the program in Step 1?
Ans: Check_Balance is an accessor member function because it is not changing the value of variable 'balance'
** accessor/const member function cannot change member variables. Modifier/Non const member functions can change the member variables.
Question 4: What kind of member function is Withdrawal in the program in Step 1?
Ans: Withdrawal is a modifier member function because it is changing the value of variable 'balance'
Question 5: What kind of member function is Deposit in the program in Step 1?
Ans: Deposit is a modifier member function because it is changing the value of variable 'balance'
Question 6: What kind of member function is Bank_Acct in the program in Step 1?
Ans: Bank_Acct is a constructor
Question 7: Can you describe the operation of the dot operation in the program in Step 1?
Ans: A dot acts as a pointer to reference the members of the class.
In this case, my_Acct.Deposit(2516.83) is referencing/calling the Deposit member function of class Bank_Acct (using object my_acct)
Question 8: Referring to the first cout statement in the program in Step 1, when was the account balance set to 0? Explain your answer?
Ans: In the constructor Bank_Acct();
When a new object is created for a class, it automatically executes the constructor.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.