C++读程题,求大神解答 100
1.给出下述源程序的运行结果。#include<iostream.h>voidfun(inti,int&j){j=i*3;}voidmain(){inta,b;fun(3...
1.给出下述源程序的运行结果。
#include <iostream.h>
void fun(int i, int &j)
{ j = i * 3; }
void main()
{ int a, b;
fun(3, a);
fun(9, b);
cout << "a + b = " << a + b << endl;
}
2.给出下述源程序的运行结果。
#include <iostream.h>
void main() {
int a = 5;
do {
a++;
cout << ++a << endl;
if(a == 10) break;
}while(a == 9 )
cout << "ok\n"; }
3.给出下述源程序的运行结果。
#include <iostream.h>
int add(int *x, int n)
{ int sum , i;
for(sum = 0, i = 0; i < n; i++) sum += *(x+i);
return sum;
}
void main(){
int arr[] = {1, 3, 5, 7};
cout << add(arr, 4) << endl;
}
4.给出下述源程序的运行结果。
#include <iostream.h>
class A {
public:
A() { a1 = a2 = 0; cout << "Default constructor called.\n"; }
A(int i, int j) { a1 = i; a2 = j; cout << "Constructor called.\n"; }
void print() { cout << "a1 = " << a1 <<" , " << "a2 = " << a2 << endl; }
private:
int a1, a2;
};
void main() {
A x, y(3,9);
x.print();
y.print();
} 展开
#include <iostream.h>
void fun(int i, int &j)
{ j = i * 3; }
void main()
{ int a, b;
fun(3, a);
fun(9, b);
cout << "a + b = " << a + b << endl;
}
2.给出下述源程序的运行结果。
#include <iostream.h>
void main() {
int a = 5;
do {
a++;
cout << ++a << endl;
if(a == 10) break;
}while(a == 9 )
cout << "ok\n"; }
3.给出下述源程序的运行结果。
#include <iostream.h>
int add(int *x, int n)
{ int sum , i;
for(sum = 0, i = 0; i < n; i++) sum += *(x+i);
return sum;
}
void main(){
int arr[] = {1, 3, 5, 7};
cout << add(arr, 4) << endl;
}
4.给出下述源程序的运行结果。
#include <iostream.h>
class A {
public:
A() { a1 = a2 = 0; cout << "Default constructor called.\n"; }
A(int i, int j) { a1 = i; a2 = j; cout << "Constructor called.\n"; }
void print() { cout << "a1 = " << a1 <<" , " << "a2 = " << a2 << endl; }
private:
int a1, a2;
};
void main() {
A x, y(3,9);
x.print();
y.print();
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询