Use the static variable to track total - C++ Class

C++ examples for Class:static member

Description

Use the static variable to track total

Demo Code

                                                                                                      
#include <iostream>
using namespace std;
class RoomDimension
{
   private:/*from   w  ww.ja  va2  s .  c om*/
   static double TotalSqFootage;  // static variable declaration
   double length;
   double width;
   public:
   RoomDimension(double = 0.0, double = 0.0);  // constructor
   void resetDimension(double, double);        // mutator
};
// static variable definition
double RoomDimension::TotalSqFootage = 0.0;
// implementation section
RoomDimension::RoomDimension(double l, double w)  // constructor
{
   length = l;
   width = w;
   TotalSqFootage += l * w;
   cout << "The total square footage is now " << TotalSqFootage << endl;
}
void RoomDimension::resetDimension(double len = 0.0, double wid = 0.0)
{
   TotalSqFootage  = length * width; // remove previous square footage
   length = len;
   width = wid;
   TotalSqFootage += len * wid;  // add new square footage
   cout << "The total square footage is now " << TotalSqFootage << endl;
}
int main()
{
   RoomDimension Kitchen(20.0, 15.0);  // declare RoomDimension object
   RoomDimension Hall(25.0, 4.0);  // declare a second RoomDimension object
   Hall.resetDimension(10, 5);
   return 0;
}

Result


Related Tutorials