《09-10程序設(shè)計(jì)及算法語言Ⅱ上機(jī)考試試卷B(電類)答》由會員分享,可在線閱讀,更多相關(guān)《09-10程序設(shè)計(jì)及算法語言Ⅱ上機(jī)考試試卷B(電類)答(5頁珍藏版)》請?jiān)谘b配圖網(wǎng)上搜索。
1、東南大學(xué)09級C++(下)上機(jī)試卷B-答案
一、改錯(cuò)題(50分)本題共10個(gè)錯(cuò)誤,每個(gè)錯(cuò)誤5分
#include
#include
using namespace std;
class student
{
char *pName;
public:
student();
student(char *pname);
student(student &s);
~student();
student & operator=(student &s);
void print();
};
student::st
2、udent() //去掉void
{
cout<<"Constructor";
pName=NULL;
cout<<"缺省"<
3、 //改為student &s
{
cout<<"Copy Constructor";
if(s.pName)
{
int len = strlen( s.pName );
pName = new char[len+1]; //len改為len+1
if ( pName ) strcpy (pName, s.pName);
cout<
4、out<
5、se pName=NULL;
return *this; //改為return *this;
}
void student::print( ) //改為void student::print( )
{
if (pName = NULL ) cout << "NULL" << endl;
else cout << pName << endl;
}
void main(void)
{
student s1("范英明"),s2("沈俊");
student *s3 = new student;
*s3 = s1;
6、 //改為*s3 = s1;
s1.print();
s2.print();
s3->print(); //改為s3->print();
delete s3;
return; //去掉0
}
二、編程題(50分)每段代碼10分,共50分。紅字為要求設(shè)計(jì)的部分。
#include
#include
#include
using namespace std;
class goods;
ostream& operator<<(ostrea
7、m &os, goods &a);
istream& operator>>(istream &is, goods &a);
class goods
{
string Name; //名稱
int Amount; //數(shù)量
float Price; //單價(jià)
public:
goods ();
~ goods (); //析構(gòu)函數(shù),將數(shù)據(jù)保存到文件中
bool IsEmpty(){ return Name==""; };
friend ostream& operator<<(ostream &os, goods &a); //用于
8、直接輸出數(shù)組對象
friend istream& operator>>(istream &is, goods &a);
};
goods:: goods ()
{
ifstream file("goodinfo.data");
if ( file ) //文件打開成功
{
file >> *this;
file.close();
}
else
{
Name = "";
Amount = 0;
Price = 0;
}
}
goods::~ goods ()
{
ofstream file("go
9、odinfo.data");
file << *this;
file.close();
}
ostream& operator << (ostream &os, goods &a)
{
os << a.Name << endl;
os << a.Amount << endl;
os << a.Price << endl;
return os;
}
istream& operator >> (istream &is, goods &a)
{
if ( is == cin )
{
cout << "請輸入你的以下信息:" <<
10、endl;
cout << "名稱:";
is >> a.Name;
cout << "數(shù)量:";
is >> a.Amount;
cout << "單價(jià):";
is >> a.Price;
}
else
{
is >> 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;
}