本站電子書

您可以在這些電子書店找到本站電子書: Google Play 圖書iBooks StoreHyReadReadmooPubu
===>>>本站推出學習遊戲意見調查<<<===《如何自學程式設計》入選 Google Play 台灣地區2016年度最佳書籍

C++ 速查手冊 V1.00 - 單元 10 範例




u1001.cpp


#include <iostream>

template <typename T1, typename T2> 
T1 do_something
(const T1& a, const T2& b) {
    return a + b;
}

int main() {
    std::cout << do_something(1, 10)
              << std::endl;
    std::cout << do_something(1, 10.5)
              << std::endl;
    std::cout << do_something(2.5, 6.32)
              << std::endl;

    return 0;
}

/* Kaiching Chang 
   u1001.cpp
   2014-02 */

u1002.cpp


#include <iostream>

template <typename T> class Demo {
public:
   Demo(const T& a, const T& b) {
      this->a = a;
      this->b = b;
   } 

   T do_something() {
      return a + b;
   }
    
private:
   T a;
   T b;
};

int main() {
   Demo<int> d1(20, 25);
   std::cout << d1.do_something()
             << std::endl;
   Demo<double> d2(5.2, 3.95);
   std::cout << d2.do_something()
             << std::endl;
   Demo<char> d3('1', '2');
   std::cout << d3.do_something()
             << std::endl;

   return 0;
}

/* Kaiching Chang 
   u1002.cpp
   2014-02 */

u1003.cpp


#include <iostream>

template <typename T> class Demo {
public:
   Demo(const T& a, const T& b) {
      this->a = a;
      this->b = b;
   } 

   T do_something() {
      return a + b;
   }
    
private:
   T a;
   T b;
};

template <typename T> using D = Demo<T>;

int main() {
   D<int> d1(20, 25);
   std::cout << d1.do_something()
             << std::endl;
   D<double> d2(5.2, 3.95);
   std::cout << d2.do_something()
             << std::endl;
   D<char> d3('1', '2');
   std::cout << d3.do_something()
             << std::endl;

   return 0;
}


/* Kaiching Chang 
   u1003.cpp
   2014-02 */

the end

沒有留言: