In-Class Exercise

This exercise overloads the no-argument constructor and simplifies internal class logic by referring to the current object directly.

Given Information

The following code is a solution to the Handout on Constructors and Destructors.

Client Module

 ``` // Constructors and Destructors // h5.cpp #include #include using namespace std; #include "Transaction.h" int main() { int n; // user-sprecified number of Transactions Transaction* tr; cout << "Enter the number of Transactions : "; cin >> n; tr = new (nothrow) Transaction[n]; if (tr == nullptr) { cout << "Memory Allocation Error " << endl; return 1; } cout << "Enter " << n << " Transactions" << endl; for (int i = 0; i < n; i++) tr[i].enter(); cout << endl; cout << " Account Description Credit Debit" << endl;  for (int i = 0; i < n; i++) { tr[i].display(); cout << endl; } delete [] tr; }```

Transaction Module

Transaction.h

 ``` // Constructor and Destructor // Transaction.h class Transaction { int acct; char type; char desc; double amount; public: Transaction(); ~Transaction(); void enter(); void display() const; };```

Transaction.cpp

 ``` // Constructors and Destructors // Transaction.cpp #include #include #include using namespace std; #include "Transaction.h" Transaction::Transaction() { acct = 0; desc = '\0'; type = 'd'; amount = 0.0; } Transaction::~Transaction() { } void Transaction::enter() { // local variables accept input temporarily int acct_; char type_; char desc_; double amount_; // store input from the user in the local variables cout << "Enter the account number : "; cin >> acct_; ; cin.ignore(); cout << "Enter the desription : "; cin.getline(desc_, 21); cout << "Enter the account type (d for debit, c for credit) : "; cin >> type_; ; cout << "Enter the account amount : "; cin >> amount_; if (acct_ > 0 && (type_ == 'd' || type_ == 'c') && amount_ > 0.0) {  // copy validated input into the instance variables acct = acct_; type = type_; strcpy(desc, desc_); amount = amount_; } else { // input was invalid - adopt a safe empty state acct = 0; type = 'd'; desc = '\0'; amount = 0.0; } } void Transaction::display() const { if (acct != 0) { cout << setw(10) << acct; cout << ' ' << setw(20) << left << desc << right; cout << setprecision(2) << fixed; if (type == 'd') cout << setw(20) << amount; else cout << setw(10) << amount; } else cout << "no data available" << endl; }```

Client Module

The following client program uses your upgraded Transaction module.  The dynamic memory allocation has been removed.

 ``` // Overloading Constructors // h6.cpp #include using namespace std; #include "Transaction.h" const int N = 3; int main() { Transaction tr[N]; cout << "Enter " << n << " Transactions" << endl; for (int i = 0; i < N; i++) tr[i].enter(); cout << endl; cout << " Account Description Credit Debit" << endl;  for (int i = 0; i < N; i++) { tr[i].display(); cout << endl; } }```

Transaction Module

Upgrade the definition of the Transaction class to include a four-argument constructor.  Make sure that your class processes valid as well as invalid input properly.

Transaction.h

1. add the prototype for the four-argument constructor
 ``` // Overloading Constructor // Transaction.h class Transaction { int acct; char type; char desc; double amount; public: Transaction(); ~Transaction(); void enter(); void display() const; };```

Transaction.cpp

1. add the definition for the four-argument constructor
 ``` // Overloading Constructor // Transaction.cpp #include #include #include using namespace std; #include "Transaction.h" Transaction::Transaction() { acct = 0; desc = '\0'; type = 'd'; amount = 0.0; } Transaction::~Transaction() { } void Transaction::enter() { // local variables accept input temporarily int acct_; char type_; char desc_; double amount_; // store input from the user in the local variables cout << "Enter the account number : "; cin >> acct_; ; cin.ignore(); cout << "Enter the desription : "; cin.getline(desc_, 21); cout << "Enter the account type (d for debit, c for credit) : ";  cin >> type_; ; cout << "Enter the account amount : "; cin >> amount_; } void Transaction::display() const { if (acct != 0) { cout << setw(10) << acct; cout << ' ' << setw(20) << left << desc << right; cout << setprecision(2) << fixed; if (type == 'd') cout << setw(20) << amount; else cout << setw(10) << amount; } else cout << "no data available" << endl; }```  