题目
(1)定义comp的构造函数,函数含参数x和y,它们都是in,型的数据,默认值都为0,请使用参数列表的形式分别将类数据成员a和b初始化x和y的值。请在注释“//**1**”之后添加适当的语句。
(2)完成类comp的成员函数input(int x,int y)的定义,将int型的参数x和y分别赋值给数据成员s和b,请在注释“//**2**”之后添加适当的语句;
(3)完成类comp的友元函数friend compplus(comp &x,comp &y)的定义,该函数首先要定义一个comp类的对象c,然后将参数x和y的数据成员a与b分别相加并分别赋值给c的成员a和b,最后返回c的值,请在注释“//**3**”之后添加适当的语句。输出结果如下:
10+20i
2+3i
result:12+23i
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件test20_3.cpp清单如下:
include <iostream.h>
class comp
{
int a;
int b;
public:
//**1**
friend comp plus(comp &x, comp &y);
void input(int x,int y)
{
//**2**
}
void output ()
{
cout<<a<<'+'<<b<<"i"<<end1;
}
};
comp plus(comp &x, comp &y)
{
//**3**
c.b=x.b+y.b;
return c;
}
void main()
{
comp x(10,20),y,z;
y.input(2,3);
z=plus(x,y);
x.output();
y.output();
cout<<"result:";
z.output();
}
第1题
注意:不得删行或增行,也不得更改程序的结构。
源程序文件RevMain12.cpp中的程序清单如下:
//RevMain12.cpp
include<iostream>
/* * * * FOUND * * * * */
using namespace std;
class test
{
private:
const int value;
char dep[10];
public:
/* * * * *FOUND* * * * */
test()
{
value=0;
strcpy(dep,"m");
}
/* * * * *FOUND* * * * */
test(int newvalue)
{
value=newvalue;
strcpy (dep, "m");
}
/* * * * *FOUND * * * * */
void show()
{
cout<<"value= "<<value<<end1;
}
};
int main ()
{
test t1;
const test t2;
t1.show ();
t2.show();
return 0;
}
第2题
使用VC6打开考生文件夹下的工程test18_1,此工程包含一个源程序文件(est18_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Enter x and y:
-4 -5
xoffset=1
yoffset=1
angle=45
radius=1.41421
源程序文件test18_1.cpp 清单如下:
include <iostream.h>
include <math.h>
class point
{
public:
void set(double ix,double iy)
{
x=ix;
y=iy;
}
double xoffset()
{
return x;
}
double yoffset()
{
return y;
}
double angle ()
{
return (180/3.14159)*atan2(y,x);
}
/**************** found *******************/
inline radius ()
{
return sqrt(x*x+y*y);
}
protected:
double x;
double y;
};
void main()
{
point p;
double x,y;
cout<<"Enter x and y:\n";
cin>>x>>y;
p.set(x,y);
/**************** found *******************/
p.x+=5;
p.y+=6;
/**************** found *******************/
cout<<"xoffset="<<p.xoffset()<<end1;
cout<<"yoffset="<<p.yoffset()<<end1;
cout<<"angle="<<p.angle()<<end1;
cout<<"radius="<<p.radius()<<end1;
第3题
5
5
源程序文件test28_1.cpp清单如下:
include <iostream.h>
class A{
public:
/***************** found *****************
static int n=1;
A(){n++;};
~A() (n--;};
};
/***************** found *****************
int n=0;
int main() {
A a;
A b[3];
A *c=new A;
c=&a;
/***************** found *****************
cout<<c.n<<end1;
cout<<A::n<<end1;
return 0;
}
第4题
The grade is 3
源程序文件test21_1.cpp清单如下:
include<iostream.h>
class student
{
private:
int grade;
public:
/**************** found*******************/
student(int thegra):(thegra){}
~student(){}
int get_grade(){return grade;}
};
void main()
{
int thegra=3;
/**************** found*******************/
student point=new student(thegra);
/**************** found*******************/
cout<<"The grade is"<<point.get_grade()<<endl;
delete point;
}
第5题
源程序文件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;
}
第6题
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;
}
第7题
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;
}
第8题
David 3123 1000
源程序文件test34_1.cpp清单如下:
include <iostream.h>
include <string.h>
class person
{
public:
char name[20];
unsigned long id;
float salary;
void print(){cout<<name<<' '<<id<<' '<<salary<<
/***************** found *****************/
}
void main()
{
person p;
person *ptr;
/***************** found *****************/
ptr=p;
/***************** found *****************/
strcpy("David",ptr->name);
ptr->id=3123;
ptr->salary=1000;
ptr->print();
}
第9题
源程序文件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;
}
第10题
7
源程序文件test41_1.cpp清单如下:
include<iostream.h>
class myclass
{
int a, b;
public:
/***************** found *****************/
int sum(myclass x);
void set_ab(int i, int j);
}:
/**************** found ****************/
void myclass:set_ab(int i, int j)
{
a=i;
b=j;
}
int sum (myclass x)
{
/***************** found ***************/
x.a+x.b;
}
void main ()
{
myclass n;
n.set_ab (3, 4);
cout <<sum(n)<<endl;
}
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!