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?.

tryna
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. CH10 Homework Questions Is there any compiling error in the following program? #include <iostream> using namespace std; class FOO{ public: FOO(int i){ x=i; } void print(FOO obj); private: int x; }; void print() { cout<<x<<endl; } void display(FOO obj){ cout<<obj.x<<endl; } int main(){ FOO obj(1); display(obj); obj.print(); } 3

  4. CH10 Answers "print"是“FOO”的成员,实现时要用“FOO::” “print”的声明与实现的参数表不一致。 “display”是“FOO”的成员不能访问FOO的成员x。

  5. CH 11 Homework Questions Q1. Write statements to declare: A) a pointer pointing to an array of three “int” elements. B) a pointer pointing to “int” variables. C) an array of pointers, each of which is a pointer of int. Q2. Analyze and write down the output of the following codes. #include <iostream> #include <cstring> using namespace std; int main() { char *p1, * p2, s[50]= "xyz"; p1 = "abcd"; p2 = "ABCD"; strcpy(s+2, strcat(p1+2,p2+1)); cout << s <<endl; } #include <iostream> #include <cstring> using namespace std; int main() { int x = 10, &y = x; cout << "x=" <<x << ", y=" <<y <<endl; int * p = &y; *p = 100; cout << "x=" <<x << ", y=" <<y <<endl; } 5

  6. CH11 Homework Questions (con’t) 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 } 1 class FOO{ 2 public: 3 FOO(int i){ 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;} Where is the copy constructor called in the following program? Please list the line numbers. 6

  7. Chapter 11 Answers Q1. Write statements to declare: A) a pointer pointing to an array of three “int” elements: int (*p)[3]; B) a pointer pointing to “int” variables: int *p; C) an array of pointers, each of which is a pointer of int: int *p[3]; Q2. Analyze and write down the output of the following codes. #include <iostream> #include <cstring> using namespace std; int main() { int x = 10, &y = x; cout << "x=" <<x << ", y=" <<y <<endl; int * p = &y; *p = 100; cout << "x=" <<x << ", y=" <<y <<endl; } #include <iostream> #include <cstring> using namespace std; int main() { char *p1, * p2, s[50]= "xyz"; p1 = "abcd"; p2 = "ABCD"; strcpy(s+2, strcat(p1+2,p2+1)); cout << s <<endl; } x=10, y=10 x=100, y=100 Runtime error. 7

  8. Chapter 11 Answers Q3: Line 16/21, 25, 24 (if RVO is enabled, Line 24 only)

  9. CH12 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?

  10. 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:

  11. Chapter 14 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; }

  12. 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”); • Return type should be “&”. • 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.

  13. Chapter 15 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(){ DialogBox dlg(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; };

  14. Chapter 15 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; };

  15. Chapter 15 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?

  16. Chapter 15 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.

  17. Chapter 15 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.

More Related