L2: n=10 L3: m=20 L4: n=0
L5: m=-858993460 L6: p2=0xCCCCCCCC
3、 阅读程序,请根据带默认参数的函数定义与调用原则,写出标有行号L*的
程序的输出结果。(P58,第10(3)题,ex2-10-3.cpp) #include L2: 2000 L3: 500 L4: 1000 4、 根据引用和返回引用的原理,阅读程序,请写出标有行号L*的程序的输出 结果。(P58,第10(4)题,ex2-10-4.cpp) #include L2: 20 L3: 100 L4: 300 4.3. 第3章 类与对象(P99~100) 1、 请阅读程序,根据对象的构造原则和构造顺序,写出程序的输出结果。(P99, 第9(1)题,ex3-9-1.cpp) #include int a; char *b; float c; public: X(int x1, char *x2, float x3):a(x1),c(x3){ //构造函数1 b=new char[sizeof(x2)+1]; strcpy(b,x2); } X():a(0),b(\ //构造函数2 } X(int x1,char *x2=\构造函数3 } X(const X& other){ //构造函数4 a=other.a; b=\ c=other.c; } void print(){ cout<<\ } }; void main(){ X *A=new X(4, \ X B, C(10), D(B); A->print(); //L1 B.print(); //L2 C.print(); //L3 D.print(); //L4 } 答: L1: a=4, b=X::X(int,char,float), c=32 L2: a=0, b=X::X(), c=10 L3: a=10, b=X::X(....), c=10 L4: a=0, b=X::X(const X &other), c=10 2、 请阅读程序,根据对象指针的使用原则,写出程序的运行结果。(P100,第9 (2)题,ex3-9-2.cpp) #include class Implementation { //类Implementation public: Implementation(int v) { value = v; } void setValue(int v) { value = v; } int getValue() const { return value; } private: int value; }; class Interface { //类Interface public: Interface( int ); //声明 void setValue( int ); int getValue() const; private: Implementation *ptr;//对象指针 }; Interface::Interface(int v):ptr(new Implementation(v)){ } void Interface::setValue(int v) { ptr->setValue(v); } int Interface::getValue() const {//常量函数:不修改数据成员的值 return ptr->getValue(); } void main(){ Interface i(5); cout< cout< L1: 5 L2: 10 3、 阅读程序,根据对象成员的构造原则和构造顺序,请按顺序写出程序的运行 结果。(P100,第9(3)题,ex3-9-3.cpp) #include int x; public: A():x(0){ cout<<\ A(int i):x(i){ cout<<\ ~A(){ cout<<\}; class B{ int y; A X1; A X2[3]; public: B(int j):X1(j),y(j){ cout<<\ ~B(){ cout<<\}; void main(){ A X1(1), X2(2); B B1(3); } 答: 从上到下X1 constructor... 依次为: X2 constructor... X3 constructor... constructor A() called... constructor A() called... constructor A() called... B3 constructor... B3 destructor... X0 destructor... X0 destructor... X0 destructor... X3 destructor... X2 destructor... X1 destructor... 4.4. 第4章 继承(P131~132) 1、 请阅读程序,根据对象成员的含对象成员的派生类对象的构造顺序和构造原 则,按顺序写出程序的运行结果。(P131,第9(1)题,ex4-9-1.cpp) #include A(int a,int b):x(a),y(b){ cout<<\ void Add(int a,int b){ x+=a;y+=b;} void display(){ cout<<\ ~A(){ cout<<\private: int x,y; }; class B:private A{ private: int i,j; A Aobj; public: B(int a,int b,int c,int d):A(a,b),i(c),j(d), constructor...\ void Add(int x1,int y1, int x2,int y2){ A::Add(x1,y1); i+=x2; j+=y2; } void display(){ A::display(); Aobj.display(); cout<<\ } ~B(){cout<<\ }; void main(){ B b(1,2,3,4); b.display(); b.Add(1,3,5,7); b.display(); } 答: 从上到下A constructor... 依次为: A constructor... B constructor... (1,2)(1,1)(3,4) (2,5)(1,1)(8,11) destructor B... destructor A... destructor A... Aobj(1,1){ cout<<\ 2、 请阅读程序,根据对象成员的含对象成员的派生类对象的构造顺序和构造原 则,按顺序写出程序的运行结果。(P131,第9(2)题,,ex4-9-2.cpp) #include