Home Segments Index Top Previous Next

387: Mainline

The following program creates an array of three cylinder objects, wires in data via assignment statements, and accesses the data in an output statement:

#include   
const double pi = 3.14159; 
// Define the cylinder class: 
class cylinder { 
  public: double radius, length; 
          double volume ( ) {return pi * radius * radius * length;} 
}; 
// Define cylinder array: 
cylinder oil_tanks[3]; 
main ( ) { 
  // Wire in sample data: 
  oil_tanks[0].radius = 4.7; oil_tanks[0].length = 40.0; 
  oil_tanks[1].radius = 3.5; oil_tanks[1].length = 35.5; 
  oil_tanks[2].radius = 3.5; oil_tanks[2].length = 45.0; 
  // Display volumes: 
  cout << "The volumes of the cylinders in the oil-tank array are" 
       << endl 
       << oil_tanks[0].volume ( ) << ", " 
       << oil_tanks[1].volume ( ) << ", and " 
       << oil_tanks[2].volume ( ) << endl; 
} 
--- Result --- 
The volumes of the cylinders in the oil-tank array are 
2775.91, 1366.2, and 1731.8