Define a class named Payment that contains a member variable of type float that stores the amount of the payment and appropriate accessor and mutator functions. Also create a member function named paymentDetails that outputs an English sentence describing the amount of the payment.
Next, define a class named CashPayment that is derived from Payment . This class should redefine the paymentDetails function to indicate that the payment is in cash. Include appropriate constructor(s).
Define a class named CreditCardPayment that is derived from Payment . This class should contain member variables for the name on the card, expiration date, and credit card number. Include appropriate constructor(s). Finally, redefine the paymentDetails function to include all credit card information in the printout.
Create a main function that creates at least two CashPayment and two CreditCardPayment objects with different values and calls to paymentDetails for each.
Complete Program:
// File: payment.h
#ifndef PAYMENT_H
#define PAYMENT_H
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
class Payment
{
private:
float amountOfPayment;
public:
Payment();
Payment(float amount);
float getAmountOfPayment();
void setAmountOfPayment(float pAmountOfPayment);
void paymentDetails();
};
#endif
// File: payment.cpp
#include "payment.h"
Payment::Payment() : amountOfPayment(0)
{
// deliberately empty
}
Payment::Payment(float pAmountOfPayment) : amountOfPayment(pAmountOfPayment)
{
// deliberately empty
}
float Payment::getAmountOfPayment()
{
return amountOfPayment;
}
void Payment::setAmountOfPayment(float pAmountOfPayment)
{
amountOfPayment = pAmountOfPayment;
}
void Payment::paymentDetails()
{
cout << "Amount of payment: $" << amountOfPayment << endl;
}
// File: cashpayment.h
#ifndef CASHPAYMENT_H
#define CASHPAYMENT_H
#include "payment.h"
class CashPayment : public Payment
{
public:
CashPayment();
CashPayment(float cpAmountOfPayment);
void paymentDetails();
};
#endif
// File: cashpayment.cpp
#include "cashpayment.h"
CashPayment::CashPayment() : Payment()
{
// deliberately empty
}
CashPayment::CashPayment(float cpAmountOfPayment) : Payment(cpAmountOfPayment)
{
// deliberately empty
}
void CashPayment::paymentDetails()
{
cout << "Amount of cash payment: $" << getAmountOfPayment() << endl;
}
// File: creditcardpayment.h
#ifndef CREDITCARDPAYMENT_H
#define CREDITCARDPAYMENT_H
#include "payment.h"
class CreditCardPayment : public Payment
{
private:
string nameOnCard;
string expirationDate;
string cardNumber;
public:
CreditCardPayment();
CreditCardPayment(float ccpAmountOfPayment, string ccpNameOnCard,
string ccpExpirationDate, string ccpCardNumber);
void paymentDetails();
};
#endif
// File: creditcardpayment.cpp
#include "creditcardpayment.h"
CreditCardPayment::CreditCardPayment()
: Payment(), nameOnCard(""), expirationDate(""), cardNumber("")
{
// deliberately empty
}
CreditCardPayment::CreditCardPayment(float ccpAmountOfPayment,
string ccpNameOnCard, string ccpExpirationDate, string ccpCardNumber)
: Payment(ccpAmountOfPayment), nameOnCard(ccpNameOnCard),
expirationDate(ccpExpirationDate), cardNumber(ccpCardNumber)
{
// deliberately empty
}
void CreditCardPayment::paymentDetails()
{
cout << "Amount of credit card payment: $" << getAmountOfPayment() << endl;
cout << "Name on the credit card: " << nameOnCard << endl;
cout << "Expiration date: " << expirationDate << endl;
cout << "Credit card number: " << cardNumber << endl;
}
// File: main.cpp
#include "payment.h"
#include "cashpayment.h"
#include "creditcardpayment.h"
int main()
{
CashPayment cp1(75.25);
CashPayment cp2(36.95);
CreditCardPayment ccp1(95.15, "Smith", "12/21/2009", "321654987");
CreditCardPayment ccp2(45.75, "James", "10/30/2008", "963852741");
cout << "Details of Cash #1..." << endl;
cp1.paymentDetails();
cout << "\nDetails of Cash #2..." << endl;
cp2.paymentDetails();
cout << "\nDetails of Credit Card #1..." << endl;
ccp1.paymentDetails();
cout << "\nDetails of Credit Card #2..." << endl;
ccp2.paymentDetails();
return 0;
}
Sample Output: