$25
– Lab 12–Objects with Classes
Goals:
· Understand how to build and use Classes
· Understand how to use arrays of objects built with classes
Development Environment: (all students must use Clion)
Skills:Classes, Default Constructor, Constructor, member functions, arrays of objects.
Reading: Chap 13
Deliverables:1) This lab with3screen shots2) lastnameFirstLab12.cpp, Scores.h, Scores.cpp
Part I – Skills Practice (5 points)
· Open a new project in CLion. Call the project Lab12a.
· For this project we are reversing a CString and a string Right click on the Lab11a
New-New C++ Class
Add the name Bank. And accept the defaults Add to targets and Lab11a<-$(Source_Files)
This will create both a Bank.h file and a Bank.cpp file
Double Click on the Bank.h file. Replace the entire text with this
· #ifndefBANK_H
#define BANK_H
#include <string
using namespace std;
class Bank
{
private:
string name;
intnumber;
float balance;
public:
Bank(); //default constructor
Bank(string _name, int_number, float _balance); //constructor
string getName(){return name;}
void setName(string _name){name=_name;}
intgetNumber() {return number;}
void setNumber(int_number){number = _number;}
float getBalance(){return balance;}
void increaseBalance(float amount);
};
#endif
· Now double click on the Bank.cpp. Replace the entire code with the code below:
#include "Bank.h"
Bank::Bank()
{
name = "";
number = 0;
balance = 0;
}
Bank::Bank(string _name, int_number, float _balance)
{
name = _name;
number = _number;
balance = _balance;
}
void Bank::increaseBalance(float amount)
{
balance = balance + amount;
}
· Now double click on the main.cpp. Replace the entire code with the code below:
#include <iostream
#include "Bank.h"
using namespace std;
intmain()
{
Bank acct1("Name", 123, 12.50);
cout<<acct1.getName() <<endl;
cout<<acct1.getNumber()<<endl;
cout<<acct1.getBalance()<<endl;
acct1.increaseBalance(200.00);
cout<<acct1.getName() <<endl;
cout<<acct1.getNumber()<<endl;
cout<<acct1.getBalance()<<endl;
return 0;
}
· Now if you did everything correctly it should compile. If it has errors, try to fix the errors by reading the error and clicking on the link to the error.
· Once it successfully compiles, go to Run-Run Lab 12a
Ø Take a screenshot of the successful output below:
· Go to Run-View Breakpoints
· Check on the main.cpp breakpoint and hit the red minus sign to delete the breakpoint.
· Some of the things that you should have noted in this example.
o Classes should start with a capital letter.
o The Class declaration should be in the .h file
o The Class definitions should either be in line (in the .h file) if only one line, or in a separate .cpp file
o The .h file should always start with the #ifndef, #define and end with the #endif. This ensures that code is only defined once in a compilation
o Every class should have a default constructor. This is called whenever an object of that type is declared. The default constructor usually initializes the private variables
o Any public member function can directly access the private variables.
Part II – More Skills Practice (5 points)
Arrays of Classes. We will now create an array of Bank Accounts
Using the code from Part I. Change main.cpp to:
#include <iostream
#include "Bank.h"
using namespace std;
intmain()
{
constintSIZE = 2;
string tempName;
inttempNumber;
float tempBalance;
Bank acctArray[SIZE];
for (inti=0; i<SIZE; i++)
{
cout<<"Name for customer " <<i+1 <<endl;
getline(cin,tempName);
cout<<"Number for customer " <<i+1 <<endl;
cintempNumber;
cout<<"Balance for customer " <<i+1 <<endl;
cintempBalance;
cin.ignore(); //removes '/n' in buffer for next getline
cout<<endl;
acctArray[i].setName(tempName);
acctArray[i].setNumber(tempNumber);
acctArray[i].increaseBalance(tempBalance);
}
for (inti=0; i<SIZE; i++)
{
cout<<acctArray[i].getName() <<endl;
cout<<acctArray[i].getNumber()<<endl;
cout<<acctArray[i].getBalance()<<endl<<endl;
}
return 0;
}
· Set a breakpoint at the return 0;
· Run-Debug 12a
· Add two users information.
Ø Take a screenshot of the output and place it below: