使用VC6打开考生文件夹下的工程proj2。此工程包含一个源程序文件main2.cpp,但该程序运行有问题。请改正main函数中的错误。
源程序文件main2.cpp清单如下:
//main2.cpp
include <iostream>
using namespace std;
class MyClass
{
public:
MyClass(int m)
{
member=m;
}
~MyClass() {}
int GetMember()
{
return member;
}
private:
int member;
};
MyClass MakeObject(int m)
{
MyClass *pMyClass=new MyClass(m);
return *pMyClass;
}
int main ()
{
int x=7;
/************found**************/
MyClass *myObj=MakeObject(x);
/*************found*************/
cout<<"My object has member"<<myObj.GetMember()<<end1;
return 0;
}
第1题
Derive1's Print() Called.
Derive2's Print() called.
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
①定义函数Print()为无值型纯虚函数。请在注释“//**1**”之后添加适当的语句。
②建立类Derivel的构造函数,请在注释“//**2**”之后添加适当的语句。
③完成类Derive2成员函数Print()的定义。请在注释“//**3**”之后添加适当的语句。
④定义类Derivel的对象指针d1,类Derive2的对象指针d2。其初始化值分别为1和2。
源程序文件MyMain2.cpp中的程序清单如下:
//MyMain2. cpp
include <iostream>
using namespace std;
class Base
{
public:
Base(int i)
{
b=i;
}
//* * 1 * *
protected:
int b;
};
class Derivel: public Base
{
public:
//* * 2 * *
void print ()
{
cout<<" Derivel's Print() called."<<end1;
}
};
class Derive2 : public Base
{
public:
Derive2(int i) :Base(i) { }
//* * 3 * *
};
void fun (Base *obj)
{
obj->Print ();
}
int main ( )
{
//* * 4 * *
fun (d1);
fun (d2);
return 0;
}
第2题
member = 0
member = 5
member = 10
源程序文件 mainl.cpp 清单如下:
//mainl.cpp
include <iostream>
using namespace std;
class MyClass
{
public:
MyClass(int i)
{
member=i;
}
void SetMember(int m)
{
member=m;
}
int GetMember()const
{
return member;
}
void Print() const
{
cout<<"member="<<member<<end1;
}
private:
int member;
};
int main()
{
/***************found***********/
MyClass obj1;
obj1. Print();
MyClass obj2(3);
/***************found***********/
obj1.member = 5;
/***************found***********/
MyClass. SetMember(10);
obj1. Print();
obj2. Print();
return 0;
}
第3题
源程序文件RevMain3.cpp清单如下:
//RevMain3.cpp
include<iostream>
using namespace std;
class MyClass{
public:
/* * * * * * * * *found * * * * * * + * * */
void MyClass(int a){ value=a;}
int Max(int x,int y)
{
if(x>y)
return x>y?x:y;
else
return y>value?y:value;
}
/* * * * * * * * *found * * * * * * * * * */
~MyClass(int a)
{
value=a;
}
private:
int value;
};
int main()
{
MyClass my(10);
cout<<my.Max(20,30)<<end1;
return 0;
}
第4题
请改正程序中的错误,使它能得到正确结果。
注意,不要改动主函数,不得删行或增行,也不得更改程序的结构。
源程序文件RevMain10.cpp中的程序清单如下:
//RevMain10.cpp
include <iostream>
using namespace std;
define N 30
int fun(int *x, int n);
int main ()
{
int h[N]={4,7, 6, 5, 1, 7,3, 8,0,2,3};
int i;
for (i=0; i<11; i++)
cout<<h [i] << " " ;
cout <<"/n";
fun(h, 11);
for (i=0; i<n; i++)
cout<<h [i]<<" ";
cout<<' \n';
return 0;
}
int fun(int *x, int n)
{
int i,t;
int a1=0, a2=0,min1=32767,min2=32676;
/* * * * *FOUND * * * * */
for(i=1;i<n;i++)
{
if (x [i]<min1)
{
min2=min1;
a2=a1;
min1=x [i];
a1=i;
}
else if (x [i] <min2)
{
min2=x [i];
a2=i;
}
}
/* * * * *FOUND * * * * */
t=x[0];x[a1]=x[0];x[a1]=t;
/* * * * *FOUND * * * * */
t=x[1] ;x[a2]=x[1];x[a2]=t;
}
第5题
请改正程序中的错误,使它能得到正确结果。
注意,不要改动main函数,不得删行或增行,也不得更改程序的结构。
源程序文件RevMain9.cpp中的程序清单如下;
//RevMain9.cpp
include<iostream>
using namespace std;
char* fun(char*s);
int main()
{
int i;
char str[81];
cout<<"Please enter a string:";
cin>>str;
cout<<"\n The result is:\n"<<fun(str)<<end1;
return 0;
}
char *fun(char*s)
{
int i;
for(i=0;s[i];i++)
{
/* * * * *FOUND* * * * *
if(('a'<=s[i])||(s[i]<:='z'))
/* * * * *FOUND * * * * * /
s[i]+=32;
}
return s;
}
第6题
class ArrayStack:public Stack{ char*P; int maxSizc; int top; public: ArravStack(int s) { top=0; maxSize=s: //*********found********* P=______; } ~ArrayStack { //*********found********* _______; } void push(char c) } if(top==maxSize){ cerr<<”Overflow! \n”: return; } //*********found********* _______; top++: } char pop { if(top==0){ cerr<<”Underflow!、n”; return‘\0’; } Top--; //*********found********* ______; } }; void f(Stack&sRef) { char ch[]={‘a’,‘b’,‘c’}; cout<<ch[0]<<”,”<<ch[1]<<”,”<<ch[2]<<endl; sRef.push(oh[0]);sRef.push(ch[1]);sRef.push(ch[2]); cout<<sRef.poP<<”,”; cout<<sRef.poP<<”,”; cout<<sRef.poP<<endl; } int main { ArrayStack as(10); f(as): return 0: }
第7题
注意,不要改动主函数,不得删行或增行,也不得更改程序的结构。
源程序文件RevMain15.cpp中的程序清单如下:
//RevMain15.cpp
include<iostream>
using namespace std;
class Sample
{
private:
int x;
static int y;
public:
Sample(int a)
{
x=a;
y+=x;
}
static void print(Sample s)
{
cout<<"x="<<x<<<<",y="<<y<<end1;
}
Sample::y=5;
int main()
{
Sampel s1(10);
Sample s2(15);
Sample::print(s1);
Sample::print(s2);
return 0;
}
第8题
注意,不要改动main函数,不得删行或增行,也不得更改程序的结构。
源程序文件RevMain14.cpp中的程序清单如下:
//RevMain14.cpp
include<iostream>
include<math>
using namespace std;
class Point
{
private:
double x;
double y;
public:
Point(){}
void Point(double x1,double y1)
{
x=x1;
y=y1;
}
void setvalue(double x,double y)
{
x=x;
y=y;
}
double getx ()
{
return x;
}
double gety()
}
return y;
}
void print()
{
cout<<"x="<<x<<",y= "<<y<<end1;
}
~Point(){}
};
class Line
{
private:
Point p1;
Point p2;
double width;
public:
Line(double x1,double y1,double x2,double y2,double d)
:p1(x1,y1),p2(x2,y2)
{
width=d;
}
~Line(){}
void displength()
{
double 1;
1=sqrt((p1.getx{)-p2.getx())*(p1.getx()-p2-getx())+
(p1.gety()-p2.gety())*(p1.gety()-p2.gety()));
cout<<"the length of Line is "<<1<<end1;
}
};
int main()
{
Line *p1;
Line 1(5,15,25,35,0.5);
p1=&1;
p1->displength();
return 0;
}
第9题
x=20
源程序文件RevMain4.cpp的清单如下:
//RevMain4.cpp
include<iostream>
using namespace std;
class MyClass
{
public:
MyClass(int a)
{
x=a;
}
void setX(int a)
{
x=a;
}
void print()
{
cout<<"x="<<x;
}
private:
int x;
};
int main()
{
const MyClass obj (10);
/ ************found**************/
obj.setX(20);
/ ************found**************/
obj.print();
return 0;
}
第10题
请按要求完成下列操作,将类的定义补充完成:
①类Inherit是类Base的公有派生类。请在注释“//* *1* *”之后添加适当的语句。
②完成类Inherit成员函数setvalue(int a,int b,int c,int d)的定义,此函数实现的功能是将基类成员x、y、z和派生类的数据成员m的值分别设置成a、b、c和d。请在注释“//* *2* *”之后添加适当的语句。
③完成类Inherit成员函数display()的类体外定义,此函数实现的功能是以“,,,,”的格式将x、y、z和m的值输出到屏幕上。请在注释“//* *3* *”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件MyMain15.cpp清单如下:
//Mymain15.cpp
include<iostream>
using namespace std;
class Base
{
private:
int x;
protected:
int y;
public:
int z;
void setx(int i)
{
x=i;
}
int getx()const
{
return x;
}
};
//* * *1* * *
{
private:
int m;
public:
void setvalue(int a,int b,int c,int d)
{
//* * *2* * *
}
void display()const;
};
//* * *3* * *
int main()
{
Inherit A;
A.setvalue(1,2,3,4);
A.display();
return 0;
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!