li945 2015-04-19 09:30 采纳率: 100%
浏览 1728
已采纳

C++想要对Box类进行类模版的操作

#include
using namespace std;
class Box
{
private:
int a, b, c;
public:
int V;
Box(int ch,int k,int g)
{
a = ch;
b = k;
c = g;
V = a*b*c;
}
bool operator <(Box &one)
{
int temp = V - one.V;
if (temp < 0)
return true;
else
return false;
}
int operator +(Box &one)
{
return V + one.V;
}
bool operator ==(Box &one)
{
if (V == one.V)
return true;
else
return false;
}
};
template
class Array
{
private:
int size;
T* element;
public:
Array();
~Array(){ delete[]element; }
void Arrayset();
void sore();
void find();
T add();
int add1()
{
int sum1 = 0;
for (int i = 0; i < size; i++)
{
sum1 = sum1 + element[i];
}
return sum1;
}
};
template
void Array::Arrayset()
{
cout << "输入数组长度:" << endl;
cin >> size;
element = new T[size];
cout << "输入数组成员:" << endl;
for (int i = 0; i < size; i++)
{
cin >> element[i];
}
}
template
void Array::sore()
{
int i, j;
T temp;
for (i = 0 ; i < size - 1; i++)
for (j = 0; j < size - 1 - i; j++)
{
if (element[j]>element[j + 1])
{
temp = element[j];
element[j] = element[j + 1];
element[j + 1] = temp;
}
}
for (int i2 = 0; i2 < size; i2++)
{
cout << element[i2] ;
}
}
template
void Array::find()
{
int p = 0;
T num;
cout << "输入要找的数:" << endl;
cin >> num;
for (int i = 0; i < size; i++)
{
if (element[i] == num)
{
cout << "该成员对应的下标为:" << (i + 1) << endl;
p = 1;
}
}
if (p!=1)
cout << "不存在!!" << endl;
}
template
T Array::add()
{
T he=0;
for (int i = 0; i < size; i++)
{
he = he + element[i];
}
return he;
}
int main()
{
/*ArrayS1;
S1.Arrayset();
cout << "排序:";
S1.sore();
cout << endl;
S1.find();
cout << "求和:";
cout << S1.add();
cout << endl;
cout << "======== ========" << endl;*/
ArrayS2;
/*cout << "排序:";
S2.sore();
cout << endl;
S2.find();
cout << "求和:";
cout << S2.add1();
cout << endl;*/
return 0;
}

  • 写回答

3条回答 默认 最新

  • devmiao 2015-04-19 09:37
    关注

    贴了那么多代码,简短截说,你的问题是什么呢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 为啥快手广告联盟的广告这么难出来
  • ¥15 k8s集群重启后,kubelet一直报systemctl restart kubelet.service "Failed to delete cgroup paths"
  • ¥15 微信小程序构建工具类发布到npm,install后报错
  • ¥15 amesim液压仿真问题
  • ¥15 MATLAB优化设计
  • ¥15 ANN预测模型 200大米
  • ¥20 优秀学生的问题 oj java
  • ¥15 scratch-gui 如何集成进 react native
  • ¥15 把全局悬浮窗改为局内悬浮窗
  • ¥15 Excel vba编程调用word如何读取换行符