// B2.cpp - provide client access to a common set of data with // data validation. // // This is a fairly long example illustrating a fairly simple idea. // The answer to the question is simply: // make the common data static members of a class and provide // static member functions to check for valid operations // on that data. These ideas are illustrated by the commonData // class defined below. I've extended the explaination to show // how to provide sets of data shared between several sets of // clients, by using integer templatized arguments. The idea is // this: Each integer template value defines a new class, so the // static data for each integer is shared by all clients of that // class, but not by clients of a class with a different integer // template value. #include using namespace std; template class commonData { public: static int getSales() { return annualSales; } static void setSales(int sales) { if(sales > 0) annualSales += sales; } static double getIncome() { return netIncome; } static void setIncome(double income) { if(income > 0) netIncome += income; } static double getCosts() { return totalCost; } static void setCosts(double cost) { if(cost > 0) totalCost += cost; if(totalCost >= netIncome) cout << "\n Big trouble in River City!"; } private: static int annualSales; static double netIncome; static double totalCost; }; // template int commonData::annualSales = 0; template double commonData::netIncome = 0; template double commonData::totalCost = 0; template class dataServer { public: commonData& data() { return serverData; } double profit() { return (serverData.getIncome() - serverData.getCosts()); } private: commonData serverData; }; void main() { dataServer<1> serverA1; commonData<1> client1data = serverA1.data(); client1data.setSales(1000); client1data.setIncome(50000.00); client1data.setCosts(38000.00); dataServer<1> serverB1; commonData<1> client2data = serverB1.data(); client2data.setSales(500); client2data.setIncome(25000.00); client2data.setCosts(33000.00); // servers A1 and B1 share a common data set so client1 and client2 // both work on shared data. cout << "\n profit reported by server A1 = " << serverA1.profit(); cout << "\n profit reported by server B1 = " << serverB1.profit(); cout << "\n\n"; // dataServer<2> serverA2; commonData<2> client3data = serverA2.data(); client3data.setSales(10000); client3data.setIncome(500000.00); client3data.setCosts(550000.00); dataServer<2> serverB2; commonData<2> client4data = serverB2.data(); client4data.setSales(50); client4data.setIncome(2500.00); client4data.setCosts(3300.00); // servers A2 and B2 share a common data set so client3 and client4 // both work on shared data. cout << "\n profit reported by server A2 = " << serverA2.profit(); cout << "\n profit reported by server B2 = " << serverB2.profit(); cout << "\n\n"; // changes to servers A2 and B2 data did not affect that of A1 and A2. cout << "\n profit reported by server A1 = " << serverA1.profit(); cout << "\n profit reported by server B1 = " << serverB1.profit(); cout << "\n\n"; }