PLZ help!! object composition.
May 8, 2013 at 4:11pm UTC
I need to write a program with 9 files: 4 header files with 4 implementation files and one Driver. There will be one base class (Account) and 2 subclasses (Checking,Saving). The last of the four classes is Customer; Account has a Customer data member and uses it in some of its functions. The process of what is supposed to happen is self-explanatory based on the header file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
class Customer
{
private :
char accountID[7];
char name[20];
public :
Customer();
Customer(char [], char []);
void view();
Customer operator = (const Customer &);
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
//customer.cpp
#include <iostream>
#include <string>
#include "customer.h"
using namespace std;
Customer::Customer()
{
for (int x = 0; x < 20; x++)
name[x] = NULL;
for (int x = 0; x < 7; x++)
accountID[x]= NULL;
}
Customer:: Customer(char n[], char id[])
{
cout << "customer constructor is working" << endl;
strcpy_s (name, n);
strcpy_s (accountID, id);
}
void Customer::view()
{
cout << "Name: " ;
for (int x = 0; name[x] != NULL; x++)
cout << name[x];
cout << "\nAccount #: " ;
for (int x = 0; accountID[x] != NULL; x++)
cout << accountID[x];
}
Customer Customer::operator = (const Customer &obj)
{
cout << "ASSINGMENT OPERATOR = IS WORKING NOW!!!! " << endl;
for (int x = 0; accountID[x] != '\0' ; x++)
accountID[x] = obj.accountID[x];
for (int x = 0; name[x] != '\0' ; x++)
name[x] = obj.name[x];
return *this ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "customer.h"
class Account
{
protected :
float balance;
Customer cust;
public :
Account();
Account(float , Customer);
void makeDeposit(float );
float getBalance();
virtual bool makeWithdrawal(float );
virtual void adjustBalance();
virtual void view();
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
//account.cpp
#include <iostream>
#include "customer.h"
#include "account.h"
using namespace std;
Account::Account()
{
balance = 0.0;
}
Account::Account(float Bal, Customer pCust) : balance(Bal)
{
cust = pCust;
}
void Account::makeDeposit(float depos)
{
balance += depos;
}
bool Account::makeWithdrawal(float withd)
{
balance -= withd;
return true ;
}
void Account::view()
{
// Customer theCustomer;
// theCustomer.view();
cust.view();
cout << "\nBalance: $ " << balance << endl;
}
float Account::getBalance()
{
return balance;
}
void Account::adjustBalance()
{}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//saving.h
#ifndef SAVINGS_H
#define SAVINGS_H
#include "account.h"
class Savings : public Account
{
private :
float interestRate;
public :
Savings();
Savings(Customer, float , float );
void makeDeposit(float );
bool makeWithdrawal(float );
void adjustBalance();
virtual void view();
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//saving.cpp
#include <iostream>
#include "savings.h"
using namespace std;
Savings::Savings()
{
interestRate = 0;
}
//Account(float Bal, const Customer &pCust) : balance(Bal), cust(pCust);
Savings::Savings(Customer pCust, float Bal, float rate) : Account (Bal, pCust)
{
interestRate = rate;
}
void Savings::makeDeposit(float depos)
{
Account::makeDeposit(depos);
}
bool Savings::makeWithdrawal(float withd)
{
if (balance < withd)
return false ;
else
Account::makeWithdrawal(withd);
return true ;
}
void Savings::adjustBalance()
{
Account::balance = (interestRate * balance) + balance;
}
void Savings::view()
{
cout << "Savings Account:" << endl;
Account::view();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//checking.h
#ifndef CHECKING_H
#define CHECKING_H
#include "account.h"
class Checking : public Account
{
private :
bool overdraftProtection;
public :
Checking();
Checking(float ,Customer, bool );
void makeDeposit(float );
bool makeWithdrawal(float );
void adjustBalance();
virtual void view();
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
//checking.cpp
#include <iostream>
#include "checking.h"
using namespace std;
Checking::Checking()
{
overdraftProtection = false ;
}
Checking::Checking(float Bal, Customer pCust, bool overP) : Account (Bal, pCust)
{
overdraftProtection = overP;
}
void Checking::makeDeposit(float depos)
{
Account::makeDeposit(depos);
}
bool Checking::makeWithdrawal(float withd)
{
if (balance < withd)
{
if (overdraftProtection == true )
Account::makeWithdrawal(withd);
else
return false ;
}
else
{
Account::makeWithdrawal(withd);
}
return true ;
}
void Checking::adjustBalance()
{
const int serviceCharge = 10;
const int minBal = 1000;
if (balance < minBal)
Account::balance -= serviceCharge;
}
void Checking::view()
{
cout << "Checking Account:" << endl;
Account::view();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
//test.cpp
#include <iostream>
#include "account.h"
#include "checking.h"
#include "savings.h"
using namespace std;
const int MAX = 4;
void doTransactions (Account*);
const int NAME_SIZE = 20;
const int ID_SIZE = 6;
int main()
{
Account* acctsPtr[MAX];
char acctType;
bool validType = true ;
char custName[NAME_SIZE];
char acctID[ID_SIZE];
double startBal;
int aNum;
for (aNum = 0; aNum < MAX && validType; aNum++)
{
cout << "Enter c for checking; s for savings; any other character to quit: " ;
cin >> acctType;
acctType = tolower (acctType);
if (acctType == 'c' || acctType == 's' )
{
cout << "Enter customer name: " ;
cin >> custName;
cout << "Enter account number: " ;
cin >> acctID;
cout << "Enter account beginning balance: " ;
cin >> startBal;
}
switch (acctType)
{
case 'c' :
{
char response;
bool overdraftOk;
cout << "Does this account have overdraft protection? " ;
cin >> response;
overdraftOk = (tolower(response) == 'y' )? true : false ;
Customer c (custName, acctID);
acctsPtr[aNum] = new Checking (c, startBal, overdraftOk);
doTransactions (acctsPtr[aNum]);
acctsPtr[aNum]->adjustBalance();
cout << "Balance after service charge (if any): " << acctsPtr[aNum]->getBalance() << endl;
break ;
}
case 's' :
{
double intRate;
cout << "Enter current monthly interest rate: " ;
cin >> intRate;
Customer c (custName, acctID);
acctsPtr[aNum] = new Savings (c, startBal, intRate);
doTransactions (acctsPtr[aNum]);
acctsPtr[aNum]->adjustBalance();
cout << "Balance after interest: " << acctsPtr[aNum]->getBalance() << endl;
break ;
}
default :
validType = false ;
aNum--;
break ;
}
}
int totalAccts = aNum;
cout << "\nAccounts: \n" ;
for (int i = 0; i < totalAccts; i++)
{
acctsPtr[i]->view();
cout << "\n" ;
}
for (int i = 0; i < totalAccts; i++)
delete acctsPtr[i];
system("pause" );
return 0;
}
void doTransactions (Account* aPtr)
{
double depAmt, withdAmt;
cout << "Enter total deposite: " ;
cin >> depAmt;
aPtr->makeDeposit (depAmt);
cout << "Balance after deposite: " << aPtr->getBalance() << endl;
cout << "Enter total withdrawals: " ;
cin >> withdAmt;
if (aPtr->makeWithdrawal (withdAmt))
cout << "Balance after withdrawals: " << aPtr->getBalance() << endl;
else
cout << "Withdrawal not made -- balance too low\n" << " and no overdraft protection\n" ;
}
My inheritance part is working perfectly. (account - savings/checking)
but my composition part (account - customer) is not working properly.
my output looks like this:
-------------------------------
Accounts:
Checking Account:
Name:
Account #:
Balance: $ -60
Checking Account:
Name:
Account #:
Balance: $ 1500
Checking Account:
Name:
Account #:
Balance: $ 890
Savings Account:
Name:
Account #:
Balance: $ 1540
------------------------------
as you can see above, my output doesn't show name and account# which is customer class member variable name and accountID.
PLEASE HELP ME OUT!!!
Topic archived. No new replies allowed.