1、东南大学09级C+(下)上机试卷B-答案一、改错题(50分)本题共10个错误,每个错误5分#include #include using namespace std;class studentchar *pName;public:student();student(char *pname);student(student &s);student();student & operator=(student &s);void print();student:student()/去掉voidcoutConstructor;pName=NULL;cout缺省endl;student:student (
2、 char *pname )coutConstructor;pName = new charstrlen(pname);if ( pName ) strcpy ( pName, pname);/pName与pname互换位置coutpNameendl;student:student(student &s) /改为student &scoutCopy Constructor;if(s.pName)int len = strlen( s.pName );pName = new charlen+1; /len改为len+1if ( pName ) strcpy (pName, s.pName);co
3、utpNameendl;else pName=NULL;student:student()coutDestructor;if ( pName ) coutpNameendl;delete pName; /改为delete pName;student & student:operator = ( student &s ) coutCopy Assign operator;delete pName; if ( s.pName ) pName = new charstrlen(s.pName)+1;if ( pName ) strcpy ( pName, s.pName );coutpNameend
4、l;else pName=NULL;return *this; /改为return *this;void student:print( ) /改为void student:print( )if (pName = NULL ) cout NULL endl; else cout pName print();/改为s3-print();delete s3; return; /去掉0二、编程题(50分)每段代码10分,共50分。红字为要求设计的部分。#include #include #include using namespace std;class goods;ostream& operator
5、(istream &is, goods &a);class goodsstringName;/名称intAmount;/数量floatPrice;/单价public:goods (); goods ();/析构函数,将数据保存到文件中bool IsEmpty() return Name=; ;friend ostream& operator(istream &is, goods &a);goods: goods () ifstream file(goodinfo.data);if ( file ) /文件打开成功file *this;file.close();elseName = ;Amoun
6、t = 0;Price = 0;goods: goods ()ofstream file(goodinfo.data);file *this;file.close();ostream& operator (ostream &os, goods &a)os a.Name endl;os a.Amount endl;os a.Price (istream &is, goods &a)if ( is = cin )cout 请输入你的以下信息: endl;cout a.Name;cout a.Amount;cout a.Price;elseis a.Name;is a.Amount;is a.Price;return is;void main()goods g;if ( g.IsEmpty() ) cin g;cout endl;cout 显示商品信息: endl;cout g;