Template Class

lecture


create the template class

demo

it has one private data member 'data'

it is a Template type

there are public functions

getData // return the value data 
setData // set the value of data


friend std::ostream& operator << (std::ostream& os,  demo& s)
// push the text representation of the content to the stream
// this should work just like the ToString functions we have been doing all quarter
// except you will use the stream passed in "os" instead of creating a stringstream
// return os


NOTE it is WAY EASIER to put your function bodies inside the class... 



there is a function in the cmpslib GetClassName that should allow you to determine the datatype at runtime

you will then test your class with "main.cpp" by creating instances of the demo class for type int, double and string

your output should match the runable example and the output below

name your file "template_class.h"


test your differnt types like so 
cout << "Testing with type int" << endl;
demo<int> a;
cout << "a.setData(99);";
a.setData(99);
cout << "a.getData() returns" << a.getData();
cout << a;



 



./runme Testing with type int a.setData(99) a.getData() returns: 99 cout << a sizeof (data) = 4 data is of type int data contains: 99 Testing with type double b.setData(99.99) b.getData() returns: 99.99 cout << b sizeof (data) = 8 data is of type double data contains: 99.99 Testing with type string c.setData("monkey") c.getData() returns: monkey cout << c sizeof (data) = 8 data is of type std::string // the longhand version is (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) data contains: monkey