Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- template<class T, class U>
- //////////////////////////////////////////////////////
- class MyClass
- {
- T value_;
- U anotherValue_;
- public:
- MyClass(T t, U u) : value_(t), anotherValue_(u) {}
- T getValue() //const
- {
- return value_;
- }
- U getAnotherValue() const { return anotherValue_; }
- private:
- };
- ///////////////////////////////////////////////////////
- int main() //
- {
- MyClass<int, string> myObject(42, "Hello, world!");
- cout << myObject.getValue() << endl;
- cout << myObject.getAnotherValue() << endl;
- MyClass<float, int> Dima(3.14, 777);
- cout << Dima.getValue() << endl;
- cout << Dima.getAnotherValue() << endl;
- return 0;
- }
- /*
- // Help
- template <typename T>
- class Person
- */
- /*
- #include <iostream>
- using namespace std;
- template <typename T>
- ///////////////////////////////////////////////////////
- class Person
- {
- private: T id;
- string name;
- public:
- Person()
- {
- }
- Person(T T_id, string str) // : id{id}, name{name}
- {
- id = T_id;
- name = str;
- }
- void set_name(string str)
- {
- name = str;
- }
- void set_id(T T_id);
- //{
- // id = T_id;
- //}
- void print() const
- {
- cout << "Id: " << id << "\tName: " << name << endl;
- }
- };
- ////////////////////////////////////////////////////////
- int main() //
- {
- Person <int>tom(123456, "Tom"); // T - число
- tom.print(); // Id: 123456 Name: Tom
- Person <float>Dima;
- Dima.set_name("Dmitry");
- Dima.set_id (0.47);
- Dima.print();
- Person <string>bob("tvi4xhcfhr", "Bob"); // T - строка
- bob.print(); // Id: tvi4xhcfhr Name: Bob
- }
- template <typename T>
- ////////////////////////////////////////////////////////
- void Person<T>::set_id(T T_id) //
- {
- id = T_id;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement