C++第三章习题
- 写出程序的运行结果:
#include <iostream>
using namespace std;
class toy
{
public:
toy(int q, int p)
{
quan = q;
price = p;
}
int get_quan() { return quan; }
int get_price() { return price; }
private:
int quan, price;
};
int main()
{
toy op[3][2] = {toy(10, 20), toy(30, 48), toy(50, 68), toy(70, 80), toy(90, 16), toy(11, 120)};
for (int i = 0; i < 3; i++)
{
cout << op[i][0].get_quan() <<”, ”;
cout << op[i][0].get_price() <<”\n”;
cout << op[i][1].get_quan() <<”, ”;
cout << op[i][1].get_price() <<”\n”;
}
cout << endl;
return 0;
}
13. 写出程序的运行结果:
#include <iostream>
using namespace std;
class example
{
public:
example(int n)
{
i = n;
cout <<"Constructing\n";
}
~example() { cout <<"Destructing\n"; }
int get_i() { return i; }
private:
int i;
};
int sqr_it(example o) { return o.get_i() * o.get_i(); }
int main()
{
example x(10);
cout << x.get_i() << endl;
cout << sqr_it(x) << endl;
return 0;
}
14. 写出程序的运行结果:
#include <iostream>
using namespace std;
class aClass
{
public:
aClass() { total++; }
~aClass() { total--; }
int gettotal() { return total; }
private:
static int total;
};
int aClass::total = 0;
int main()
{
aClass o1, o2, o3;
cout << o1.gettotal() <<"objects in existence\n";
aClass *p;
p = new aClass;
if (!p)
{
cout <<"Allocation error \n";
return 1;
}
cout << o1.gettotal();
cout <<"ojects in existence after allocation\n";
delete p;
cout << o1.gettotal();
cout <<"ojects in existence after deletion\n";
return 0;
}
15. 写出程序的运行结果:
#include <iostream>
using namespace std;
class test
{
public:
test();
~test() {}
private:
int i;
};
test::test()
{
i = 25;
for (int ctr = 0; ctr < 10; ctr++)
{
cout << "Counting at" << ctr << "\n";
}
}
test anObject;
int main() { return 0; }
16. 写出程序的运行结果:
#include <iostream>
using namespace std;
class A
{
int a, b;
public:
A()
{
a = 0;
b = 0;
cout <<"Default constructor called.\n";
}
A(int i, int j)
{
a = i;
b = j;
cout <<"Constructor : a ="<< a <<", b ="<< b << endl;
}
};
int main()
{
A a[3];
A b[3] = {A(1, 2), A(3, 4), A(5, 6)};
return 0;
}
17. 写出程序的运行结果:
#include <iostream>
using namespace std;
class Test
{
int val;
public:
Test() { cout <<"default."<< endl; }
Test(int n)
{
val = n;
cout <<"Con."<< endl;
}
Test(const Test &t)
{
val = t.val;
cout <<"Copy con."<< endl;
}
};
int main()
{
Test t1(6);
Test t2 = t1;
Test t3;
t3 = t1;
return 0;
}
18. 写出程序的运行结果:
#include <iostream>
using namespace std;
class N
{
int A;
static int B;
public:
N(int a)
{
A = a;
B += a;
}
static void f1(N m);
};
void N::f1(N m)
{
cout <<"A ="<< m.A << endl;
cout <<"B ="<< B << endl;
}
int N::B = 0;
int main()
{
N P(5), Q(9);
N::f1(P);
N::f1(Q);
return 0;
}
19. 写出程序的运行结果:
#include <iostream>
using namespace std;
class M
{
int x, y;
public:
M() { x = y = 0; }
M(int i, int j)
{
x = i;
y = j;
}
void copy(M *m);
void setxy(int i, int j)
{
x = i;
y = j;
}
void print() { cout << x << ","<< y << endl; }
};
void M::copy(M *m)
{
x = m->x;
y = m->y;
}
void fun(M m1, M *m2)
{
m1.setxy(12, 15);
m2->setxy(22, 25);
}
int main()
{
M p(5, 7), q;
q.copy(&p);
fun(p, &q);
p.print();
q.print();
return 0;
}