Typo in my book

From my book:

"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Can.h for Ex9_10
#pragma once
#define _USE_MATH_DEFINES              // For constants in math.h
#include <math.h>
#include "Container.h"                 // For CContainer definition
        
class CCan: public CContainer
{
  public:
    // Function to calculate the volume of a can
    virtual double Volume() const override
    { return 0.25*M_PI*m_Diameter*m_Diameter*m_Height; }
        
    // Constructor
    explicit CCan(double hv = 4.0, double dv = 2.0): m_Height(hv), m_Diameter(dv){}
    
  protected:
    double m_Height;
    double m_Diameter;
};


The CCan class also defi nes a Volume() function based on the formula hπr2 where h is the height of a can and r is the radius of the cross-section. The constant, M_PI, is defined in math.h and becomes available when _USE_MATH_DEFINES is defined. The math.h header defines a wealth of other mathematical constants that you can see if you place the cursor in math.h in the code and press Ctrl+Shift+G. The volume is calculated as the height multiplied by the area of the base. The expression in the function definition assumes a global constant PI is defined, so you have the extern statement indicating that PI is a global variable of type const double that is defined elsewhere — in this program it is defined in the Ex9_10.cpp fi le. Also notice that you redefine the ShowVolume() function in the CBox class, but not in the CCan class. You will see the effect of this in the program output. You can exercise these classes with the following source file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Ex9_10.cpp
// Using an abstract class
#include "Box.h"                       // For CBox and CContainer
#include "Can.h"                       // For CCan (and CContainer)
#include <iostream>                    // For stream I/O
using std::cout;
using std::endl;
        
int main(void)
{
  // Pointer to abstract base class
  // initialized with address of CBox object
  CContainer* pC1 = new CBox(2.0, 3.0, 4.0);
        
  // Pointer to abstract base class
  // initialized with address of CCan object
  CContainer* pC2 = new CCan(6.5, 3.0);
        
  pC1->ShowVolume();                   // Output the volumes of the two
  pC2->ShowVolume();                   // objects pointed to
  cout << endl;
        
  delete pC1;                          // Now clean up ... 
  delete pC2;                          // ... the free store
        
  return 0;
}


"

Ok where is that extern statement, definitely not in Ex9_10.cpp? Infact why is it even needed? Anywhere you mention M_PI is replaced by the actual value of pi...

So what is my book talking about there?
Last edited on
Topic archived. No new replies allowed.