Can I get some explanation about this c++ code?? #include \"CreditCard.h\" #incl
ID: 3740473 • Letter: C
Question
Can I get some explanation about this c++ code??
#include "CreditCard.h"
#include <string>
using namespace std;
CreditCard::CreditCard(string no, string nm, int lim, double bal = 0)
{
number = no;
name = nm;
balance = bal;
limit = lim;
}
bool CreditCard::chargelt(double price) {
if (price + balance > double(limit))
return false;
balance += price;
return true;
}
std::ostream & operator << (std::ostream& out, const CreditCard & c) {
out << "Number = " << c.getNumber() << endl
<< "Name = " << c.getName() << endl
<< "Balance = " << c.getBalance() << endl
<< "Limit = " << c.getLimit << endl;
return out;
}
#include <vector>
#include "CreditCard.h"
using namespace std;
void testCard() {
vector<CreditCard*> wallet(10);
wallet[0] = new CreditCard("5391 0375 9387 5309", "Jonh Bowman", 3500);
wallet[1] = new CreditCard("3485 0399 3395 1954", "Jonh Bowman", 3500);
wallet[2] = new CreditCard("6011 4902 3294 2994", "John Bowman", 5000);
for (int j = 1; j <= 16; j++) {
wallet[0]->chargelt(double(j));
wallet[1]->chargelt(2 * j);
wallet[2]->chargelt((3 * j));
}
cout << "Card payments: ";
for (int i = 0; i < 3; i++) {
cout << *wallet[i];
while (wallet[i]->getBalance() > 100.0) {
wallet[i]->makePayment(100.0);
cout << "New balane = " << wallet[i]->getBalance() << endl;
}
cout << " ";
delete wallet[i];
}
}
int main() {
testCard();
return EXIT_SUCCESS;
}
Explanation / Answer
Hi,
1. First of all, main() is executed and calls testCard().
2. In testCard(), following operations are executed:
(i) vector<CreditCard*> wallet(10) creates a vector, wallet having 10 objects of type CreditCard.
(ii)
wallet[0] = new CreditCard("5391 0375 9387 5309", "Jonh Bowman", 3500);
wallet[1] = new CreditCard("3485 0399 3395 1954", "Jonh Bowman", 3500);
wallet[2] = new CreditCard("6011 4902 3294 2994", "John Bowman", 5000);
Then, these three lines creates three objects of type CreditCard calling its constructor:
CreditCard::CreditCard(string no, string nm, int lim, double bal = 0) and assigns each to an object in vector wallet. So each of the wallet has limit set to 3500 and balance set to 0.
(iii) Now it runs a loop 16 times, and passes different values of j to method chargeIt using objects wallet[0], wallet[1] and wallet[2].
So, for example when wallet[0] -> chargeIt(double j) is called for j=1,
chargeIt() assigns value of j to variable price, and checks if price + wallet[0].balance > wallet[0].limit, it returns false, otherwise it adds the price to wallet[0].balance and returns true.
This is repeated 16 times with all the three wallets with different values of j.
(iv) Now in this code, operator << has been overridden to print the attributes of an object of type CreditCard in the code: std::ostream & operator << (std::ostream& out, const CreditCard & c) {}
Here cout << *wallet[i]; prints the values for each wallet and then calls the method makePayment() repeatedly till the balance for that wallet is more than 100.
while (wallet[i]->getBalance() > 100.0) {
wallet[i]->makePayment(100.0);
You have not given the class definition CreditCard.h, which must be containing the method definition for makePayment(), which in my understanding must be deducting the passed value from the balance of the wallet being used to call it.
Then it prints the New Balance each time after makePayment() is called.
When the balance left is less than equal to 100, the wallet is deleted from the memory.
If you still find any confusion in any part of the code, feel free to comment.
And if this solves your query, please rate this answer :)
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.