Class Definitions
void CarLoan::printLoanInfo() const
{ // calling the overridden base class
Loan version of printLoanInfo()
cout << Loan::printLoanInfo() << ; VIN:
<< VIN << endl;
}
The following code fragment illustrates the declaration and definition
of the classes Account, CarLoan and Mortgage.
/*****************************************************/
/* declaration of base class Account
*/
/*****************************************************/
class Account
{
protected:
char* acctNum; // acct
number
char* name;
// owner name
char* SSN;
float currBal;
public:
Account(const char* const num,
const char* const name,
const char* const ssn=0, const float bal=0);
~Account(){delete[] ID; delete[]
name; delete[] SSN;}
void deposit(const float val);
void withdraw(const
float val);
friend ostream& operator<<(ostream&,
const Account&);
};
/*****************************************************/
/* declaration of derived class Loan
*/
/*****************************************************/
class Loan : public Account
{
protected:
float origBal;
float rate;
public:
Loan(const char* const num, const char*
const name,
const char* const ssn=0, const float bal=0, const float r=0);
void setLoan(const
float bal, const float rate);
void printLoanInfo() const;
};
/*****************************************************/
/* implementation of derived class Loan
*/
/*****************************************************/
Loan::Loan(const char* const num, const char* const name,
const
char* const ssn, const float bal, const float r)
: Account(num, name, ssn,
bal), rate(r)
{}
void Loan::setLoan(const float bal, const float r)
{
origBal = bal;
rate = r;
}
void Loan::printLoanInfo() const
{
cout << Loan amt: << origBal <<
; int rate: << rate << endl;
}
/*****************************************************/
/* declaration of derived class CarLoan
*/
/*****************************************************/
class CarLoan : public Loan
{
char* VIN;
public:
CarLoan(const char* const num, const
char* const name,
const char* const vin, const char* const ssn=0,
const float bal=0, const float r=0);
~CarLoan() {delete[] VIN;}
void setLoan(const
float bal, const float rate,
const char* const vin); // overloading
void printLoanInfo() const;
// overriding Loan::printLoanInfo()
};
/*****************************************************/
/* implementation of derived class CarLoan
*/
/*****************************************************/
CarLoan::CarLoan(const char* const num, const char* const name,
const char* char vin, const char* const ssn,
const float bal, const float rate)
: Loan(num, name, ssn, bal,
rate)
{
VIN = new char[strlen(vin) + 1];
strcpy(VIN, vin);
}
//
overloading
void CarLoan::setLoan(const
float bal, const float r,
const char* const vin)
{
origBal = bal;
rate = r;
VIN = new char[strlen(vin) + 1];
strcpy(VIN, vin);
}
//
overriding
void CarLoan::printLoanInfo()
const
{
cout << Loan::printLoanInfo() << ; VIN:
<< VIN << endl;
}
/*****************************************************/
/* declaration of derived class Mortgage
*/
/*****************************************************/
class Mortgage : public Loan
{
char* address;
public:
Mortgage(const char* const id, const
char* const name,
const char* const addr, const char* const ssn=0,
const float bal=0, const float r=0);
~Mortgage() {delete[] address;}
void setLoan(const float, const float,
const char* const);
void printLoanInfo() const; // overriding
};
/*****************************************************/
/* implementation of derived class Mortgage
*/
/*****************************************************/
Mortgage::Mortgage(const char* const num, const char* const name,
const char* const addr, const char* const ssn,
const float bal, const float rate)
: Loan(num, name, ssn,
bal, rate)
{
address = new char[strlen(addr) + 1];
strcpy(address, addr);
}
//
overloading
void Mortgage::setLoan(const
float bal, const float r,
const char* const addr)
{
origBal = bal;
rate = r;
address = new char[strlen(addr) + 1];
strcpy(address, addr);
}
//
overriding
void Mortgage::printLoanInfo()
const
{
cout << Loan::printLoanInfo() << ; address:
<< address << endl;
}
|