使用VC6打开考生文件夹下的工程test22_1,此工程包含—个源程序文件test22_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果为:
6/15
3/4
9/19
源程序文件test22_1.cpp清单如下:
include<iostream.h>
class Franction
{
int nume;
int deno;
public:
Franction FranAdd(const Franction& x)
{
this->nume+=x.nume;
this->deno+=x.deno;
/****************found*******************/
return this;
}
void InitFranction() {nume=0; deno=l;}
void InitFranction(int n, int d) {nume=n; deno=d;}
void FranOutput() {cout<<nume<<'/'<<deno<<endl;}
};
void main()
{
Franction *a=new Franction;
Franction *b=new Franction;
a->InitFranction(6,15);
b->InitFranction(3,4);
a->FranOutput();
b->FranOutput();
Franction c;
/****************found*******************/
c->InitFranction();
/****************found*******************/
c=a->FranAdd(b);
c. FranOutput();
}
第1题
使用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);
}
第2题
使用VC6打开考生文件夹下的工程test40_3。此工程包含一个test40_3.cpp,其中定义了类CVector,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)完成类CVector构造函数的定义,把传入的int型参数a和b分别赋值给类的成员变量x和y。请在注释“//**1**”之后添加适当的语句。
(2)完成类CVector的“+”运算符重载函数的定义,使Cvector类的两个对象相加时可以直接使用符号“+”,并且结果仍然为类Cvector的对象,请在注释“//**2**”之后添加适当的语句。
(3)在主函数中添加适当的语句,把类Cvector的对象a和b相加,并且把结果保存在对象c中,最后以“x,y”的形式输出在屏幕上,请在注释“//**3**”之后添加适当的语句。
源程序文件test40_3.cpp清单如下:
include <iostream.h>
class CVector
{
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b)
{
//**1**
}
CVector CVector::operator+ (CVector param)
{
//**2**
temp.y = y + param.y;
return (temp);
}
int main ()
{
CVector a (3,1);
CVector b (1,2);
CVector c;
//**3**
return 0;
}
第3题
(1)定义CStudent的构造函数,函数含参数nm,它是char型的指针数据,请使用字符串函数将类数据成员name初始化。请在注释“//**1**”之后添加适当的语句。
(2)完成类CStudent的成员函数void addscore(double sc)的定义,将类数据成员score加上参数sc,请在注释“//**2**”之后添加适当的语句。
(3)添加类CGraduateStudent的成员函数void addpaper(int d)的定义,将int型的参数d的值加到类的数据成员paper上,请在注释“//**3**”之后添加适当的语句。
(4)调用对象sdt的成员函数addpaper使得该考生的论文数量变为10,将请在注释“//**4**”之后添加适当的语句。
输出结果如下:
Student Hanson
(Advisor:MichaelPhong)
Total paper is 10
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件test22_3.cpp清单如下:
include<iostream.h>
include<string.h>
class CStudent
{
private:
char name[30];
int code;
double score;
public:
CStudent(char* nm)
{
//**1**
}
CStudent(){}
void putname(char* nm){ strcpy(name,nm);}
void putcode(int d){code=d;}
void putscore(double sc){score=sc;}
void getname(char* nm){strcpy(nm,name);}
double getcode(){return code;)
double getscore(){return score;}
void addscore(double sc)
{
//**2**
}
};
class CGraduateStudent:public CStudent
{
private:
char advisor[30];
int paper;
public:
void putadvisor(char*nm){ strcpy(advisor, nm);}
void getadvisor(char*nm){ strcpy(nm, advisor);}
void putpaper(int d){ paper=d;}
//**3**
int getpaper(){ return paper;}
};
void main()
{
CGraduateStudent sdt;
sdt.putname("Hanson");
sdt.putadvisor("MichaelPhong");
sdt.putpaper(8);
//**4**
char csname[30];
sdt.getname(csname);
char caname[30];
sdt.getadvisor(caname);
cout<<"Student"<<csname<<endl;
cout<<"(Advisor:"<<caname<<")"<<endl;
cout<<"Total paper is"<<sdt.getpaper()<<endl;
}
第4题
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();
}
第5题
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();
}
第6题
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;
}
第7题
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();
}
第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题
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();
}
第10题
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;
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!