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

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

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

#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;
}
时间: 2024-07-30 08:49:21

C++语言基础 例程 类模板的相关文章

C++语言基础 例程 类和对象的简单应用举例

贺老师的教学链接  本课讲解 实例1:求出三角形的周长和面积 #include<iostream> #include<Cmath> #include<cstdlib> using namespace std; class Triangle { public: void setABC(double x, double y, double z);//置三边的值,注意要能成三角形 double perimeter();//计算三角形的周长 double area();//计算

C++语言基础 例程 类的声明和对象的定义

贺老师的教学链接  本课讲解 类的声明和对象的定义-形式1 #include <iostream> #include <cstring> using namespace std; class Student { private: int num; char name[20]; char sex; public: void set_data(int n, char *p,char s) { num=n; strcpy(name,p); sex=s; } void display( )

C++语言基础 例程 函数模板

贺老师的教学链接 使用用函数模板 #include <iostream> using namespace std; template<typename T> //模板声明,其中T为类型参数 T max(T a,T b,T c) //定义一个通用函数,用T作虚拟的类型名 { if(b>a) a=b; if(c>a) a=c; return a; } int main( ) { int i1=185,i2=-76,i3=567; double d1=56.87,d2=90.

C++语言基础 例程 类声明和成员函数定义的分离

贺老师的教学链接  本课讲解 1.一个程序,一个源文件的做法 #include<iostream> #include<cstring> using namespace std; class Student { private: char Name[20]; //学生姓名 double Chinese; //语文成绩 double Math; //数学成绩 public: double Average( );//计算平均成绩 double Sum( ); //计算总分 void Sh

C++语言基础 例程 类的成员函数

贺老师的教学链接  本课讲解 分清"你我" #include <iostream> using namespace std; class Time { public: void set_time(int,int,int); void show_time(); private: int hour; int minute; int sec; }; int main( ) { Time t1,t2; t1.set_time(12,45,32); t2.set_time(21,32

C++语言基础 例程 基类与派生类的转换

贺老师的教学链接  本课讲解 指向基类对象的指针变量也可以指向派生类对象 #include <iostream> #include <string> using namespace std; class Student//声明Student类 { public: Student(int, string,float); void display( ); private: int num; string name; float score; }; Student::Student(in

C++语言基础 例程 派生类的构造函数和析构函数

贺老师的教学链接  本课讲解 一个简单派生类的定义 #include <iostream> #include<cstring> using namespace std; class Student //声明基类Student { public: Student(int n,string nam,char s):num(n),name(nam),sex(s) {} //基类构造函数 ~Student( ) { } //基类析构函数 void show( ) { cout<<

C++语言基础 例程 派生类的声明与构成

贺老师的教学链接  本课讲解 派生类 #include <iostream> #include<string> using namespace std; class Student//声明基类Student { public: void sets(int n,string nam,char s); void show( ); protected: //保护部分 int num; string name; char sex ; }; void Student::sets(int n,

C++语言基础 例程 虚基类及应用

贺老师的教学链接  本课讲解 虚基类应用举例 #include <iostream> #include <cstring> using namespace std; class Person { public: Person(char *nam,char s,int a) //构造函数 { strcpy(name,nam); sex=s; age=a; } protected: //保护成员 char name[20]; char sex; int age; }; class Te