学习C++ 模板类
2015-06-25 16:31
411 查看
#include<iostream>
#include<typeinfo>
#include<cstring>
using namespace std;
class A
{
public:
A(int x, int y) :m_x(x), m_y(y)
{
}
int compare(void)
{
cout << "Type of m_x is" << typeid (m_x).name() << endl;
cout << "Type of m_y is" << typeid (m_y).name() << endl;
if (m_x < m_y)
return -1;
if (m_x > m_y)
return 1;
return 0;
}
private:
int m_x;
int m_y;
};
class B
{
public:
B(double x, double y) :m_x(x), m_y(y)
{
}
int compare(void)
{
cout << "Type of m_x is" << typeid (m_x).name() << endl;
cout << "Type of m_y is" << typeid (m_y).name() << endl;
if (m_x < m_y)
return -1;
if (m_x > m_y)
return 1;
return 0;
}
private:
double m_x;
double m_y;
};
class C
{
public:
C(string x, string y) :m_x(x), m_y(y)
{
}
int compare(void)
{
cout << "Type of m_x is" << typeid (m_x).name() << endl;
cout << "Type of m_y is" << typeid (m_y).name() << endl;
if (m_x < m_y)
return -1;
if (m_x > m_y)
return 1;
return 0;
}
private:
string m_x;
string m_y;
};
template < class T> class D
{
public:
D(T x, T y) : m_x(x), m_y(y)
{
}
int compare(void)
{
cout << "Type of m_x is" << typeid (m_x).name() << endl;
cout << "Type of m_y is" << typeid (m_y).name() << endl;
if (m_x < m_y)
return -1;
if (m_x > m_y)
return 1;
}
private:
T m_x;
T m_y;
};
int main(int agrc, char* argv[])
{
// 不用模板
int r;
A a(10, 20);
cout << "A:: Compare() return " << a.compare() << endl;
B b(0.1, 0.2);
cout << "B:: Compare() return " << b.compare() << endl;
C c("pan", "tan");
cout << "C:: Compare() return " << c.compare() << endl;
cout << "====================================" << endl;
//用模板类
D<int> d1(10, 20);
cout << "A:: Compare() return use panel " << d1.compare() << endl;
D <double> d2(0.1, 0.3);
cout << "B:: Compare() return use panel" << d2.compare() << endl;
cin >> r;
}
#include<typeinfo>
#include<cstring>
using namespace std;
class A
{
public:
A(int x, int y) :m_x(x), m_y(y)
{
}
int compare(void)
{
cout << "Type of m_x is" << typeid (m_x).name() << endl;
cout << "Type of m_y is" << typeid (m_y).name() << endl;
if (m_x < m_y)
return -1;
if (m_x > m_y)
return 1;
return 0;
}
private:
int m_x;
int m_y;
};
class B
{
public:
B(double x, double y) :m_x(x), m_y(y)
{
}
int compare(void)
{
cout << "Type of m_x is" << typeid (m_x).name() << endl;
cout << "Type of m_y is" << typeid (m_y).name() << endl;
if (m_x < m_y)
return -1;
if (m_x > m_y)
return 1;
return 0;
}
private:
double m_x;
double m_y;
};
class C
{
public:
C(string x, string y) :m_x(x), m_y(y)
{
}
int compare(void)
{
cout << "Type of m_x is" << typeid (m_x).name() << endl;
cout << "Type of m_y is" << typeid (m_y).name() << endl;
if (m_x < m_y)
return -1;
if (m_x > m_y)
return 1;
return 0;
}
private:
string m_x;
string m_y;
};
template < class T> class D
{
public:
D(T x, T y) : m_x(x), m_y(y)
{
}
int compare(void)
{
cout << "Type of m_x is" << typeid (m_x).name() << endl;
cout << "Type of m_y is" << typeid (m_y).name() << endl;
if (m_x < m_y)
return -1;
if (m_x > m_y)
return 1;
}
private:
T m_x;
T m_y;
};
int main(int agrc, char* argv[])
{
// 不用模板
int r;
A a(10, 20);
cout << "A:: Compare() return " << a.compare() << endl;
B b(0.1, 0.2);
cout << "B:: Compare() return " << b.compare() << endl;
C c("pan", "tan");
cout << "C:: Compare() return " << c.compare() << endl;
cout << "====================================" << endl;
//用模板类
D<int> d1(10, 20);
cout << "A:: Compare() return use panel " << d1.compare() << endl;
D <double> d2(0.1, 0.3);
cout << "B:: Compare() return use panel" << d2.compare() << endl;
cin >> r;
}
相关文章推荐
- 指针
- C++ stringstream 用法
- gdb调试c++的STL容器
- C++入门学习——标准模板库之vector
- Effective C++ 条款19
- Effective C++ 条款18
- C++获取文件路径
- 值得推荐的C/C++框架和库
- 经典开源C/C++项目
- vs2010 C++常见问题1:back_inserter
- 搭建Eclipse C/C++开发环境
- Unreal Engine4 C++基础代码项目编译失败原因小结
- C语言文件操作
- 网狐_经典版机器人添加说明
- C++栈和队列
- C++文件处理的操作方式
- int类型取值范围
- 基于C++11线程池
- C语言----内存区划分
- c++ 关于换行符