50 lines
996 B
C++
50 lines
996 B
C++
/*
|
|
Question #1
|
|
|
|
Write a class template named Triad that has 3 private data members with
|
|
independent type template parameters. The class should have a constructor,
|
|
access functions, and a print() member function that is defined outside the
|
|
class.
|
|
|
|
The following program should compile and run:
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
template <typename T1, typename T2, typename T3>
|
|
class Triad {
|
|
private:
|
|
T1 t1;
|
|
T2 t2;
|
|
T3 t3;
|
|
|
|
public:
|
|
explicit Triad() {
|
|
}
|
|
explicit Triad(T1 t1, T2 t2, T3 t3) : t1(t1), t2(t2), t3(t3) {
|
|
}
|
|
T1 first() { return t1; }
|
|
void print() ;
|
|
~Triad() {
|
|
}
|
|
};
|
|
|
|
template <typename T1, typename T2, typename T3>
|
|
void Triad<T1, T2, T3>::print() {
|
|
std::cout << '[' << t1 << ", " << t2 << ", " << t3 << "]\n";
|
|
}
|
|
|
|
int main() {
|
|
Triad t1{1, 2, 3};
|
|
t1.print();
|
|
std::cout << '\n';
|
|
std::cout << t1.first() << '\n';
|
|
|
|
using namespace std::literals::string_literals;
|
|
const Triad t2{1, 2.3, "Hello"s};
|
|
t2.print();
|
|
std::cout << '\n';
|
|
|
|
return 0;
|
|
}
|