Chapter 9 Questions

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

description

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?. - PowerPoint PPT Presentation

Transcript of Chapter 9 Questions

Page 1: 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?

Page 2: Chapter 9 Questions

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

Page 3: Chapter 9 Questions

04/20/23

CH10 Homework QuestionsIs there any compiling error in the following

program?

3

#include <iostream>using namespace std;

class FOO{public:

FOO(int i){x=i;

}void print(FOO obj);

private:int x;

};

#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();}

void print() { cout<<x<<endl; }

void display(FOO obj){cout<<obj.x<<endl;

}int main(){

FOO obj(1);display(obj);

obj.print();}

Page 4: Chapter 9 Questions

CH10 Answers

• "print"是“ FOO”的成员,实现时要用“ FOO::”

• “print”的声明与实现的参数表不一致。• “display” 是“ FOO”的成员不能访问 FOO的成员 x。

Page 5: Chapter 9 Questions

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.

5

#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;}

Page 6: Chapter 9 Questions

CH11 Homework Questions (con’t)

3. Where is the copy constructor called in the following program? Please list the line numbers.

6

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;}

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 }

Page 7: Chapter 9 Questions

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.

7

#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;}

x=10, y=10x=100, y=100Runtime error.

Page 8: Chapter 9 Questions

Chapter 11 Answers

• Q3: Line 16/21, 25, 24 (if RVO is enabled, Line 24 only)

Page 9: Chapter 9 Questions

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?

Page 10: Chapter 9 Questions

Answers• A1:

• A2:

10

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;

}

Overloading Template

Advantage Flexible: Various parameter lists, return types Various function bodies Implicit type conversion Operator overloading

Simple: Single function code

Flexible: unspecified data type

Disadvantage Complex: Repeated code

In flexible: only known data type

Inflexible: Uniform parameter list Uniform function body , return type

Page 11: Chapter 9 Questions

Chapter 14 Questions

1. Is the following code a correct implementation of “+=”?

2. Why do you need to overload “=”?

11

Rational Rational::operator+=(Rational &secondRational){ this->add(secondRational); return this;}

Page 12: Chapter 9 Questions

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.

12

Page 13: Chapter 9 Questions

13

Chapter 15 Questions1. 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 BOX{public: BOX(int, int, int, int, char*, int); ~BOX(); int show(); int hide(); int move(int, int); int zoom(int);protected: int draw(); int start_x, start_y; int width, height; char *title; int color;};

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(); int start_x, start_y; int width, height; char *title; int color;};

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 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);}

Page 14: Chapter 9 Questions

14

Chapter 15 Questions (con’t)2. Write down the output of the following code.

class Base1{public: Base1( int x ){ cout<<"Const. Base1.\n"; value = x; } int getData(){ return value; } protected: int value; };

class Base1{public: Base1( int x ){ cout<<"Const. Base1.\n"; value = x; } int getData(){ return value; } protected: int value; };

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 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 Base2{public: Base2( char ch ){ cout<<"Const. Base2.\n"; letter = ch; } char getData() const{ return letter; } protected: char letter; };

class Base2{public: Base2( char ch ){ cout<<"Const. Base2.\n"; letter = ch; } char getData() const{ return letter; } protected: char letter; };

Page 15: Chapter 9 Questions

15

Chapter 15 Questions (con’t)3. Describe the difference between “virtual

function” and “abstract function”.4. How is the polymorphism enabled?5. What is the difference between dynamic

casting and static casting?

Page 16: Chapter 9 Questions

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:

16

Const. Base1.Const. Base2.Const. Base2.Const. Base1.Const. Derived.

Page 17: Chapter 9 Questions

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.

17