Skip to content

Commit

Permalink
added 13.18 ~ 21
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jan 15, 2015
1 parent 871151a commit ad9cd41
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 269 deletions.
25 changes: 25 additions & 0 deletions ch13/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
86 changes: 0 additions & 86 deletions ch13/ex13.14.15.16.cpp

This file was deleted.

70 changes: 0 additions & 70 deletions ch13/ex13.18.19.cpp

This file was deleted.

112 changes: 0 additions & 112 deletions ch13/ex13.9.10.11.13.cpp

This file was deleted.

5 changes: 4 additions & 1 deletion ch13/ex13_17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>
Expand All @@ -20,7 +23,7 @@ class numbered {
numbered(const numbered& n) {
mysn = n.mysn+1;
}

int mysn;
};

Expand Down
25 changes: 25 additions & 0 deletions ch13/ex13_18.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
33 changes: 33 additions & 0 deletions ch13/ex13_18.h
Original file line number Diff line number Diff line change
@@ -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 <string>
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
Loading

0 comments on commit ad9cd41

Please sign in to comment.