// A2.2.cpp : Defines the entry point for the console application. // // Please provide the definition of a template-based class that provides // access to a shared value of some type, specified when the using // application is compiled. Wherever an object of this type is declared // it provides access to the same value of the template type. #include "stdafx.h" #include using namespace std; template class transporter { public: transporter() {} T& value() { return val; } private: static T val; }; template T transporter::val; template class alternateTransporter { public: alternateTransporter() {} T& value() { return val; } private: static T val; }; template T alternateTransporter::val; ostream& operator<<(ostream& out, std::string& s) { out << s.c_str(); return out; } int _tmain(int argc, _TCHAR* argv[]) { cout << "\n Exam A2 - Question #2 " << "\n =======================\n"; transporter t1; t1.value() = 3.1415927; transporter t2; cout << "\n value transported from t1 to t2 = " << t2.value(); // Note that any module that includes the transporter class declaration // will have access to this value. // // We can provide access to more than one value of type T by defining // template alternateTransporter tstring11; tstring11.value() = "first shared string"; alternateTransporter tstring12; cout << "\n string value transported from tstring11 to tstring12 = \"" << tstring12.value() << "\""; alternateTransporter tstring21; tstring21.value() = "second shared string"; alternateTransporter tstring22; cout << "\n string value transported from tstring21 to tstring22 = \"" << tstring22.value() << "\""; cout << "\n\n"; return 0; }