Object-Oriented Thinking in C++: Thinking in Objects

The procedural paradigm focuses on designing functions. The object-oriented paradigm couples data and functions together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects.

This book has introduced fundamental programming techniques for problem solving using loops, functions, and arrays. The study of these techniques lays a solid foundation for object- oriented programming. Classes provide more flexibility and modularity for building reusable software. This section uses the object-oriented approach to improve the solution for a problem introduced in Chapter 3. Observing the improvements, you will gain insight on the differences between the procedural programming and object-oriented programming and see the benefits of developing reusable code using objects and classes.

Listing 3.2, ComputeAndInterpreteBMI.cpp, presented a program for computing body mass index. The program cannot be reused in other programs. To make the code reusable, define a function to compute body mass index as follows:

double getBMI(doub1e weight, double height)

This function is useful for computing body mass index for a specified weight and height. However, it has limitations. Suppose you need to associate the weight and height with a per­son’s name and birth date. You may declare separate variables to store these values. But these values are not tightly coupled. The ideal way to couple them is to create an object that contains them. Since these values are tied to individual objects, they should be stored in instance data fields. You can define a class named BMI, as shown in Figure 10.11.

Figure 10.11 The BMI class encapsulates BMI information.

The BMI class can be defined as in Listing 10.11.

Listing 10.11 BMI.h

1 #ifndef BMI_H
2
#define BMI_H
3
4
#include <string>
5
using namespace std;
6
7
class BMI
8 {
9     
public:

10    BMI(const string& newName, int newAge,
11   
double newWeight, double newHeight);
12    BMI(
const string& newName, double newWeight, double newHeight);
13   
double getBMI() const;
14    string getStatus()
const;
15    string getName()
const;
16   
int getAge() const;
17   
double getWeight() const;
18   
double getHeight() const;
19
20   
private:
21    string name;
22   
int age;
23   
double weight;
24   
double height;
25 };
26
27
#endif

LisTing 10.12 UseBMIClass.cpp

1 #include <iostream>
2
#include “BMI.h”
3 using namespace std;
4
5
int main()
6 {
7    BMI bmi1(
“John Doe”, 18, 145, 70);
8    cout <<
“The BMI for ” << bmi1.getName() << ” is ”
9    << bmi1.getBMI() << ” ” << bmi1.getStatus() << endl;
10
11    BMI bmi2(
“Susan King”, 215, 70);
12    cout <<
“The BMI for ” << bmi2.getName() << ” is ”
13    << bmi2.getBMI() << ” ” + bmi2.getStatus() << endl;
14
15   
return 0;
16 }

Line 7 creates an object bmil for John Doe and line 11 creates an object bmi2 for Susan King. You can use the instance functions getName(), getBMI(), and getStatus() to return the BMI information in a BMI object.

The BMI class can be implemented as in Listing 10.13.

Listing 10.13 BMI.cpp

1 #include <iostream>
2
#include “BMI.h”
3 using namespace std;
4
5 BMI::BMI(
const string& newName, int newAge,
6
double newWeight, double newHeight)
7 {
8    name = newName;
9    age = newAge;
10   weight = newWeight;
11   height = newHeight;
12 }
13
14 BMI::BMI(
const string& newName, double newWeight, double newHeight)
15 {
16    name = newName;
17    age =
20;
18    weight = newWeight;
19    height = newHeight;
20 }
21
22
double BMI::getBMI() const
23 {
24   
const double KILOGRAMS_PER_POUND = 0.45359237;
25   
const double METERS_PER_INCH = 0.0254;
26   
double bmi = weight * KILOGRAMS_PER_POUND /
27      ((height * METERS_PER_INCH) * (height * METERS_PER_INCH));
28   
return bmi;
29 }
30
31 string BMI::getStatus()
const
32 {
33   
double bmi = getBMI();
34   
if (bmi < 18.5)
35       
return “Underweight”;
36   
else if (bmi < 25)
37       
return “Normal”;
38   
else if (bmi < 30)
39       
return “Overweight”;
40   
else
41       return “Obese”;
42 }
43
44 string BMI::getName()
const
45 {
46   
return name;
47 }
48
49
int BMI::getAge() const
50 {
51   
return age;
52 }

53
54
double BMI::getWeight() const
55 {
56   
return weight;
57 }
58
59
double BMI::getHeight() const
60 {
61   
return height;
62 }

The mathematic formula for computing the BMI using weight and height is given in Section 3.7, “Case Study: Computing Body Mass Index.” The instance function getBMI() returns the BMI. Since the weight and height are instance data fields in the object, the getBMI () function can use these properties to compute the BMI for the object.

The instance function getStatus() returns a string that interprets the BMI. The interpre­tation is also given in Section 3.7.

This example demonstrates the advantages of using the object-oriented over the procedural paradigm. The procedural paradigm focuses on designing functions. The object-oriented paradigm couples data and functions together into objects. Software design using the objectoriented paradigm focuses on objects and operations on objects. The object-oriented approach combines the power of the procedural paradigm with an added dimension that integrates data with operations into objects.

In procedural programming, data and operations on the data are separate, and this meth­odology requires sending data to functions. Object-oriented programming places data and the operations that pertain to them within a single entity called an object; this approach solves many of the problems inherent in procedural programming. The object-oriented programming approach organizes programs in a way that mirrors the real world, in which all objects are associated with both attributes and activities. Using objects improves software reusability and makes programs easier to develop and easier to maintain.

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 *