Skip to content

Commit 43ce5c7

Browse files
committed
chpt 1.5
1 parent 95b483a commit 43ce5c7

6 files changed

Lines changed: 124 additions & 2 deletions

File tree

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"args": [
88
"-fdiagnostics-color=always",
99
"-g",
10-
"${file}",
10+
"${fileDirname}/*.cpp",
1111
"-o",
12-
"${fileDirname}/${fileBasenameNoExtension}.out"
12+
"${fileDirname}/main.out"
1313
],
1414
"options": {
1515
"cwd": "${fileDirname}"

source/chpt_1_ACppPrimer/aSimpleCppProgram.cpp renamed to source/chpt_1_ACppPrimer/aSimpleCppProgram/aSimpleCppProgram.cpp

File renamed without changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Counter {
5+
private:
6+
/* data */
7+
int count;
8+
public:
9+
Counter();
10+
void increaseBy(int x);
11+
int getCount();
12+
};
13+
14+
Counter::Counter() {
15+
count = 0;
16+
}
17+
18+
void Counter::increaseBy(int x) {
19+
count += x;
20+
}
21+
22+
int Counter::getCount() {
23+
return count;
24+
}
25+
26+
int main() {
27+
Counter ctr; // an instance of Counter
28+
cout << ctr.getCount() << endl; // prints the initial value (0)
29+
ctr.increaseBy(3); // increase by 3
30+
cout << ctr.getCount() << endl; // prints 3
31+
ctr.increaseBy(5); // increase by 5
32+
cout << ctr.getCount() << endl;
33+
return 0;
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "CreditCard.h" // provides CreditCard
2+
using namespace std; // make std:: accessible
3+
4+
CreditCard::CreditCard(const string& no, const string& nm, int lim, double bal) {
5+
number = no;
6+
name = nm;
7+
balance = bal;
8+
limit = lim;
9+
}
10+
bool CreditCard::chargeIt(double price) { // make a charge
11+
if (price + balance > double(limit))
12+
return false; // over limit
13+
14+
balance += price;
15+
return true; // the charge goes through
16+
}
17+
18+
void CreditCard::makePayment(double payment) { // make a payment
19+
balance -= payment;
20+
}
21+
// print card information
22+
ostream& operator<<(ostream& out, const CreditCard& c) {
23+
out << "Number = " << c.getNumber() << "\n"
24+
<< "Name = " << c.getName() << "\n"
25+
<< "Balance = " << c.getBalance() << "\n"
26+
<< "Limit = " << c.getLimit() << "\n";
27+
return out;
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef CREDIT_CARD_H // avoid repeated expansion
2+
#define CREDIT_CARD_H
3+
4+
#include <string> // provides string
5+
#include <iostream> // provides ostream
6+
7+
class CreditCard {
8+
public:
9+
CreditCard(const std::string& no, const std::string& nm, int lim, double bal=0);
10+
std::string getNumber() const { return number; }
11+
std::string getName() const { return name; }
12+
double getBalance() const { return balance; }
13+
int getLimit() const { return limit; }
14+
15+
bool chargeIt(double price); // make a charge
16+
void makePayment(double payment); // make a payment
17+
private: // private member data
18+
std::string number; // credit card number
19+
std::string name; // card owner's name
20+
int limit; // credit limit
21+
double balance; // credit card balance
22+
};
23+
24+
std::ostream& operator<<(std::ostream& out, const CreditCard& c); // print card information
25+
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <vector> // provides STL vector
2+
#include "CreditCard.h" // provides CreditCard, cout, string
3+
4+
using namespace std; // make std accessible
5+
6+
void testCard() { // CreditCard test function
7+
vector<CreditCard*> wallet(10); // vector of 10 CreditCard pointers
8+
9+
// allocate 3 new cards
10+
wallet[0] = new CreditCard("5391 0375 9387 5309", "John Bowman", 2500);
11+
wallet[1] = new CreditCard("3485 0399 3395 1954", "John Bowman", 3500);
12+
wallet[2] = new CreditCard("6011 4902 3294 2994", "John Bowman", 5000);
13+
14+
for (int j=1; j <= 16; j++) { // make some charges
15+
wallet[0]->chargeIt(double(j)); // explicitly cast to double
16+
wallet[1]->chargeIt(2 * j); // implicitly cast to double
17+
wallet[2]->chargeIt(double(3 * j));
18+
}
19+
20+
cout << "Card payments:\n";
21+
for (int i=0; i < 3; i++){ // make more charges
22+
cout << *wallet[i];
23+
while (wallet[i]->getBalance() > 100.0) {
24+
wallet[i]->makePayment(100.0);
25+
cout << "New balance = " << wallet[i]->getBalance() << "\n";
26+
}
27+
cout << "\n";
28+
delete wallet[i]; // deallocate storage
29+
}
30+
}
31+
32+
int main() { // main function
33+
testCard();
34+
return EXIT_SUCCESS; // successful execution
35+
}

0 commit comments

Comments
 (0)