home *** CD-ROM | disk | FTP | other *** search
/ Master Visual C++ 1.5 / MASTERVC15.ISO / vcprog / original / ch02 / circle4.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-29  |  2.7 KB  |  157 lines

  1. /////////////////////////////
  2. // Program Name: Circle4.CPP
  3. /////////////////////////////
  4.  
  5. ///////////////////
  6. // #include files
  7. ///////////////////
  8. #include <iostream.h>
  9.  
  10. // Declare the CCircle class
  11. class CCircle
  12. {
  13. public:
  14.   CCircle( int r);    // Constructor
  15.  
  16.   void   SetRadius(int r);           // Overloaded
  17.   void   SetRadius(int r, int c );   // Overloaded
  18.  
  19.   int    GetRadius(void);
  20.   void   DisplayArea(void);
  21.   ~CCircle();         // Destructor
  22.  
  23.   int m_Color;
  24.  
  25. private:
  26.   float CalculateArea(void);
  27.   int m_Radius;
  28.   // int Color;
  29. };
  30.  
  31.  
  32. ///////////////////////////
  33. // The constructor function
  34. ///////////////////////////
  35. CCircle::CCircle ( int r )
  36. {
  37.  
  38. // Set the radius
  39. m_Radius = r;
  40. m_Color = 0;
  41.  
  42. }
  43.  
  44.  
  45. //////////////////////////
  46. // The destructor function
  47. //////////////////////////
  48. CCircle::~CCircle ()
  49. {
  50.  
  51.  
  52. }
  53.  
  54.  
  55. //////////////////////////////
  56. // Function Name: SetRadius()
  57. //////////////////////////////
  58. void CCircle::SetRadius ( int r)
  59. {
  60.  
  61. m_Radius = r;
  62. m_Color = 255;
  63.  
  64. }
  65.  
  66.  
  67. //////////////////////////////
  68. // Function Name: SetRadius()
  69. //////////////////////////////
  70. void CCircle::SetRadius ( int r, int c)
  71. {
  72.  
  73. m_Radius = r;
  74. m_Color = c;
  75.  
  76. }
  77.  
  78.  
  79. //////////////////////////////
  80. // Function Name: GetRadius()
  81. //////////////////////////////
  82. int CCircle::GetRadius ( void)
  83. {
  84.  
  85. return m_Radius;
  86.  
  87. }
  88.  
  89. ///////////////////////////////
  90. // Function Name: DisplayArea()
  91. ///////////////////////////////
  92. void CCircle::DisplayArea ( void )
  93. {
  94.  
  95. float fArea;
  96.  
  97. fArea = CalculateArea ( );
  98.  
  99. // Print the area
  100. cout << "The area of the circle is: " << fArea;
  101.  
  102.  
  103. }
  104.  
  105.  
  106. /////////////////////////////////
  107. // Function Name: CalculateArea()
  108. /////////////////////////////////
  109. float CCircle::CalculateArea ( void )
  110. {
  111.  
  112. float f;
  113.  
  114. f = (float) (3.14 * m_Radius * m_Radius);
  115.  
  116. return f;
  117.  
  118. }
  119.  
  120.  
  121.  
  122. void main ( void)
  123. {
  124. // Create the MyCircle object with Radius equal to 10
  125. CCircle MyCircle (10);
  126.  
  127. // Display the radius of the circle
  128. cout << "The Radius is: " << MyCircle.GetRadius();
  129. cout << "\n";
  130.  
  131. // Display the color of the circle
  132. cout << "The Color of the circle is: " << MyCircle.m_Color;
  133. cout << "\n";
  134.  
  135. //Set the Radius of MyCircle to 20.
  136. MyCircle.SetRadius (20);
  137.  
  138. // Display the radius of the circle
  139. cout << "The Radius is: " << MyCircle.GetRadius();
  140. cout << "\n";
  141.  
  142. // Display the color of the circle
  143. cout << "The Color of the circle is: " << MyCircle.m_Color;
  144. cout << "\n";
  145.  
  146. // Use the other SetRadius() function
  147. MyCircle.SetRadius (40, 100);
  148.  
  149. // Display the radius of the circle
  150. cout << "The Radius is: " << MyCircle.GetRadius();
  151. cout << "\n";
  152.  
  153. // Display the color of the circle
  154. cout << "The Color of the circle is: " << MyCircle.m_Color;
  155.  
  156. }
  157.