Problem E: 类的初体验(V)
来源:互联网 发布:java删除sftp文件夹 编辑:程序博客网 时间:2024/05/23 07:24
HomeWeb BoardProblemSetStandingStatusStatistics
Submit: 1118 Solved: 633
[Submit][Status][Web Board][Submit][Status][Web Board]
Problem E: 类的初体验(V)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1118 Solved: 633
[Submit][Status][Web Board]
Description
定义一个类Data,只有一个int类型的属性和如下方法:
1. 缺省构造函数,将属性初始化为0,并输出"Data's default constructor."。
2. 析构函数,输出:"Data # is erased.",其中“#”是属性值。
4. int getValue()——获得属性值。
5. void setValue(int)——设置属性值。
定义一个由Data的对象组成的数组类DataArray,该类包括1个int类型的属性和一个Data*类型的属性,分别为数组中元素的个数、所有的元素。定义如下方法:
2. 构造函数——参数格式请参考main函数的调用方法给出,输出:"DataArray's constructor."。
3. int getSum()——求所有Data对象属性值的和。
4. 析构函数——输出一行"DataArray's deconstructor."。
Input
若干个整数。个数大于2个。
第1个N表示后面输入的元素个数。之后有N个整数。它们用于设置main函数中DataArray的对象datas的数组元素值。
最后一个整数用于设置main函数中testP指向的对象的值。
Output
见样例~
Sample Input
10 1 2 3 4 5 6 7 8 9 10 1222
Sample Output
Data's default constructor.Data's default constructor.Data's default constructor.Data's default constructor.Data's default constructor.Data's default constructor.Data's default constructor.Data's default constructor.Data's default constructor.Data's default constructor.Data's default constructor.DataArray's constructor.Sum is 55Data's default constructor.Data 1222 is erased.Data 10 is erased.Data 9 is erased.Data 8 is erased.Data 7 is erased.Data 6 is erased.Data 5 is erased.Data 4 is erased.Data 3 is erased.Data 2 is erased.Data 1 is erased.DataArray's deconstructor.Data 0 is erased.
HINT
Append Code
append.cc,
한국어< 中文 فارسی English ไทย
All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin:admin
#include <iostream>using namespace std;class Data{ private: int x; public: Data(int y ):x(y){ } Data(){ x= 0; cout << "Data's default constructor." << endl;} int getValue(){return x;} void setValue(int y){x = y;} ~Data() { cout << "Data " << x << " is erased." <<endl; }};class DataArray{ private: Data *a; int yy; public: DataArray(){cout << "DataArray's default constructor." << endl;} DataArray(int n,int * d) { yy = n; a = new Data[yy]; for(int i = 0; i < yy; i++) { a[i].setValue(d[i]); } cout << "DataArray's constructor." << endl; } int getSum() { int sum ; sum = 0; for(int i = 0; i < yy; i++) { sum += a[i].getValue(); } return sum; } ~DataArray() { delete []a; cout << "DataArray's deconstructor." << endl; }};int main(){ int i, n; Data test, *testP; cin>>n; int tmp[n]; for (i = 0; i < n; i++) cin>>tmp[i]; DataArray datas(n, tmp); cout<<"Sum is "<<datas.getSum()<<endl; testP = new Data; cin>>n; testP->setValue(n); delete testP; return 0;}
0 0
- Problem E: 类的初体验(V)
- Problem C: 类的初体验(V)
- Problem C: 类的初体验(V)
- Problem E: 平面上的点和线——Point类、Line类 (V)
- Problem E: 平面上的点——Point类 (V)
- Problem B: 类的初体验(IV)
- Problem B: 类的初体验(II)
- Problem C: 类的初体验(III)
- Problem D: 类的初体验(IV)
- Problem B: 类的初体验(II)
- Problem C: 类的初体验(III)
- Problem B: 类的初体验(IV)
- Problem A: 类的初体验
- Problem A: 类的初体验
- Vue初体验(二),以 v- 前缀的指令
- YTU-OJ-Problem E: 类的应用
- Problem E: 分数类的乘法
- Problem E: 时间类的错误数据处理
- Problem C: 分数类的输出
- Github全面详解-09 使用issues
- BZOJ3289: Mato的文件管理
- Problem D: 类的初体验(IV)
- python中read() readline()以及readlines()对比
- Problem E: 类的初体验(V)
- hdu杭电1003 dp 连续子序列最大值
- opencv HSV 颜色模型(H通道取值 && CV_BGR2HSV_FULL)
- opencv中hsv
- 数位模17密码
- Codeforces777C 技巧
- hadoop中文件操作
- 第四届省赛题 节能
- 1338: 紧张的考试周 [技巧]