扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:李马编译 来源:VCKBASE 2007年10月19日
关键字:
#include <iostream> using namespace std; template <typename T> T Maximum(const T& a, const T& b) { return a > b ? a : b; } class Point { private: int m_x, m_y; public: Point(int p_x = 0, int p_y = 0) : m_x(p_x), m_y(p_y) { } bool friend operator > (const Point& lhs, const Point& rhs) { return lhs.m_x > rhs.m_x && lhs.m_y > rhs.m_y; } friend ostream& operator << (ostream& os, const Point& p) { return os << "(" << p.m_x << ", " << p.m_y << ")"; } }; int main() { Point a(5, 10), b(15, 20); cout << Maximum(a, b) << endl; return 0; } |
#include <iostream> using namespace std; template <typename T> class Point { private: T m_x, m_y; public: Point(T p_x = 0, T p_y = 0) : m_x(p_x), m_y(p_y) { } bool friend operator > (const Point<T>& lhs, const Point<T>& rhs) { return lhs.m_x > rhs.m_x && lhs.m_y > rhs.m_y; } friend ostream& operator << (ostream& os, const Point<T>& p) { return os << "(" << p.m_x << ", " << p.m_y << ")"; } }; template <typename T, int iSize = 10> class Stack { private: T m_pData[iSize]; int m_iTop; public: Stack() : m_iTop(0) { } void Push(T p_iData) { m_pData[m_iTop++] = p_iData; } T Pop() { return m_pData[--m_iTop]; } T Top() { return m_pData[m_iTop]; } private: Stack(const Stack<T>&); Stack<T>& operator = (const Stack<T>&); }; int main() { Stack<Point<int> > st; st.Push(Point<int>(5, 10)); st.Push(Point<int>(15, 20)); cout << st.Pop() << endl; cout << st.Pop() << endl; return 0; } |
(15, 20) (5, 10) |
template <typename T, int iSize = 10> |
template <typename T = int, int iSize = 10> |
#include <iostream> using namespace std; template <typename T> class Point { private: T m_x, m_y; public: Point(T p_x = 0, T p_y = 0); void Setxy(T p_x, T p_y); T getX() const; T getY() const; friend ostream& operator << (ostream& os, const Point<T>& p) { return os << "(" << p.m_x << ", " << p.m_y << ")"; } }; template <typename T> Point<T>::Point(T p_x, T p_y) : m_x(p_x), m_y(p_y) { } template <typename T> void Point<T>::Setxy(T p_x, T p_y) { m_x = p_x; m_y = p_y; } template <typename T> T Point<T>::getX() const { return m_x; } template <typename T> T Point<T>::getY() const { return m_y; } int main() { Point<int> p; p.Setxy(20, 30); cout << p << endl; return 0; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者