Description
補充完成類numCal的定義
(注意:所有運算符的重載都必須符合運算符原本的語義,比如++A是“先加後使用”) class numCal { public: numCal(int a[], int size); //構造函數,size是數組a的長度 numCal(const numCal& other) //拷貝構造函數 ~numCal(); //析構函數,要完成釋放內存的操作 int sum(); //求動態數組arr中size個元素值和 numCal& operator=(const numCal& other) //=運算符重載 numCal& operator++(); //前序++運算符重載,使得++A後,A.arr的每個元素值加1(A爲numCal類的對象) numCal operator++(int); //後序++運算符重載,使得A++後,A.arr的每個元素值加1(A爲numCal類的對象) //add any other function or property needed here private: int size; //數組arr元素個數 int *arr; //動態數組 }; 調用示例: int main() { int a[3] = {1, 3, 5}; numCal n1(a, 3); cout << n1.sum() << endl; numCal n2(n1); cout << n2.sum() << endl; int b[4]={2, 4, 6, 8}; numCal n3(b,4); n2=n3; cout << (n2++).sum() << endl; cout << n2.sum() << endl; cout << (++n2).sum() << endl; cout << n2.sum() << endl; n2 = n2; return 0; } 輸出結果: 9 9 20 24 28 28 注意:你只需要提交類定義。 Problem Source: 12級期末機考題 |
一發WA:(*@ο@*) 作大死,一小時了還是WA;
#include<iostream>
using namespace std;
class numCal {
public:
numCal(int a[], int size){ //構造函數,size是數組a的長度
arr = a;
/*for (int i = 0; i < size; i++){
arr[i] = a[i];
}*/
this->size = size;
}
numCal(const numCal& other){ //拷貝構造函數
this->arr = other.arr;
this->size = other.size;
}
~numCal(){//析構函數,要完成釋放內存的操作
}
int sum(){ //求動態數組arr中size個元素值和
int sum = 0;
for (int i = 0; i<size; i++){
sum += arr[i];
}
return sum;
}
numCal& operator=(const numCal& other){ //=運算符重載
size = other.size;
arr = other.arr;
return *this;
}
numCal& operator++(){ //前序++運算符重載,使得++A後,A.arr的每個元素值加1(A爲numCal類的對象)
for (int i = 0; i<size; i++){
arr[i]+=1;
}
return *this;
}
numCal operator++(int ize){//後序++運算符重載,使得A++後,A.arr的每個元素值加1(A爲numCal類的對象)
numCal temp(*this);
for (int i = 0; i<size; i++){
++arr[i];
}
cout << (*this).sum() << endl;
return temp;
}
//add any other function or property needed here
private:
int size; //數組arr元素個數
int *arr; //動態數組
};
int main(){
int a[3] = { 1, 3, 5 };
numCal n1(a, 3);
cout << n1.sum() << endl;
numCal n2(n1);
cout << n2.sum() << endl;
int b[4] = { 2, 4, 6, 8 };
numCal n3(b, 4);
n2 = n3;
cout << (n2++).sum() << endl;
cout << (n2).sum() << endl;
cout << (++n2).sum() << endl;
cout << n2.sum() << endl;
n2 = n2;
system("pause");
return 0;
}
最終AC:構造函數出現問題
#include<iostream>
using namespace std;
class numCal {
public:
numCal(int a[], int size1)
{
int*list = new int;
this->size = size1;
for (int i = 0; i < size1; i++)
{
list[i] = a[i];
}
arr = list;
}
numCal(const numCal& other)
{
this->size = other.size;
int* list = new int;
for (int i = 0; i < size; i++)
{
list[i] = other.arr[i];
}
this->arr = list;
}
~numCal(){//析構函數,要完成釋放內存的操作
}
int sum(){ //求動態數組arr中size個元素值和
int sum = 0;
for (int i = 0; i<size; i++){
sum += arr[i];
}
return sum;
}
numCal& operator=(const numCal& other){ //=運算符重載
size = other.size;
arr = other.arr;
return *this;
}
numCal& operator++(){ //前序++運算符重載,使得++A後,A.arr的每個元素值加1(A爲numCal類的對象)
for (int i = 0; i<size; i++){
arr[i] += 1;
}
return *this;
}
numCal operator++(int ize){//後序++運算符重載,使得A++後,A.arr的每個元素值加1(A爲numCal類的對象)
numCal temp(*this);
for (int i = 0; i<size; i++){
++arr[i];
}
return temp;
}
//add any other function or property needed here
private:
int size; //數組arr元素個數
int *arr; //動態數組
};
conclusion:=_=醉了,做了4個多小時才ac所有的題目。。還百度了很多問題,期末要掛的感覺。開始擼期末理論試題