请打开考生文件夹下的解决方案文件proj2,其中有两个类:一是销售类(sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale),用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale类继承了sale类。类的主要数据成员的含义和成员函数的功能要求在程序注释中有说明。请在程序中的横线处填写适当的代码,然后删除横线,完成程序的功能。此程序的正确输出结果应为: Discount item is cheaper. Saving is 0.1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。#includeiostreamusing namespace std;Class Sale{public: Sale();//默认构造函数,将price初始化为0 Sale(double the_price);//构造函数,用the_price初始化price virtual double bill()const;//返回当前商品的价格(基本价) double savings(const Saleother)const;//返回参数other所引用的对象比当前对象便宜的差价protected: double price;//商品的基本价格(不打折的价格)};Sale::Sale():price(0){}Sale::Sale(double the_price):price(the_price){)double Sale::bill()const{ return price,}double Sale::savings(const Saleother)const{ //ERROR ******found****** ____________;//返回当前对象价格比other贵多少的差价}class DiscountSale:public Sale//打折销售类继承销售类{public: DiscountSale(),//默认构造函数,将discount初始化为0 DiscountSale(double the_price,double the_discount);//构造函数,the_price是基本价格;the_discount是折扣百分比 virtual double bill()const;//返回本商品销售价格(即打折以后的实际售价,覆盖了基类的biii函数)protected: double discount;//折扣百分比。例如降价至原价的70%,此成员值应为70};DiscountSale::DiscountSale():discount(0){}DiscountSale::DiscountSale(double the_price,double the_discount) :Sale(the price),discount(the discount){}double DiscountSale::bill ( )const{ double fraction=discount/100; // ******found****** ___________;//返回本对象打折以后的实际售价}bool operator(const Salefirst,const Salesecond){ // ******found****** ___________;//判断是否first价格低于second价格}int main(){ Sale simple(10.00); DiscountSale discount(11.00,90); if(discountsimple) { cout"Discount item ischeaper.\n"; // ******found****** //这里输出购买discount比购买simple节省多少钱 cout"Saving is"____________endl; ) else cout"Discount item isnot cheaper.\n"; return 0;}
请打开考生文件夹下的解决方案文件proj1,程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:Name:Smith Age:21 ID:99999 CourseNum:12 Record:970注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#includeusing namespace std;class StudentInfo{protected://ERROR*******found*******char Name;int Age;int ID;int CourseNum;float Record;public:StudentInfo(char*name,intAge,int ID,int courseNum,float record);//ERROR*******found*******VOid~StudentInfo( ){}float AVerageRecord( ){return Record/CourseNum;}void show( )const{coutCourSeNum}};//ERROR*******found*******StudentInfo StudentInfo(char*Name,int Age,int ID,intCourseNum,float Record){Name=name;Age=age;this->ID=ID;CourSeNum=courSeNum;Record=record;}int main( ){StudentInfo st("Smith",21,99999,12,970);st.show( );return0;}
下述关于成员函数的描述,正确的是( )。
如在类中有如下函数定义 Void fun(int a,int b,int c=0);则下列调用中,正确的是( )。
请打开考生文件夹下的解决方案文件proj2,此工程中包含一个源程序文件main.cpp,其中有“房间”类Room及其派生出的“办公室”类Office的定义,还有主函数main的定义。请在程序中“//****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 办公室房间号:308 办公室长度:5.6 办公室宽度:4.8 办公室面积:26.88 办公室所属部门:会计科 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#includeiostreamusing namespace std;class Room( //“房间”类 int room no; //房间号 double length; //房间长度(m) double width; //房间宽度(m)public: Room(int the_room no,double the_length,double the_ width):room no(th_room_no),length(the length),width(the width)() int theRoomNo()const{return room_no;}//返回房间号 double theLength()const{return length;}//返回房间长度 double theWidth()const{return width;}//返回房间宽度 //**************found************** double theArea()const{_____________}//返回房间面积(矩形面积)};class Office:public Roomt//“办公室”类 char*depart; //所属部门public: Office(int the _room _no,double the _length,double the_width,const char *the _depart)//**************found************** :_____________{ depart=new char[ strlen(the depart)+1];//**************found************** strcpy(____________); } ~Office(){delete[]depart;} const char*theDepartment()const { return depart;}//返回所属部门};int main(){ //**************found************** Office___________; cout"办公室房间号:"an office.theRoomNo()endl "办公室长度:"an office.theLength()endl "办公室宽度:"an_office.theWidth()endl ”办公室面积:"an_office.theArea()endl "办公室所属部门:"an_office.theDepartmenI()endl; return 0;}
请打开考生文件夹下的解决方案文件proj2,该工程中含有一个源程序文件proj2.cpp。其中定义了类Set和用于测试该类的主函数main。类Set是一个用于描述字符集合的类,在该字符集合中,元素不能重复(将“a”和“A”视为不同元素),元素最大个数为100。为该类实现一个构造函数Set(char*s),它用一个字符串来构造一个集合对象,当字符串中出现重复字符时,只放入一个字符。此外,还要为该类实现另一个成员函数InSet(char c),用于测试一个字符e是否在一个集合中,若在,则返回true;否则返回false。 构造函数Set和成员函数InSet的部分实现代码已在文件proj2.cpp中给出,请在标有注释“//TODO:”的行中添加适当的代码,将这两个函数补充完整,以实现其功能。 提示:在实现构造函数时,可以调用InSet函数来判断一个字符是否已经在集合中。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//proj2.cpp#includeiostreamusing namespace std;const int MXNUN=100;class Set{private: int num; //元素个数 char setdata[NAXNUM]; //字符数组,用于存储集合元素public: set(char*s); //构造函数,用字符串s构造一个集合对象 bool InSet(char c);//判断一个字符c是否在集合中,若在,返回true,否则返回false void Print()const; //输出集合中所有元素};Set::Set(char*s){ num=0; while(*s){//********found********if(_________)//TODO:添加代码,测试元素在集合中不存在//********found********__________;//TODO:添加一条语句,加入元素至集合中 S++; }}bool Set::InSet(char c){ for(int i=0 ; inum;i++)//********found******** if(_______) //TODO:添加代码,测试元素C是否与集合中某元素相同//********found********__________;//TODO:添加一条语句,进行相应处理 return false ;}void Set::Print()conSt{ Cout"Set elements:"endl; for(int i=0;inum;i++) coutsetdata[i]''; coutendl;}int main(){ char s[MAXNUM]; cin.getline(s,MAXNUM-1); //从标准输入中读入一行 Set setobj (s); /构造对象setobj setobj.Print(); //显示对象setobj中内容 return 0;}
使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成函数fun(char*str,char ch),本函数采用二分法,在己按字母次序从小到大排序的字符数组str中,查找字符ch,若ch在数组中,函数返回字符ch在数组中的下标,否则返回一1。 二分法查找的思想是:初始查找区间的下界为0,上界为len一1,查找区间的中后,k=(下界+上界)/2;若list[k]等于ch,查找成功;若list[k]ch,则新的查找区间的下界不变,上界改为k-1;否则新的查找区间的下界改为k+1,上界不变。在新区间内继续用二分法查找。注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。#includeiostream.hint fun(char*Str,char ch){}Void msin(){ char Str[]=(‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’,‘h’,‘i’,‘j’,‘k’,0); char ch; cout“请输入一个字符:”end1; Cinch;cout“输入数字的位置是:”fun(Str,ch)end1; return;}
为 int *p; 动态分配内存空间并且赋初始值0的语句是【 】。
请打开考生文件夹下的解决方案文件proj2,该工程中含有一个源程序文件proj2.cpp。其中定义了类Set和用于测试该类的主函数main。类Set是一个用于描述字符集合的类,在该字符集合中,元素不能重复(将“a”和“A”视为不同元素),元素最大个数为100。为该类实现一个构造函数Set(char*s),它用一个字符串来构造一个集合对象,当字符串中出现重复字符时,只放入一个字符。此外,还要为该类实现另一个成员函数InSet(char c),用于测试一个字符e是否在一个集合中,若在,则返回true;否则返回false。 构造函数Set和成员函数InSet的部分实现代码已在文件proj2.cpp中给出,请在标有注释“//TODO:”的行中添加适当的代码,将这两个函数补充完整,以实现其功能。 提示:在实现构造函数时,可以调用InSet函数来判断一个字符是否已经在集合中。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//proj2.cpp#includeiostreamusing namespace std;const int MXNUN=100;class Set{private: int num; //元素个数 char setdata[NAXNUM]; //字符数组,用于存储集合元素public: set(char*s); //构造函数,用字符串s构造一个集合对象 bool InSet(char c);//判断一个字符c是否在集合中,若在,返回true,否则返回false void Print()const; //输出集合中所有元素};Set::Set(char*s){ num=0; while(*s){//********found********if(_________)//TODO:添加代码,测试元素在集合中不存在//********found********__________;//TODO:添加一条语句,加入元素至集合中 S++; }}bool Set::InSet(char c){ for(int i=0 ; inum;i++)//********found******** if(_______) //TODO:添加代码,测试元素C是否与集合中某元素相同//********found********__________;//TODO:添加一条语句,进行相应处理 return false ;}void Set::Print()conSt{ Cout"Set elements:"endl; for(int i=0;inum;i++) coutsetdata[i]''; coutendl;}int main(){ char s[MAXNUM]; cin.getline(s,MAXNUM-1); //从标准输入中读入一行 Set setobj (s); /构造对象setobj setobj.Print(); //显示对象setobj中内容 return 0;}
使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: 10 6 30 2 2 注意:错误的语句在//******error******的下面,修改该语句即可。1 #include2 class CMyClass3 {4 private:5 int number;6 int Add(int i)7 {8 return number+=i;9 }10 int Sub(int i)11 {12 return number-=i;13 }14 int Mul(int i)15 {16 return number*=i;17 }18 int Div(int i)19 {20 if(i!=0)21 {22 return number/=i;23 }24 else25 return number;26 }27 //******error******28 typedef int(FUNC)(int);29 //******error******30 FUNC func[];31 public:32 CMyClass()33 {34 func[0]=CMyClass::Add;35 func[1]=CMyClass::Sub;36 func[2]=CMyClass::Mul;37 func[3]=CMyClass::Div;38 number=0;39 }40 int CallFunction(int i,int j)41 {42 //******error******43 return(func[i])(j);44 }45 };46 void main()47 {48 CMyClass myobj;49 cout50 cout51 cout52 cout53 cout54 }
请打开考生文件夹下的解决方案文件proj3,其中声明了SortedList类,是一个用于表示有序数据表的类。其成员函数insert的功能是将一个数据插入到一个有序表中,使得该数据表仍然保持有序。请编写这个insert函数。程序的正确输出应为:插入前:1,2,4,5,7,8,10插入6和3后:1,2,3,4,5,6,7,8,10要求:补充编制的内容写在“//********333********”与“//********666********”之间。不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//SortedList.h#includeusing namespace std;class SortedLi{//有序数据表类int len;double*d;public:SortedList(int len,doubledata[ ]=NULL);~SortedList( ){delete[ ]d;}int length( )const{returnlen;)//有序数据表长度(即元素的个数)double getElement (int i)const{return d[i];}VOid insert(double data);void show( )const;//显示有序数据表};void writeToFiie(char*,const SortedLiSt);//main.cpp#include"SortedList.h"SortedList::SortedList(int len,double data[ ]):len(len){d=new double[len];for(int k=0;kd[k]=(data==NULL?0.0:data[k]);for(int i=0;iint m=i;for(int j=i;jif(d[j]if(m>i){double t=d[m];d[m]=d[i];d[i]=t;}}}void SortedList::insert(double data){//********333********//********666********}void SortedLiSt::show( )const{//显示有序数据表for(int i=0;icoutcout}int main( ){double s[ ]={5,8,1,2,10,4,7);SortedList1ist(7,s);coutlist.show( );list.insert(6.0);list.insert(3.0);coutendl;list.show( );writeToFile(" ",list);return0;}
下面程序的运行结果为【 】。 class A } public: int num; A(int i){num=i;} A(A &a){num=++a.num;} void print(){cout<<num;} }; void main(){ A a(1),b(a); a.print(); b.print(); }
