Document 10805474

advertisement
Short Quiz 8 1) What is the relation between a reference and a pointer ? (same thing/ they are different perspective of the same piece of memory) 2) Below is a demo of a struct and a class struct X class Y { { private: double c,d,e; double c, d, e; public: public: double t();
double t(); void done();
void done(); };
}; int B::t() int X::t() { { return c-­‐d-­‐e; return c-­‐d-­‐e; } } void B::done() void X::done() {
{ c=d=e=7; c = d =e =7; }
} In terms of data type, are they the same? Why? Yes. They are equivalent in terms memory management and compiler implementation. 3) What is the output of follow program ? Why? #include <iostream> using namespace std; class X { public: int i; X(); // Constructor }; X::X(){} int main(){ X a; cout<<"the i is : "<<a.i<<endl; return 0; } Ans: the i is : 0 4) After a class Z running out of its live cycle, what happen to class Z and every field/property defined in it ? Ans: When z runs out of live cycle, the object is destroyed and all data fields in heap are garbage collected. 5) if we following 5 functions : void overload( int x ); void overload ( short y ) ; void overload( long z ); void overload( float a ); void overload ( double b); Are the allowable in C++? If they are, how C++ compiler handle them ? If they are not, why ? Ans: They are allowed in C++. C++ has type system to identify each function’s input and output. Then C++ compiler will compile source code into corresponding label. Take overload( float a) as example, it will be compiled into a lablel: overload_float: 6) if we following 5 functions : int overload( int x ); short overload ( int x ) ; long overload( int x); float overload( int x ); double overload ( int x ); Are the allowable in C++? If they are, how C++ compiler handle them ? If they are not, why ? Ans: No. Because type system in C++ is many-­‐one mapping. The type system can not enumerate the program’s return types. 7) In following program, there is a statement static const char* s, where is it located? ( stack segment/ heap segment / static data segment ) Why do we use this type of storage? #include <iostream> using namespace std; char oneChar(const char* charArray = 0) { static const char* s; if(charArray) { s = charArray; return *s; } if(*s == '\0') return 0; return *s++; } char* a = "abcdefghijklmnopqrstuvwxyz"; int main() { oneChar(a); char c; while ((c = oneChar()) != 0) { cout<< c <<endl; } } This data field is supposed to be shared with all objects of the same class/structure. 8) What will be printed out based on the following program ? #include <iostream> using namespace std; class PPY { int myPPY; public: PPY(int k = 0) : myPPY(k) {} ~PPY() { cout << "deconstructor PPY::~PPY()" << endl; } }; void gp() { static PPY ppy1(47); static PPY ppy2(399); static PPY ppy3(756); } int main() { gp(); } deconstructor PPY::~PPY() deconstructor PPY::~PPY() deconstructor PPY::~PPY() 9) What is the result of following program ? #include <iostream> using namespace std; class A{ int i; public: A(int ii) : i(ii) {} ~A(){ cout<<" the deconstructor of A "<<endl; } void f( ) const{ cout<<" f from class A "<<endl; } }; class B{ int i; public: B(int ii) : i(ii) {} ~B(){ cout<<" the deconstructor of B "<<endl; } void f() const{ cout<<" overriden f from class B "<< endl; } }; class C : public B{ A a; B b; public: C(int ii) : B(ii), a(ii), b(ii) {} ~C() {} // override void f() const{ a.f(); B::f(); b.f(); } }; int main(){ C c(47); c.f(); } f from class A overriden f from class B overriden f from class B the deconstructor of B the deconstructor of A the deconstructor of B 10) What is the result of following program ? #include <iostream> using namespace std; enum place { Boston, NewYork, LA }; // Etc. class BallGame { public: void play(place) const { cout<<"BallGame::play" <<endl; } }; class BasketBallGame : public BallGame { public: // Redefine interface function: void play(place) const { cout<< "BasketBallGame::play" <<endl; } }; void family(BallGame& b) { b.play(LA); } int main() { BasketBallGame bbg; family(bbg); return 0; } BallGame::play 
Download