In-Class Exercise
Constructor and Destructor
This exercise introduces a constructor and destructor
to a class definition to ensure that all objects contain
either valid data or a safe empty state and introduces logic
to ensure that member functions gracefully handle objects
that contain a safe empty state.
Given Information
The following code is a solution to the Handout on
Dynamic Memory.
Client Module
// Dynamic Memory
// h4.cpp
#include <new>
#include <iostream>
using namespace std;
#include "Transaction.h"
int main() {
int n; // user-specified 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
// Dynamic Memory
// Transaction.h
struct Transaction {
private:
int acct;
char type;
char desc[21];
double amount;
public:
void enter();
void display() const;
};
|
Transaction.cpp
// Dynamic Memory
// Transaction.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "Transaction.h"
void Transaction::enter() {
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 {
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;
}
|
Your Task
Transaction Module
Upgrade the definition of the Transaction class
to include a no-argument constructor and an empty destructor. Make
sure that your member functions gracefully process objects
in a safe empty state.
Transaction.h
Upgrade the header file for your Transaction type:
- replace the keywords struct
and private with the keyword
class
- add the prototype for the no-argument constructor
- add the prototype for the destructor
// Constructor and Destructor
// Transaction.h
Transaction {
int acct;
char type;
char desc[21];
double amount;
public:
void enter();
void display() const;
};
|
Transaction.cpp
Upgrade the implementation file for your Transaction type:
- add the function definition for the no-argument constructor
- add the function definition for the empty destructor
- complete the logic for the enter() member
function, rejecting account numbers that aren't positive, amounts that
aren't positive, and types that are neither 'c' nor 'd'
- complete the logic for your display()
query
// Constructor and Destructor
// Transaction.cpp
#include <iostream>
using namespace std;
#include "Transaction.h"
void Transaction::enter() {
// local variables accept input temporarily
int ;
char ;
char [21];
double ;
// store input from the user in the local variables
cout << "Enter the account number : ";
cin >> ; ;
cin.ignore();
cout << "Enter the desription : ";
cin.getline(desc, 21);
cout << "Enter the account type (d for debit, c for credit) : ";
cin >> ; ;
cout << "Enter the account amount : ";
cin >> ;
if ( ) {
// copy validated input into the instance variables
acct = ;
type = ;
;
amount = ;
}
else {
// input was invalid - adopt a safe empty state
acct = ;
type = ;
;
amount = ;
}
}
void Transaction::display() const {
if ( ) {
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 << << endl;
}
|
|