使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: 150 200 注意:错误的语句在//******error******的下面,修改该语句即可。提示:定义Number1为一个整型数据位长的变量。1 #include2 //******error******3 struct4 {5 //******error******6 int Number1:1;7 int Number2;8 } MyStruct;9 void main()10 {11 MyStruct mstr;12 mstr.Number1=150;13 mstr.Number2=15;14 //******error******15 int *ptr=mstr.Number1;16 cout17 *ptr=200;18 cout19 }
请打开考生文件夹下的解决方案文件proj2,其中定义了Employee类和Manager类。Employee用于表示某公司的雇员,其属性包括姓名(name)和工作部分(dept)。Manager是Employee的公有派生类,用于表示雇员中的经理。除了姓名和工作部分之外,Manager的属性还包括级别(level)。Employee类的成员函数print用于输出雇员的信息;Manager类的成员函数print负责输出经理的信息。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: Name:Sally Smith Dept:Sales Level:2 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#includeiostEeam#inctude stringusing namespace std;class Employee{public: Employee(string name,string dept)://***********found*********** {} virtual void print()const; string dept()const //返回部门名称 {//***********found*********** } Virtual ~Employee(){}private: string name; string dept;};class Hanager:public Employee {public: Nanager(string name,stringdept,int level)://***********found*********** {} Virtual void print()const;prirate: int level;};void Employee::print()const{ cout"Name:" name endl; tout "Dept:" dept endl ;}void Nanager::print() const{//***********found*********** cout "Level: " levelendl;}int main(){ Employee*emp=new Nanager("Sally Smith","Sa2es",2); emp-print(); delete emp; return 0 ; }
若类B是类A的私有派生类,类C是类B的公有派生类,则类C【 】访问基类A的公有成员。
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp,请将堆栈类的定义补充完整。使程序的输出结果为: The element of stack are:4 3 2 1 注意:请勿修改主函数main和其他函数中的任何内容,只在横线处编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//proj2.cpp#includeiostreamusing namespace std;const int Size=5;class Stack;class Item{public://********found********Item(const intval):________{}//构造函数对item进行初始化private: int item; Item * next; friend class Stack;};class Stack{public: Stack():top(NULL){} ~Stack(); int Pop(); void Push(const int);private: Item*top; }; Stack::~Stack() { Item*p=top,*q; while(p!=NULL) { q=p-next;//********found******** _________; //释放p所指向的节点 p=q; }}int Stack::Pop(){ Item*temp; int ret;//********found******** _________; //使temp指向栈顶节点 ret=top-item; top=top-next; delete temp; return ret;}void Stack::Push(const intval){ Item*temp=new Item(val);//********found********___________;//使新节点的next指针指向栈顶数据 top=temp;}int main(){ Stack s; for(int i=1;iSize;i++) s.Push(i); cout"The element of stackare:"; for(i=1;iSize;i++) couts.Pop()'\t'; return 0;}
使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成以下函数: int factorial(int n):求出n的阶乘,必须使用递归调用。 如果n小于1则返回0。 注意:不能修改函数的其他部分。1 #include2 #include3 int factorial(int n)4 {56 }7 void main()8 {9 cout10 cout11 cout12 return;13 }
请打开考生文件夹下的解决方案文件proj3,此工程包含一个源程序文件proj3.cpp,其中定义了用于表示二维向量的类MyVeetor;程序应当显示(6,8)。但程序中有缺失部分,请按照以下提示,把缺失部分补充完整: (1)在“//**1** ****found****”的下方是构造函数的定义,它用参数提供的坐标对戈和Y进行初始化。 (2)在“//**2** ****found****”的下方是减法运算符函数定义中的一条语句。两个二维向量相减生成另一个二维向量:其X坐标等于两向量X坐标之差,其Y坐标等于两向量Y坐标之差。 (3)在“//**3** ****found****”的下方,语句的功能是使变量v3获得新值,它等于向量v1与向量v2之和。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。 1 //proj3.cpp 2 #includeiostream 3 using std::ostream; 4 using std::cout; 5 using std::endl; 6 7 class MyVector{ //表示二维向量的类 8 double X; //X坐标值 9 double Y; //Y坐标值 10 public: 11 MyVector(double i=0.0, doub2e j=0.0); //构造函数 12 MyVector operator+(MyVector j); //重载运算符 + 13 friend MyVector operator -( MyVector i,MyVector j); //重 载运算符 - 14 friend ostreamoperator( ostreamOS,Myvector v); //重载运算符 15 }; 16 //**1** ******found****** 17 ___________(double i,double j):x(i),y(j){} 18 MyVector MyVector::operator+{ MyVector j) { 19 return MyVectot(x+j.x,y+j.y); 20 } 21 MyVector operator -(MyVector i,MyVector j) 22 {//**2** ******found****** 23 return MyVector(__________); 24 } 25 26 ostream operator( ostreamos,MyVector v){ 27 os '('v.x ','v.y')';//输出向量v的坐标 28 return os; 29 } 30 int main() 31 { 32 MyVector v1(2,3),v2(4,5),v3; 33 //**3** ******found****** 34 v3=____________; 35 coutv3endl; 36 return 0 ; 37 }
请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.cpp,其中有坐标点类point、线段类Line和三角形类Triangle的定义,还有main函数的定义。程序中两点间距离的计算是按公式d=实现的,三角形面积的计算是按公式f=实现的,其中s=。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: Side 1:9.43398 Side 2:5 Side 3:8 area:20 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#irlclude#includeusing rlamespace std;class point{//坐标点类public: const double x,y; Point(double x=0.0,doubley=0.0):x(x),y(y){}// ********found******** double distanceTo(____________)const{//到指定点的距离 return sqrt((x-p.x)*(x—p.x)+(y—p.y)*(y—p.y)); }};class Line{//线段类public: const Point p1,p2 ;//线段的两个端点 // ********found******** Line(Point p1,Point p2):_____________{} double length()const{returnp1.distanceTo(p2);}//线段的长度};class Triangle{//三角形类public:const Point p1,p2,p3;//三角形的三个顶点// ********found********Triangle(__________):p1(p2),p2(p2),p3(p3){}double lengthl()const{//边p1,p2的长度 return Line(p1,p2).length(); } double length2()const{//边p2,p3的长度 return Line(p2,p3).length(); } double length3()const{//边p3,p1的长度 return Line(p3,p1).length(); } double area()const(//三角形面积 // ********found******** double s=___________; return sqrt(s*(s-length1())*(s-length2())*(s-length3())); }};int main(){ Triangle r(Point(0.0,8.0),Point(5.0,0.0),Point(0.0,0.0)); cout"Side 1:"
