1 / 17

Chapter 9 Questions

Chapter 9 Questions. 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors and member functions. 3. What is the purpose of using accessibility control? 4. What are the benefits of data field encapsulation?.

zhen
Download Presentation

Chapter 9 Questions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 Questions • 1. What are the difference between constructors and member functions? • 2. Design and implement a simple class as you want, with constructors and member functions. • 3. What is the purpose of using accessibility control? • 4. What are the benefits of data field encapsulation?

  2. Chapter 9 Answers • Q1. : A function to create and initialize an object. • In syntax: No return type; Class name; Called automatically by the system • Q2.: Omitted. • Q3.: to control the access to class members, it is a method to encapsulate data. • To protect the members from access of outside functions. • Q4.: • to realize an “object”: variables and functions are bound • to protect data from directly change from outside of a class • to hide detailed information of object properties • to ease the maintenance of code

  3. Chapter 10 Questions (Submit your answers in the class of next week) 1. Is the following class (in (a)) immutable? Why? 2. Modify the following program (in (b)) to output the following text, but you can’t modify the main function. #include <iostream> using namespace std; int main(){ A a; cout<<"Hello, everyone!\n"; return 0; } class A{ public: A(){ name = "Xyz"; } string *getName(){ return &name; } private: string name; }; Hi, I am coming... Hello, erveryone! Thanks! Bye! (a) (b)

  4. Chapter 10 Questions • Where is the copy constructor called in the following program? Please list the line numbers. 1 class FOO{ 2 public: 3 FOO(inti){ 4 pointer = new int; 5 *pointer = i; 6 } 7 FOO(const FOO& other){ 8 pointer = new int; 9 *pointer = *other.pointer; 10 } 11 ~FOO(){ delete pointer;} 12 int get(){return *pointer;} 13 FOO getObj(){ 14 FOO obj(0); 15 *obj.pointer = *pointer; 16 return obj; 17 } 18 private: 19 int *pointer; 20 }; 21 void display(FOO obj){cout<<obj.get()<<endl;} 22 int main(){ 23 FOO obj1(15); 24 FOO obj2 = obj1; 25 display(obj2.getObj()); 26 }

  5. Chapter 10 Questions • Is there any compiling error in the following program? #include <iostream> using namespace std; class FOO{ public: FOO(int i){ x=i; } friend void display(FOO obj); private: int x; }; void FOO::display(FOO obj){ cout<<obj.x<<endl; } int main(){ FOO obj(1); display(obj); }

  6. Chapter 10 Answers • Q1: No. The function getName() returns the address of name, which can be used to change the value of name although it is a private field. • Q2: • Q3: Line 16, 25, 24 (if RVO is enabled, Line 24 only) • Q4: “error C2039: “display”: 不是“FOO”的成员” A a; string *ps = a.getName(); ps->append("abc"); cout<<ps->data()<<endl; class A{ A(){ cout<<"Hi, I am coming...\n“;} ~A(){cout<<"Thanks! Bye!\n“;}

  7. Chapter 11 Questions • Point out the errors in the following code. Assume the implementation of the classes is correct and omitted due to the limit of space. class DialogBox: BOX{ public: DialogBox(int, int, int, int, char*, int, char*, char*); ~DialogBox(); int select(); private: int draw(); char *ok_button; char *cancel_button; }; //…. The Implementation of the classes is omitted.// int main(){ DialogBoxdlg(0, 0, 15, 13, "test", 1, "OK", "Cancel"); dlg.move(19, 20); } class BOX{ public: BOX(int, int, int, int, char*, int); ~BOX(); int show(); int hide(); int move(int, int); int zoom(int); protected: int draw(); intstart_x, start_y; int width, height; char *title; int color; };

  8. Chapter 11 Questions (con’t) • Write down the output of the following code. class Derived :public Base2, public Base1{ public: Derived( int i, char ch, double db ) : Base1( i ), Base2( ch ), real( db ) { cout<<"Const. Derived.\n"; } double getReal(){ return real; } private: double real; }; int main() { Base1 base1( 10 ); Base2 base2( 'Z' ); Derived derived( 7, 'A', 3.5 ); return 0; } class Base1{ public: Base1( int x ){ cout<<"Const. Base1.\n"; value = x; } int getData(){ return value; } protected: int value; }; class Base2{ public: Base2( char ch ){ cout<<"Const. Base2.\n"; letter = ch; } char getData() const{ return letter; } protected: char letter; };

  9. Chapter 11 Questions (con’t) • Describe the difference between “virtual function” and “abstract function”. • How is the polymorphism enabled? • What is the difference between dynamic casting and static casting?

  10. Chapter 11 Answers • Q1: “dlg.move(19, 20)” will cause a compiling error. The default inheritance is private, so “move()” is treated as a private member of DialogBox. • Q2: Const. Base1. Const. Base2. Const. Base2. Const. Base1. Const. Derived.

  11. Chapter 11 Answers • Q3: an abstract function is a pure virtual function, i.e. a virtual function without function body. It must be overridden in the derived class. • Q4: two elements, pointer of base class and overriding of virtual function. When a pointer of a base class type points to an object of a derived class, and the virtual function is called via the pointer, the function version defined in the derived class rather than the one in the base class would be executed. • Q5: Static casting casts numeric types. It is done in the compiling process. If the cast is invalid, a compiling error will occur. Dynamic casting is used to safely cast a base class pointer into a pointer of a derived class. It is done at runtime. If the cast fails because the real type of the object pointed to is not the type of the desired subclass, the cast returns NULL.

  12. Chapter 13 Questions • Is the following code a correct implementation of “+=”? • Why do you need to overload “=”? Rational Rational::operator+=(Rational &secondRational){ this->add(secondRational); return this; }

  13. Answers • Q1. No, because: • It does not assign the new value to the current object, i.e. the first operand of “+=”; • It returns a wrong value (the correct value is “*this”). • Q2. “=” may be used to “assign” one object to another. Then, a shallow copy is in fact conducted, which may cause problems of sharing pointing target if the constructor or copy constructor involves resources allocation, e.g. the “new” operation. To avoid such problem, we need to overload “=” with deep copy.

  14. Chapter 14 Homework Questions 1. Show the output of the code in Question 14.1 (p445) with the input “94”. 2. Given the following code. If an exception is thrown by statement1, will statement2 be excuted? try{ statement1; }catch(…){ } statement2;

  15. Answers • Q1. • Q2. Yes, if the exception can be caught by the “catch” block; Otherwise, the program may be terminated before the statement 2. Start of try block … End of try block … Continue…

  16. Chapter 15 Homework Questions 1. Design a function template to compute the absolute value of a number. 2. Compare and discuss the advantages/disadvantages of function template and overloading?

  17. Answers template <typename T> T abs(T orig){ return orig>0?orig:-orig; } int main(){ cout<<abs(0)<<endl; cout<<abs(-1)<<endl; cout<<abs(0.5)<<endl; cout<<abs('a')<<endl; } • A1: • A2:

More Related