C++语言基础 例程 类模板

简介: 贺老师的教学链接  本课讲解类模板的使用——参数化类#include <iostream>using namespace std;template<class numtype>class Compare{public: Compare(numtype a,numtype b) { x=a; y=b;

贺老师的教学链接  本课讲解


类模板的使用——参数化类

#include <iostream>
using namespace std;
template<class numtype>
class Compare
{
public:
    Compare(numtype a,numtype b)
    {
        x=a;
        y=b;
    }
    numtype max( )
    {
        return (x>y)?x:y;
    }
    numtype min( )
    {
        return (x<y)?x:y;
    }
private:
    numtype x,y;
};


int main( )
{
    Compare<int> cmp1(3,7);
    cout<<cmp1.max( )<<" is the Maximum of two integer numbers."<<endl;
    cout<<cmp1.min( )<<" is the Minimum of two integer numbers."<<endl<<endl;
    Compare<float> cmp2(45.78,93.6);
    cout<<cmp2.max( )<<" is the Maximum of two float numbers."<<endl;
    cout<<cmp2.min( )<<" is the Minimum of two float numbers."<<endl<<endl;
    Compare<char> cmp3('a','A');
    cout<<cmp3.max( )<<" is the Maximum of two characters."<<endl;
    cout<<cmp3.min( )<<" is the Minimum of two characters."<<endl;
    return 0;
}


在类模板外定义成员函数
#include <iostream>
using namespace std;
template<class numtype>
class Compare
{
public:
    Compare(numtype a,numtype b);
    numtype max( );
    numtype min( );
private:
    numtype x,y;
};


template<class numtype>
Compare<numtype>::Compare(numtype a,numtype b)
{
    x=a;
    y=b;
}


template<class numtype>
numtype Compare<numtype>::max( )
{
    return (x>y)?x:y;
}


template<class numtype>
numtype Compare<numtype>::min( )
{
    return (x<y)?x:y;
}


int main( )
{
    Compare<int> cmp1(3,7);
    cout<<cmp1.max( )<<" is the Maximum of two integer numbers."<<endl;
    cout<<cmp1.min( )<<" is the Minimum of two integer numbers."<<endl<<endl;
    Compare<double> cmp2(45.78,93.6);
    cout<<cmp2.max( )<<" is the Maximum of two float numbers."<<endl;
    cout<<cmp2.min( )<<" is the Minimum of two float numbers."<<endl<<endl;
    Compare<char> cmp3('a','A');
    cout<<cmp3.max( )<<" is the Maximum of two characters."<<endl;
    cout<<cmp3.min( )<<" is the Minimum of two characters."<<endl;
    return 0;
}


类库中的模板
#include<vector>
#include <iostream>
using namespace std;
int main()
{
    int i = 0;
    vector<int> v;
    for( i = 0; i < 10; i++ )
    {
        v.push_back(i);//把元素一个一个存入到vector中
    }
    /* v.clear() 对存入的数据清空*/
    for( i = 0; i < v.size(); i++ )//v.size() 表示vector存入元素的个数
    {
        cout << v[i] << "  "; //把每个元素显示出来
    }
    cout << endl;
    return 0;
}


目录
相关文章
|
23天前
|
编译器 C++
【C++】——初识模板
【C++】——初识模板
30 1
【C++】——初识模板
|
2天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
42 30
|
16天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
16天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。
|
16天前
|
存储 设计模式 编译器
C++(十三) 类的扩展
本文详细介绍了C++中类的各种扩展特性,包括类成员存储、`sizeof`操作符的应用、类成员函数的存储方式及其背后的`this`指针机制。此外,还探讨了`const`修饰符在成员变量和函数中的作用,以及如何通过`static`关键字实现类中的资源共享。文章还介绍了单例模式的设计思路,并讨论了指向类成员(数据成员和函数成员)的指针的使用方法。最后,还讲解了指向静态成员的指针的相关概念和应用示例。通过这些内容,帮助读者更好地理解和掌握C++面向对象编程的核心概念和技术细节。
|
7天前
|
JavaScript 前端开发 测试技术
一个google Test文件C++语言案例
这篇文章我们来介绍一下真正的C++语言如何用GTest来实现单元测试。
9 0
|
16天前
|
存储 C++
C++(五)String 字符串类
本文档详细介绍了C++中的`string`类,包括定义、初始化、字符串比较及数值与字符串之间的转换方法。`string`类简化了字符串处理,提供了丰富的功能如字符串查找、比较、拼接和替换等。文档通过示例代码展示了如何使用这些功能,并介绍了如何将数值转换为字符串以及反之亦然的方法。此外,还展示了如何使用`string`数组存储和遍历多个字符串。
|
25天前
|
存储 C++
C++ dll 传 string 类 问题
C++ dll 传 string 类 问题
16 0
|
1月前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
32 0
|
1月前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)