Home Segments Index Top Previous Next

179: Mainline

In the following program, the tank_car class definition includes a default constructor function that initializes the member variables in particular tank_car objects:

#include 
const double pi = 3.14159;
class tank_car {
  public:
    double radius, length;
    // Default constructor:                      
    tank_car ( ) {radius = 3.5; length = 40.0;}  
    // Volume function: 
    double volume ( ) {return pi * radius * radius * length;} 
}; 
main ( ) { 
  tank_car t; 
  cout << "The volume of the tank car is " << t.volume ( ) << endl; 
} 
--- Result --- 
The volume of the tank car is 1539.38