精華區beta NTU-Exam 關於我們 聯絡資訊
課程名稱︰計算機程式 課程性質︰C++ How to Program Fourth Edition前10章 課程教師︰林宗男 開課系所︰電機系 考試時間︰2006/1/11 試題 : 1.In the function definition below, mark out the definitely illegal statements (ones that will give a compile error). Assume that the Thunk class has a public int member variable named x. Your score on this question will be number right minus number wrong. void foo(Thunk t, const Thunk ct, Thunk & rt, const Thunk &crt, Thunk * tp, const Thunk * ctp) { // one point for each statement: t.x = 3; ct.x = 3; rt.x = 3; crt.x = 3; tp->x = 3; ctp->x = 3; } (8 points) 2.What is the output of the following code? (10 points) class Time( public: Time(int=0,int=0,int=0); Time setHour(int); Time setMinute(int); Time setSecond(int); void printUniversal() const; private: int hour; int minute; int second; }; Time::Time(int hr, int min, int sec) { hour=hr; minute=min; second=sec; } Time Time::setHour(int h) { hour=(h>=0 && h<24)?h:0; return *this; //enables cascading } Time Time::setMinute(int m) { minute=(m>=0 && m<60)?m:0; return *this; } Time Time::setsecond(int s) { second=(s>=0 && s<60)?s:0; return *this //enables cascading } void Time::printUniversal()const { cout<<setfill('0')<<setw(2)<<hour<<":" <<setw(2)<<minute<<":" <<setw(2)<<second; } int main() { Time t; t.setHour(18).setMinute(30).setSecond(22); cout<<"Universal time: "; t.printUniversal(); } 3.The following is a member function from a class similar to <string> in that it manages a piece of dynamically allocated memory containing a C-string, whose starting address is in a member variable named ptr. There are no syntax errors in this code. (5 points) Symbol& Symbol::operator= (const Symbol& rhs) { delete[] ptr; ptr = new char[strlen(rhs.ptr)]; strcpy(ptr, rhs.ptr); return *this; } But there are two things wrong with this function. Write one sentence for each of them. 4.The following is another member function from a class similar to <string> in that it manages a piece of dynamically allocated memory containing a C-string, whose starting address is in a member variable named ptr. Thereare no syntax errors in this code.(5 points) Symbol::Symbol(const Symbol& t) { delete[] ptr; ptr = new char[strlen(t.ptr)]; strcpy(ptr, t.ptr); } There are also two things wrong with this function. Write one sentence for each of them. 5.Consider the following bit of code. Based just on what is shown, which member function of class Gizmo will get called?(15 points) Assume that any functions that could get called are in fact defined in the Gizma class, and willget called. Write your answer on the same line as the statement that clearly corresponds to where the function gets called. You can just provide the standard names of the functions; you don't have to show there prototypes or their code. If a statement results in more than one member function being called, be sure that it is clear which statement the function names go with. Finally, be sure that you write the name of the function only once for each time it is called - if you think there is more than one place where you could write down where a function is called, then pick just one of them. The first one is shown as an example. Gizmo foo (Gizmo g); Gizmo goo (Gizmo & x); int main() { Gizmo a; default constructor (example provided - do not score) Gizmo b(23); a = foo(b); b = goo(a); return 0; } Gizmo foo (Gizmo g) { Gizmo h(g); h.dosomething();// no answer needed for this call return h; } Gizmo goo(Gizmo & x) { x.dosomethingelse();// no answer needed for this call return x; } 6.Consider the following three class definitions(read the columns left to right).(15 points) class A{ class B:public A{ class C:public B{ public: public: public: void foo(); void bar(); void zap(); int a; int i; int x; protected: protected: protected: int b; int j; int y; private: private: private: int c; int k; int z; }; }; }; Here are the definitions for the member functions. Look at each statement that refers to a variable. Mark out (draw a line through) each statement that is illegal (will result in a compile error); leave legal statements alone. Your score on this question will be the number right minus the number wrong, divided by three, rounded up to the nearest integer, 15 pts max. A::foo() B::bar() C::zap() { { { a=1; a=1; a=1; b=2; b=2; b=2; c=3; c=3; c=3; i=4; i=4; i=4; j=5; j=5; j=5; k=6; k=6; k=6; x=7; x=7; x=7; y=8; y=8; y=8; z=9; z=9; z=9; } } } 7.Consider the following String class:(30 points) class String{ public: String(const char*value); ~String(); virtual void foo() {cout<< "Hello!" <<endl;} virtual int f() const { cout<< "String::f()\n"; return 1;} virtual void f(double) const{} virtual void g() const {} ... private: char *data; }; (1) Please implement the constructor String(const char*value) (7 points) (2) Please note that there is no assignment operator or copy constructor defined in this class. Please describe the potential problems of the following code: (8 points) String a("Hello"); String t("World"); { String b("Goodby"); ... b=a; } String c = a; (3) How to fix the problems? Please implement the appropriate member functions .(5 points) (4)A derived class SubString is inherited from String (10 points) class SubString : public String{ public: void foo() {cout<< "Goodbye!" << endl;} }; (a) What is the output from the following code? String * p1 = new String; String * p2 = new SubString; SubString * p3 = new SubString; p1->foo(); p2->foo(); p3->foo(); (b) Suppose that the word "virtual" is removed from the declaration of the String class shown above. Now what is the output from the same code? 8.Consider carefully the following C++ program. This program includes a class hierarchy containing three classes and a main program which uses these three classes. You must examine this program and then produce the output which the program would create. You must show this program output exactly as it would be produced by the program, output line by output line.(20 points) #include<iostream.h> class A { public: virtual~A(){cout<<"\tdestruct A!\n";} virtual void vf(){cout<<"\texecuting A::vf.\n";} void rg(){cout<<"\texecuting A::rg.\n;} }; class B : public A { public: ~B(){cout<<"\tdestruct B!\n;} void vf(){cout<<"\texecuting B::vf.\n";} void rg(){cout<<"\texecuting B::rg.\n";} virtual void vbf(){cout<<"\texecuting B::vbf.\n";} void rbf(){cout<<"\texecuting B::rbf.\n";} }; classC : public B { public: ~C(){cout<<"\tdestruct C!\n";} void vbf(){cout<<"\texecuting C::vbf.\n";} void rbf(){cout<<"\texecuting C::rbf.\n";} }; int main() { A * ab = new B; A * ac = new C; B * bc = new C; C * c = new C; cout<<"calls for the rg functions:"<<endl; ab->rg(); ac->rg(); bc->rg(); c->rg(); cout<<"calls for the vf functions:"<<endl; ab->vf(); ac->vf(); bc->vf(); c->vf(); cout<<"last two function calls:"<<endl; bc->vbf(); bc->rbf(); cout<<"now delete c:"<<endl; delete c; cout<<"now delete ab:"<<endl; delete ab; cout<<"now delete ac:"<<endl; delete ac; cout<<"now delete bc:"<<endl; delete bc; cout<<"all done, folks!"<<endl; return 0; } Write the exact program output. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.87.206.107