From ad9cd41f0e1d218da9cbbb6f424e5dc8423c53de Mon Sep 17 00:00:00 2001 From: pezy_mbp Date: Fri, 16 Jan 2015 00:19:34 +0800 Subject: [PATCH] added 13.18 ~ 21 --- ch13/README.md | 25 +++++++++ ch13/ex13.14.15.16.cpp | 86 ------------------------------ ch13/ex13.18.19.cpp | 70 ------------------------ ch13/ex13.9.10.11.13.cpp | 112 --------------------------------------- ch13/ex13_17.cpp | 5 +- ch13/ex13_18.cpp | 25 +++++++++ ch13/ex13_18.h | 33 ++++++++++++ ch13/ex13_19.h | 35 ++++++++++++ 8 files changed, 122 insertions(+), 269 deletions(-) delete mode 100644 ch13/ex13.14.15.16.cpp delete mode 100644 ch13/ex13.18.19.cpp delete mode 100644 ch13/ex13.9.10.11.13.cpp create mode 100644 ch13/ex13_18.cpp create mode 100644 ch13/ex13_18.h create mode 100644 ch13/ex13_19.h diff --git a/ch13/README.md b/ch13/README.md index 11503d3b..3546cdfb 100644 --- a/ch13/README.md +++ b/ch13/README.md @@ -135,3 +135,28 @@ Yes, the output will change. cause we don't use the synthesized copy-control mem Yes, the output will change. cause the function `f` haven't any copy operators. Thus, the output are the same when pass the each object to `f`. ## [Exercise 13.17](ex13_17.cpp) + +## Exercise 13.18 [.h](ex13_18.h) | [.cpp](ex13_18.cpp) + +## [Exercise 13.19](ex13_19.h) + +## Exercise 13.20: +>Explain what happens when we copy, assign, or destroy objects of our TextQuery and QueryResult classes from § 12.3 (p. 484). + +The member (smart pointer and container) will be copied. + +## Exercise 13.21: +>Do you think the TextQuery and QueryResult classes need to define their own versions of the copy-control members? If so, why? If not, why not? Implement whichever copy-control operations you think these classes require. + +(@Mooophy) +No copy-control members needed. + +Because, all these classes are using smart pointers to manage dynamic memory which can be freed automatically by calling synthesized destructors. The objects of these classes should share the same dynamic memory.Hence no user-defined version needed as well. + +```cpp +TextQuery(const TextQuery&) = delete; +TextQuery& operator=(const TextQuery) = delete; + +QueryResult(const QueryResult&) = delete; +QueryResult& operator=(const QueryResult) = delete; +``` diff --git a/ch13/ex13.14.15.16.cpp b/ch13/ex13.14.15.16.cpp deleted file mode 100644 index f6ff5f32..00000000 --- a/ch13/ex13.14.15.16.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Alan.W - * @date 31 DEC 2013 - * @remark - ***************************************************************************/ -//! Exercise 13.14: -//! Assume that numbered is a class with a default constructor that generates -//! a unique serial number for each object, which is stored in a data member -//! named mysn. Assuming numbered uses the synthesized copy-control members and -//! given the following function: -//! -// void f (numbered s) { cout << s.mysn << endl; } -//! what output does the following code produce? -// numbered a, b = a, c = b; -// f(a); f(b); f(c); -//! x -//! x -//! x -//! i.e.three identical numbers. --correct! -//! -//! Exercise 13.15: -//! Assume numbered has a copy constructor that generates a new serial number. -//! Does that change the output of the calls in the previous exercise? -//! If so, why? What output gets generated? -//! Yes. the ouput will change. -//! 102 -//! 103 -//! 104 -//! --correct! -//! -//! Exercise 13.16: -//! What if the parameter in f were const numbered&? Does that change the -//! output? If so, why? What output gets generated? -//! -//! Yes. Because ,if so, no copy operation any more.The function f just print -//! the object of Numbered directly whose mySn is unique. But if without the newly -//! defined copy constructor , it will still be : -//! 1 -//! 1 -//! 1. - -#include -#include -#include -#include - -struct Numbered -{ - //! for ex13.14 - Numbered() - { - static unsigned i = 0; - ++i; - mySn = i; - } - - //! for ex13.15 - /* - Numbered(const Numbered& num) - { - static unsigned j = 99; - ++j; - mySn = j; - } - */ - - unsigned mySn; -}; - -//void f (Numbered s) - -//! -void f(Numbered& s) -{ - std::cout << s.mySn << std::endl; -} -int main() -{ - Numbered a, b = a, c = b; - - f(a); f(b); f(c); - - return 0; -} - diff --git a/ch13/ex13.18.19.cpp b/ch13/ex13.18.19.cpp deleted file mode 100644 index 4b9de410..00000000 --- a/ch13/ex13.18.19.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Alan.W - * @date 31 DEC 2013 - * @remark - ***************************************************************************/ -//! -//! Exercise 13.18: -//! Define an Employee class that contains an employee name and a unique employee -//! identifier. Give the class a default constructor and a constructor that takes a -//! string representing the employee’s name. Each constructor should generate a unique -//! ID by incrementing a static data member. -//! -//! Exercise 13.19: -//! Does your Employee class need to define its own versions of the copy-control members? -//! If so, why? If not, why not? Implement whatever copy-control members you think Employee -//! needs. -// Yes it need the user-defined copy constructor and copy-assignment operator, which makes -// sure that each object has unique id.The user-defined destructor is not required. -//! - -#include -#include -#include -#include - -//! ex13.18 -class Employee -{ -public: - //! constructors - Employee() : name(std::string()), id(idGenerator()) { } - Employee(const std::string& n) : name(n), id(idGenerator()) { } - - //! copy constructor and copy assignment operator for ex13.19 - Employee(const Employee& e) : name(e.name), id(idGenerator()) { } - Employee& - operator = (const Employee& e) - { - name = e.name; - id = idGenerator(); - return *this; - } - - - unsigned idGenerator(); - std::string name; - unsigned id; -}; - -int main() -{ - Employee e1; - Employee e2("sss"); - - e1 =e2; - - std::cout << e1.id << " " << e2.id; - std::cout << e1.name <<"\n"; - std::cout << e2.name <<"\n"; - - return 0; -} - -inline unsigned -Employee::idGenerator() -{ - static unsigned i = 0; - return ++i; -} diff --git a/ch13/ex13.9.10.11.13.cpp b/ch13/ex13.9.10.11.13.cpp deleted file mode 100644 index 8ed47deb..00000000 --- a/ch13/ex13.9.10.11.13.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Alan.W - * @date 31 DEC 2013 - * @remark - ***************************************************************************/ -//! -//! Exercise 13.9: -//! What is a destructor? What does the synthesized destructor do? When is a destructor synthesized? -// The destructor is a member function with the name of the class prefixed by a tilde (~). -// It has no return value and takes no parameters: -class Foo -{ -public: - ~Foo(); // destructor -}; -// -// As with the copy constructor and the copy-assignment operator, for some classes, -// the synthesized destructor is defined to disallow objects of the type from being -// destroyed (§ 13.1.6, p. 508). Otherwise, the synthesized destructor has an empty -// function body. -// -// When no user-defined destructor, the compiler will define a synthesized version. -//! -//! Exercise 13.10: -//! What happens when a StrBlob object is destroyed? What about a StrBlobPtr? -//! When a StrBlobPter object is destroyed the object dynamicaly allocated will not be -//! freed. -//! When a StrBlob object destroyed, the use count of the dynamic object will decrement. -//! It wiil be freed if no shared_ptr to that dynamic object. -//! -//! Exercise 13.11: -//! Add a destructor to your HasPtr class from the previous exercises. -//! -//! Exercise 13.13: -//! A good way to understand copy-control members and constructors is to define a simple -//! class with these members in which each member prints its name: -//! -//! Add the copy-assignment operator and destructor to X -//! and write a program using X -//! objects in various ways: Pass them as nonreference and reference parameters; dynamically -//! allocate them; put them in containers; and so forth. Study the output until you are -//! certain you understand when and why each copy-control member is used. As you read the output, -//! remember that the compiler can omit calls to the copy constructor. - - - -#include -#include -#include -#include - -/** - * @brief ex13.13 - */ -struct X -{ - X() {std::cout << "X()\n" << std::endl;} - X(const X&) {std::cout << "X(const X&)\n" << std::endl;} - - X& - operator = (const X& x) - { - std::cout << "operator = (const X&)\n"; - return *this; - } - - - ~X() - { - std::cout << "~X()\n"; - } -}; - - -class HasPtr -{ -public: - //! default constructor. - HasPtr(const std::string &s = std::string()): - ps(new std::string(s)), i(0) { } - - //! copy constructor. - HasPtr(const HasPtr& hp) : ps(new std::string(*hp.ps)), i(hp.i) { } - - //! Copy-Assignment operator - HasPtr& - operator = (const HasPtr& hp) - { - *ps = *hp.ps; - i = hp.i; - return *this; - } - - //! destructor. - ~HasPtr() - { - delete ps; - } - -private: - std::string *ps; - int i; -}; - -int main() -{ - X x,y; - x = y; - return 0; -} - diff --git a/ch13/ex13_17.cpp b/ch13/ex13_17.cpp index f57cd9f3..edd838d7 100644 --- a/ch13/ex13_17.cpp +++ b/ch13/ex13_17.cpp @@ -5,6 +5,9 @@ // Created by pezy on 1/15/15. // Copyright (c) 2015 pezy. All rights reserved. // +// Write versions of numbered and f corresponding to the previous three exercises +// and check whether you correctly predicted the output. +// // See 13.14, 13.15, 13.16 #include @@ -20,7 +23,7 @@ class numbered { numbered(const numbered& n) { mysn = n.mysn+1; } - + int mysn; }; diff --git a/ch13/ex13_18.cpp b/ch13/ex13_18.cpp new file mode 100644 index 00000000..b1d7cfcd --- /dev/null +++ b/ch13/ex13_18.cpp @@ -0,0 +1,25 @@ +// +// ex13_18.cpp +// Exercise 13.18 +// +// Created by pezy on 1/15/15. +// Copyright (c) 2015 pezy. All rights reserved. +// +// Define an Employee class that contains an employee name and a unique employee identifier. +// Give the class a default constructor and a constructor that +// takes a string representing the employee’s name. +// Each constructor should generate a unique ID by incrementing a static data member. +// + +#include "ex13_18.h" + +int Employee::s_increment = 0; + +Employee::Employee() { + id_ = s_increment++; +} + +Employee::Employee(const string &name) { + id_ = s_increment++; + name_ = name; +} \ No newline at end of file diff --git a/ch13/ex13_18.h b/ch13/ex13_18.h new file mode 100644 index 00000000..ca54b68a --- /dev/null +++ b/ch13/ex13_18.h @@ -0,0 +1,33 @@ +// +// ex13_18.h +// Exercise 13.18 +// +// Created by pezy on 1/15/15. +// Copyright (c) 2015 pezy. All rights reserved. +// +// Define an Employee class that contains an employee name and a unique employee identifier. +// Give the class a default constructor and a constructor that +// takes a string representing the employee’s name. +// Each constructor should generate a unique ID by incrementing a static data member. +// + +#ifndef CP5_ex13_18_h +#define CP5_ex13_18_h + +#include +using std::string; + +class Employee { +public: + Employee(); + Employee(const string &name); + + const int id() const { return id_; } + +private: + string name_; + int id_; + static int s_increment; +}; + +#endif \ No newline at end of file diff --git a/ch13/ex13_19.h b/ch13/ex13_19.h new file mode 100644 index 00000000..f6c14538 --- /dev/null +++ b/ch13/ex13_19.h @@ -0,0 +1,35 @@ +// +// ex13_19.h +// Exercise 13.19 +// +// Created by pezy on 1/15/15. +// Copyright (c) 2015 pezy. All rights reserved. +// +// Does your Employee class need to define its own versions of the copy-control members? +// If so, why? If not, why not? +// Implement whatever copy-control members you think Employee needs. +// +// Answer: No, cause there really is no sensible meaning. employee can't copy in real world. + +#ifndef CP5_ex13_19_h +#define CP5_ex13_19_h + +#include +using std::string; + +class Employee { +public: + Employee(); + Employee(const string &name); + Employee(const Employee&) = delete; + Employee& operator=(const Employee&) = delete; + + const int id() const { return id_; } + +private: + string name_; + int id_; + static int s_increment; +}; + +#endif