程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c++-C++想要對Box類進行類模版的操作

c++-C++想要對Box類進行類模版的操作

編輯:編程綜合問答
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;
}

最佳回答:


貼了那麼多代碼,簡短截說,你的問題是什麼呢

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved