使用VC6打开考生文件夹下的工程test29_1,此工程包含一个源程序文件test29_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果为:
A:no parameters
B:int parameter
A:no parameters
B:int parameter
C:int parameter
源程序文件test29_1.cpp清单如下;
include <iostream.h>
class A
{
/***************** found *****************/
private:
A(){ cout<<"A:no parameters\n";}
A(int a) {cout<<"A: int parameter\n";}
};
class B:public A
{
public:
B(int a){cout<<"B:int Parameter\n";}
};
/***************** found *****************/
class C:public B,public A
{
public:
/***************** found *****************/
C(int a) ::B(a) {cout<<"C: int parameter\n"; )
};
void main ()
{
B b(1);
C c(2);
}
第1题
A:no parameters
B:int parameter
A:no parameters
B:int parameter
C:int parameter
源程序文件test29_1.cpp清单如下;
include <iostream.h>
class A
{
/***************** found *****************/
private:
A(){ cout<<"A:no parameters\n";}
A(int a) {cout<<"A: int parameter\n";}
};
class B:public A
{
public:
B(int a){cout<<"B:int Parameter\n";}
};
/***************** found *****************/
class C:public B,public A
{
public:
/***************** found *****************/
C(int a) ::B(a) {cout<<"C: int parameter\n"; )
};
void main ( )
{
B b(1);
C c(2);
}
第2题
(1)定义类Coordinate的保护数据成员x和y,它们都是int型的数据,代表二维坐标的横纵坐标值。请在注释“//**1 **”之后添加适当的语句。
(2)根据类Coordinate定义后的成员函数Display的实现,补充该函数在类Coordinate定义体内的声明,Display为二维坐标类的虚函数。请在注释“//**2**。”之后添加适当的语句。
(3)完成二维坐标类Coordinate的构造函数,将参数a和b分别赋值给数据成员x和y。请在注释“//**3**”之后添加适当的语句。
(4)根据ThreeDCoord类构造函数的声明,补充ThreeDCoord构造函数的实现,参数a和b通过调用基类的构造函数来初始化基类的数据成员x和y,c赋值给数据成员2。请在注释“//**4**”之后添加适当的语句。
输出结果如下;
[1,2]
[3,4,5]
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件tese9_3.cpp清单如下:
include<iostream.h>
class Coordinate
{protected:
//** 1 **
public:
Coordinate(int a=0, int b=0);
//** 2 **
};
Coordinate::Coordinate(int a, int b)
{
//** 3 **
}
void Coordinate::Display() const
{
cout << '[' << x << ", "<< y << ']' << end1;
}
class ThreeDCoord:public Coordinate
{
int z;
public:
ThreeDCoord(int a=0, int b=0, int c=0);
virtual void Display() const;
};
//** 4 **
void ThreeDCoord::Display() const
{
cout << '[' << x << ", "<< y << ", "<< z << ']' << end1;
}
void main ( )
{
Coordinate c(1, 2);
ThreeDCoord t(3, 4, 5);
c.Display ();
t.Display ();
}
第3题
Constructor2
Constructor1
i=0
i=10
Destructor
源程序文件test6_1.cpp清单如下:
include<iostream.h>
class CSample
{
int i;
public;
CSample(){cout<<"Constructorl"<<endl;i=0;)
CSample(int val){cout<<"Constructor2"<<endl;i=val;}
~CSample()(cout<<"Destructor"<<endl;)
void disp();
};
/**********found********/
void disp()
{
cout<<"i=”<<i<<endl;
}
void main()
{
CSample *a,b(10);
/**********found*********/
a->disp();
/**********found*********/
b->disp();
}
第4题
Constructor called.
Default constructor called.
Area is 6
Area is 0
Area is 6
源程序文件test1_1,cpp清单如下:
include<iostream.h>
class RectAngle
{
private:
double ledge,sedge;
public:
RectAngle()
{
cout<<"Default constructor called.";
}
RectAngle(double l,double s)
{
ledge=l;sedge=s;
cout<<"Constructor called.";
}
void Set(double l,double s)
{
ledge=l;sedge=s;
}
void Area()
{
cout<<"Area is"<<ledge*sedge<<endl;
}
};
void main()
{
/***************** found *****************/
RectAngle Rect1(2,3);
RectAngle Rect2(1);
/**************** found *****************/
RectAnglC Rect3;
Rectl.Area();
/***************** found *****************/
RecL2.lodge=0;Rect2.sedge=0;
Reck2.Area();
Rect3.Area();
}
第5题
n=2
n=5
n=7
源程序文件test13_1清单如下:
include<iostream.h>
class Sample
{
int n;
public:
/*************** found ************+***/
Sample()
Sample(int i){n=i;}
/***************** found ********+**********/
void add(Sample s1,Sample s2)
/***************** found ****************/
{
this.n=s1->n+s2.n;
}
void disp(){cout<<"n="<<n<<endl;}
};
void main()
{
Sample s1(2),s2(5),s3;
s3.add(&s1,s2);
s1.disp();
s2.disp();
s3.disp();
}
第6题
Number=1,2,3
sum=16
源程序文件test33_1.cpp清单如下;
include <iostream.h>
class Num
{
public:
/***************** found *****************/
void Num(int a,int b, int c);
void PrintNumber();
void PrintSum();
private:
int x,y,z;
static int sum;
};
/***************** found *****************/
int sum=10;
Num::Num(int a,int b, int c)
{
x=a;
y=b;
z=c;
sum=sum+x+y+c;
}
void Num::PrintNumber()
{
cout<<"Number="<<x<<","<<y<<","<<z<<end1;
}
void Num::PrintSum()
{
cout<<"sum="<<sum<<end1;
}
void main( )
{
Num m(1,2,3),*n;
n=&m;
m. PrintNumber();
/***************** found *****************/
n. PrintSum();
}
第7题
i
iz
O
12
22
源程序文件test23_1.cpp清单如下:
include<iostream.h>
include<stdio.h>
class innerclass
{
int x;
public:
/****************found*******************/
innerclass(int z) { x=z; cout<<"iz"<<endl;};
/****************found*******************/
void write( ) {print("%d\n",x);}
}
class outerclass
{
int y;
innerclass g;
innerclass *h;
public:
outerclass(int a);
void write( ) { printf("%d\n",y); }
void write_inner_g() {g.write(); }
void write_inner_h() {h->write();}
};
outerclass::outerclass(int a)
{
/****************found*******************/
h=new innerclass(12);
cout<<"o"<<endl;
}
int main ( )
{
outerclass ob(22);
ob.write_inner_h();
ob.write();
return 1;
}
第8题
0149 16 25 36 49 64 81
源程序文件test37_1.cpp清单如下:
include<iostream.h>
template <class T, int N = 100> class Vector
{
T vec[N];
public:
void set(int pos, T val);
T get(iht pos);
/***************** found *****************/
}
template <class T, int N> void Vector<T, N>::set(int pos, T val)
{
vec[pos] = val;
}
/***************** found *****************/
template <class T, int N> Vector<T, N>::get(int pos)
{
return vec[pos];
}
int main ()
{
Vector<double, 10> v;
int i = 0;
double d = 0.0;
for (i = 0; i < 10; i++)
v.set(i, double(i * i));
for (i = 0; i < 10; i++)
cout<<v.get(i)<<" ";
cout<<end1;
/***************** found *****************/
}
第9题
100
源程序文件test27_1.cpp清单如下:
include <iostream.h>
template <class T>
class pair
{
T valuel, value2;
public:
pair (T first, T second)
{valuel=first; value2=second;}
/***************** found *****************/
char getmax ();
};
/***************** found *****************/
T pair<T>::getmax ()
{
T retval;
/***************** found *****************/
retval = valuel>value2?? value1 : value2;
return retval;
}
void main ()
{
pair <int> myobject (100, 75);
cout << myobject.getmax()<<end1;
}
第10题
My object has member 7
源程序文件test15_1.cpp清单如下:
include<iostream.h>
class MyClass
{
public:
MyClass(int mem){member=mem;}
~MyClass(){}
int GetAge()const{ return member;}
private:
int member;
};
/*****+********+** found *************/
void main()
{
int mem=7;
/*************** found ***************/
MyClass myObj=MakeObject(mem);
cout<<"My object has member"<<myObj->GetAge()<<endl;
/***************** found ****************/
delete;
}
MyClass *MakeObject(int mem)
{
MyClass *pMyClass=new MyClass(mem);
return pMyClass;
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!