






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







//---- start of the required answer --------------------------- template class Vector { public: Vector(int n); Vector(const Vector& v); ~Vector(); Vector& operator=(const Vector& v); // v1 = v Vector& operator=(const T& t); // v1 = t Vector operator(const T&t); // v1 = v2t Vector& operator+=(const Vector& v); // v1 += v Vector operator+(const Vector& v); // v1 = v2 + v Vector& operator-=(const Vector& v); // v1 -= v Vector operator-(const Vector& v); // v1 = v2 – v T& operator[](int n); // v1[3] = t T operator[](int n) const; // t = v1[2] private: int _numElem; T* array; }; //----< global function v1 = tv2 >------------------------- Vector operator(const T& t, const Vector& v); //---- end of the required answer ----------------------------- template Vector::Vector(int n) : array(new T[n]) {} template Vector::~Vector() { delete [] array; } // other members defined here int main() { Vector v(10); // statements using v } (^1) There are n elements in an n-dimensional vector.
//---- start of required answer -------------------- template class shared { public: static T& value(R &r) { pR = &r); template void register(R &r); private: static T _value; R* pR; }; template T shared::_value; template T& shared::value(R &r) { if(pR != &r) { // if access is not allowed you can throw an exception // or return a static dummy value of type T } return _value; } //---- end of required answer ------------------------- #include using namespace std; void someFunction() { shared sd; // use the shared value cout << "\n getting shared value = " << sd.value(); } int main() { shared sd; cout << "\n setting shared value to " << (sd.value() = 2.5); someFunction(); cout << "\n\n"; }
class A { str say() { return "class A"; } }; class B { public: virtual ~B() {}; str say() { return typeid(*this).name(); } }; class D : public B { public: A a; }; void fun1(B b[], int size) { for(int i=0; i
#include #include typedef std::string str; //---- start of required answer ---------------------- class caser { public: caser(str &change) : _cased(change) {} void up() { for(int i=0; i<_cased.length(); ++i) _cased[i] = toupper(_cased[i]); } void down() { for(int i=0; i<_cased.length(); ++i) _cased[i] = tolower(_cased[i]); } private: str& _cased; }; //---- end of required answer -------------------------- int main() { str change = "This Will Be Changed To All One Case"; std::cout << "\n " << change; std::cout << "\n " << (caser(change).up(), change); std::cout << "\n " << (caser(change).down(), change) << "\n\n"; }
(^3) This is the one case where a header file without a corresponding implementation file is an acceptable structure for a modular system.