SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Inheritance
Exercise:
Programming Projects
Question:6 | ISBN:9780132846813 | Edition: 5

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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:

0 0

Discussions

Post the discussion to improve the above solution.