// static2.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program is similar to static1.cpp, except that it does not declare the private // variable "totalprice" as static. What difference did this change make? // Key Points: 1) "static" objects #include "static2.h" Vehicle::Vehicle( double pr ) { price = pr; totalprice = 0; } double Vehicle::GetPrice( void ) { return price; } void Vehicle::SetPrice( double pr ) { price = pr; } double Vehicle::GetTotalPrice( void ) { return totalprice; } void Vehicle::UpdateTotalPrice( void ) { static int numofupdates = 0; // Notice that this local variable still static totalprice += price; // Increment the total price by the price of the // object calling the function numofupdates++; cout << " The total number of updates to the total price at this point is: " << numofupdates << endl; } int main() { Vehicle v1(5000), v2(10000), v3(30000); v1.UpdateTotalPrice(); cout << " The price of v1 is: " << v1.GetPrice() << ",and the total price of the vehicles at this point is: " << v1.GetTotalPrice() << endl << endl; v2.UpdateTotalPrice(); cout << " The price of v2 is: " << v2.GetPrice() << " The total price of the vehicles at this point is: " << v2.GetTotalPrice() << endl << endl; v3.UpdateTotalPrice(); cout << " The price of v3 is: " << v3.GetPrice() << " The total price of the vehicles at this point is: " << v3.GetTotalPrice() << endl << endl; return 0; } // The result of running the program: /* The total number of updates to the total price at this point is: 1 The price of v1 is: 5000,and the total price of the vehicles at this point is: 5000 The total number of updates to the total price at this point is: 2 The price of v2 is: 10000 The total price of the vehicles at this point is: 10000 The total number of updates to the total price at this point is: 3 The price of v3 is: 30000 The total price of the vehicles at this point is: 30000 */