In-Class Exercise
Class with a Resource
This exercise defines a copy constructor, assignment operator
and destructor for copying objects that access their own resources.
Given Information
The following code is a solution to the
Handout on Overloading Constructors.
Client Module
// Overloading Constructors
// h6.cpp
#include <iostream>
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
Transaction.h
// Overloading Constructor
// Transaction.h
class Transaction {
int acct;
char type;
char desc[21];
double amount;
public:
Transaction();
Transaction(int, char, double, const char*);
~Transaction();
void enter();
void display() const;
};
|
Transaction.cpp
The implementation file for the Transaction module
contains
// Overloading Constructor
// Transaction.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
#include "Transaction.h"
Transaction::Transaction() {
acct = 0;
desc[0] = '\0';
type = 'd';
amount = 0.0;
}
Transaction::Transaction(int ac, char ty, double am, const char* de) {
if (ac > 0 && (ty == 'd' || ty == 'c') && am > 0.0) {
// copy validated input into the instance variables
acct = ac;
type = ty;
strcpy(desc, de);
amount = am;
}
else {
// input was invalid - adopt a safe empty state
*this = Transaction();
}
}
Transaction::~Transaction() { }
void Transaction::enter() {
// local variables accept input temporarily
int acct_;
char type_;
char desc_[21];
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_;
*this = Transaction(acct_, type_, amount_, desc_);
}
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;
}
|
Your Task
Transaction Module
Upgrade the definition of the Transaction
class to accept descriptions of any length.
Transaction.h
Upgrade the header file for the Transaction class:
- change the data member that holds the description to one that points to the resource
- add a prototype for the copy constructor
- add a prototype for the assignment operator
// Class with a Resource
// Transaction.h
class Transaction {
int acct;
char type;
char ;
double amount;
public:
Transaction();
Transaction(int, char, double, const char*);
~Transaction();
void enter();
void display() const;
};
|
Transaction.cpp
Upgrade the implementation file for the Transaction class:
- upgrade the definition of the no-argument constructor to initialize the resource's address
- upgrade the definition of the four-argument constructor to allocate memory
- add the definition for the copy-constructor
- add the definition for the assignment operator
- add logic to the destructor that de-allocates dynamic memory
- increase the size of the char array in the
enter() function to accommodate string input
up to 60 characters
// Class with a Resource
// Transaction.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
#include "Transaction.h"
Transaction::Transaction() {
acct = 0;
type = 'd';
amount = 0.0;
}
Transaction::Transaction(int ac, char ty, double am, const char* de) {
if (ac > 0 && (ty == 'd' || ty == 'c') && am > 0.0) {
// copy validated input into the instance variables
acct = ac;
type = ty;
amount = am;
}
else {
// input was invalid - adopt a safe empty state
*this = Transaction();
}
}
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_, );
cout << "Enter the account type (d for debit, c for credit) : ";
cin >> type_; ;
cout << "Enter the account amount : ";
cin >> amount_;
*this = Transaction(acct_, type_, amount_, desc_);
}
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;
}
|
|
|
|