C++考试题带部分答案函数模板

1写出下面程序的运行结果。

#include<iostream>   

using namespace std;

template <class Type1,class Type2>

class myclass{

public:

myclass(Type1 a,Type2 b)

{ i=a; j=b;

}

void show()

{ cout<<i<< <<j<<\n;

}

private:

Type1 i;

Type2 j;

};

int main()

{ myclass<int,double> ob1(10,0.23);

myclass<char,char*> ob2(X,"This is a test.");

ob1.show();    

ob2.show();    

return 0;

}

8.13写出下面程序的运行结果。

#include<iostream>

using namespace std;

template<class T>

T min(T a,T b)

{

  if(a<b)return a;

  else return b;

}

int main()

{ int n1=5,n2=81;

  double d1=1.3,d2=5.6;

  cout<<"较小整数:"<<min(n1,n2)<<endl;

  cout<<"较小实数:"<<min(d1,d2)<<endl;

  return 0;

}

8.14写出下面程序的运行结果。

#include<iostream.h>

template<class T>

class Sample{

    T d;

  public:

    Sample(){}

    Sample(T i){d=i;}

    void disp(){cout<<"d="<<d<<endl;}

    friend Sample<T>operator+(Sample<T>s1,Sample<T>s2)

    {

      Sample<T>tmp;

      tmp.d=s1.d+s2.d;

    return tmp;

    }

  };

  void main()

  {

    Sample<int>s1(2),s2(3),s3;

    Sample<double>s4(2.5),s5(8.4),s6;

    s3=s1+s2;

 &n

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值