Objects and Classes in C++: Class Abstraction and Encapsulation

Class abstraction is the separation of class implementation from the use of a class. The details of implementation are encapsulated and hidden from the user. This is known as class encapsulation.

In Chapter 6 you learned about function abstraction and used it in stepwise program develop­ment. C++ provides many levels of abstraction. Class abstraction is the separation of class class abstraction implementation from the use of a class. The creator of a class provides a description of the class and lets the user know how it can be used. The collection of functions and fields that are accessible from outside the class, together with the description of how these members are expected to behave, serves as the class’s contract. As shown in Figure 9.7, the user of the class’s contract class does not need to know how the class is implemented. The details of implementation are encapsulated and hidden from the user. This is known as class encapsulation. For example, class encapsulation you can create a Circle object and find the area of the circle without knowing how the area is computed.

Class abstraction and encapsulation are two sides of the same coin. Many real-life exam­ples illustrate the concept of class abstraction. Consider, for instance, building a computer system. Your personal computer is made up of many components, such as a CPU, CD-ROM, floppy disk, motherboard, fan, and so on. Each component can be viewed as an object that has properties and functions. To get the components to work together, all you need to know is how each component is used and how it interacts with the others. You don’t need to know how it works internally. The internal implementation is encapsulated and hidden from you. You can build a computer without knowing how a component is implemented.

The computer-system analogy precisely mirrors the object-oriented approach. Each com­ponent can be viewed as an object of the class for the component. For example, you might have a class that models all kinds of fans for use in a computer, with properties like fan size and speed, functions like start, stop, and so on. A specific fan is an instance of this class with specific property values.

As another example, consider getting a loan. A specific loan can be viewed as an object of a Loan class. Interest rate, loan amount, and loan period are its data properties, and com­puting monthly payment and total payment are its functions. When you buy a car, a loan object is created by instantiating the class with your loan interest rate, loan amount, and loan period. You can then use the functions to find the monthly payment and total payment of your loan. As a user of the Loan class, you don’t need to know how these functions are implemented.

Let us use the Loan class as an example to demonstrate the creation and use of classes. Loan has the data fields annualInterestRate, numberOfYears, and loanAmount, and the functions getAnnualInterestRate, getNumberOfYears, getLoanAmount, setAnnualInterestRate, setNumberOfYears, setLoanAmount, getMonthlyPayment, and getTotalPayment, as shown in Figure 9.8.

The UML diagram in Figure 9.8 serves as the contract for the Loan class. Throughout the book, you will play the role of both class user and class developer. The user can use the class without knowing how the class is implemented. Assume that the Loan class is available, with the header file, as shown in Listing 9.13. Let us begin by writing a test program that uses the Loan class, in Listing 9.14.

Listing 9.13 Loan.h

1 #ifndef LOAN_H
2
#define LOAN_H
3
4
class Loan
5 {
6   
public:
7    Loan();
8    Loan(
double rate, int years, double amount);
9   
double getAnnualInterestRate();
10   
int getNumberOfYears();
11   
double getLoanAmount();
12   
void setAnnualInterestRate(double rate);
13   
void setNumberOfYears(int years);
14   
void setLoanAmount(double amount);
15   
double getMonthlyPayment();
16   
double getTotalPayment();
17
18
private:
19   
double annualInterestRate;

20    int numberOfYears;
21   
double loanAmount;
22 };
23
24
#endif

Listing 9.14 TestLoanClass.cpp

1 #include <iostream>
2
#include <iomanip>
3
#include “Loan.h”
4 using namespace std;
5
6
int main()
7 {
8   
// Enter annual interest rate
9    cout << “Enter yearly interest rate, for example 8.25: “;
10   
double annualInterestRate;
11   cin >> annualInterestRate;
12
13   
// Enter number of years
14    cout << “Enter number of years as an integer, for example 5: “;
15   
int numberOfYears;
16    cin >> numberOfYears;
17
18   
// Enter loan amount
19    cout << “Enter loan amount, for example 120000.95: “;
20   
double loanAmount;
21    cin >> loanAmount;
22
23   
// Create Loan object
24    Loan loan(annualInterestRate, numberOfYears, loanAmount);
25
26   
// Display results
27    cout << fixed << setprecision(2);
28    cout <<
“The monthly payment is ”
29    << loan.getMonthlyPayment() << endl;
30    cout <<
“The total payment is ” << loan.getTotalPayment() << endl;
31
32   
return 0;
33 }
34

The main function reads interest rate, payment period (in years), and loan amount (lines 8-21), creates a Loan object (line 24), and then obtains the monthly payment (line 29) and total payment (line 30) using the instance functions in the Loan class.

The Loan class can be implemented as in Listing 9.15.

Listing 9.15 Loan.cpp

1 #include “Loan.h”
2 #include <cmath>
3
using namespace std;
4
5 Loan::Loan()
6 {
7     annualInterestRate =
9.5;
8     numberOfYears =
30;
9     loanAmount =
100000;
10 }

11
12 Loan::Loan(
double rate, int years, double amount)
13 {
14    annualInterestRate = rate;
15    numberOfYears = years;
16    loanAmount = amount;
17 }
18
19
double Loan::getAnnualInterestRate()
20 {
21   
return annualInterestRate;
22 }
23
24
int Loan::getNumberOfYears()
25 {
26   
return numberOfYears;
27 }
28
29
double Loan::getLoanAmount()
30 {
31   
return loanAmount;
32 }
33
34
void Loan::setAnnualInterestRate(double rate)
35 {
36    annualInterestRate = rate;
37 }
38
39
void Loan::setNumberOfYears(int years)
40 {
41    numberOfYears = years;
42 }
43
44
void Loan::setLoanAmount(double amount)
45 {
46    loanAmount = amount;
47 }
48
49
double Loan::getMonthlyPayment()
50 {
51   
double monthlyInterestRate = annualInterestRate / 1200;
52   
return loanAmount * monthlyInterestRate / (1
53    (pow(
1 / (1 + monthlyInterestRate), numberOfYears * 12)));
54 }
55
56
double Loan::getTotalPayment()
57 {
58   
return getMonthlyPayment() * numberOfYears * 12;
59 }

From a class developer’s perspective, a class is designed for use by many different custom­ers. In order to be useful in a wide range of applications, a class should provide a variety of ways for customization through constructors, properties, and functions.

The Loan class contains two constructors, three get functions, three set functions, and the functions for finding monthly payment and total payment. You can construct a Loan object by using the no-arg constructor or the one with three parameters: annual interest rate, number of years, and loan amount. The three get functions, getAnnualInterest, getNumberOfYears, and getLoanAmount, return annual interest rate, payment years, and loan amount, respectively.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *