How to use class template with multiple and default parameters in C++

1 Answer

0 votes
#include <iostream>
 
template <class T, class U, class V = char>
class Example {
    private:
        T a;
        U b;
        V c;

   public:
        Example(T val1, U val2, V val3) : a(val1), b(val2), c(val3) {}  // constructor

    void print() {
        std::cout << "a = " << a << std::endl;
        std::cout << "b = " << b << std::endl;
        std::cout << "c = " << c << std::endl;
    }
};

int main() {
    Example<int, double> obj1(98, 3.14, 'w');
    obj1.print();

    Example<double, char, bool> obj2(5.891, 'p', true);
    obj2.print();
}
 
 
 
 
/*
run:
 
10 25
 
*/

 



answered Dec 6, 2022 by avibootz

Related questions

1 answer 221 views
1 answer 128 views
1 answer 174 views
2 answers 185 views
2 answers 278 views
1 answer 225 views
1 answer 203 views
...