forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add soultions to first half of exercises in chapter 13.
- Loading branch information
Jaege
committed
Feb 5, 2016
1 parent
86dd48a
commit 3e4f933
Showing
35 changed files
with
1,064 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
A copy constructor is a constructor whose first parameter is a reference to the class type and any additional parameters have default values. | ||
|
||
The copy constructor may be used when we use copy initialization, but not always. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
When a `StrBlob` object is destroyed, the shared pointer member is destroyed by calling the pointer's destructor, thus decreasing the reference count of the shared pointer, if the count is zero, then the vector pointed by the smart pointer is destroyed too. | ||
|
||
When a `StrBlobPtr` object is destroyed, the weak pointer member is destroyed by calling the pointer's destructor, the vector pointed by the smart pointer is not affected. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <string> | ||
#include <iostream> | ||
|
||
class HasPtr { | ||
public: | ||
HasPtr(const std::string &s = std::string()) | ||
: ps(new std::string(s)), i(0) {} | ||
HasPtr(const HasPtr &ori) | ||
: ps(new std::string(*ori.ps)), i(ori.i) {} | ||
~HasPtr(); | ||
|
||
HasPtr &operator=(const HasPtr &rhs); | ||
|
||
const std::string &get() const { return *ps; } | ||
void set(const std::string &s) { *ps = s; } | ||
|
||
private: | ||
std::string *ps; | ||
int i; | ||
}; | ||
|
||
HasPtr::~HasPtr() { | ||
delete ps; | ||
} | ||
|
||
HasPtr &HasPtr::operator=(const HasPtr &rhs) { | ||
// This copy-assignment operator is wrong, see ex13.23 for correct version. | ||
delete ps; | ||
ps = new std::string(*rhs.ps); | ||
i = rhs.i; | ||
return *this; | ||
} | ||
|
||
int main() { | ||
HasPtr hp1 = "World"; | ||
HasPtr hp2 = hp1; | ||
HasPtr hp3; | ||
hp3 = hp1; | ||
hp1.set("Hello"); | ||
|
||
std::cout << hp1.get() << std::endl; | ||
std::cout << hp2.get() << std::endl; | ||
std::cout << hp3.get() << std::endl; | ||
|
||
hp1 = hp1; | ||
std::cout << "After `hp1 = hp1`: " << hp1.get() << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
bool fcn(const Sales_data *trans, Sales_data accum) | ||
{ | ||
Sales_data item1(*trans), item2(accum); | ||
return item1.isbn() != item2.isbn(); | ||
} | ||
|
||
3 destructors are called on `accum`, `item1` and `item2`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <list> | ||
|
||
struct X { | ||
X() { ++i; j = i; std::cout << "X() " << j << std::endl; } | ||
X(const X &) { ++i; j = i; std::cout << "X(const X &) " << j << std::endl; } | ||
X &operator=(const X &) { std::cout << "operator= " << j << std::endl; } | ||
~X() { std::cout << "~X() " << j << std::endl; } | ||
static int i; | ||
int j; | ||
}; | ||
|
||
int X::i = 0; | ||
|
||
void f1(X x) { | ||
} | ||
|
||
void f2(X &x) { | ||
} | ||
|
||
X f3() { | ||
return X(); | ||
} | ||
|
||
X f5() { | ||
X x; | ||
return x; | ||
} | ||
|
||
X &f4(X &x) { | ||
return x; | ||
} | ||
|
||
int main() { | ||
std::cout << "\n----- X x1;\n"; | ||
X x1; | ||
std::cout << "\n----- f1(x1);\n"; | ||
f1(x1); | ||
std::cout << "\n----- f2(x1);\n"; | ||
f2(x1); | ||
std::cout << "\n----- X x3 = f3();\n"; | ||
X x3 = f3(); | ||
std::cout << "\n----- f3();\n"; | ||
f3(); | ||
std::cout << "\n----- X x5 = f5();\n"; | ||
X x5 = f5(); | ||
std::cout << "\n----- f5();\n"; | ||
f5(); | ||
std::cout << "\n----- X x4 = f4(x1);\n"; | ||
X x4 = f4(x1); | ||
std::cout << "\n----- f4(x1);\n"; | ||
f4(x1); | ||
std::cout << "\n----- X *x2 = new X;\n"; | ||
X *x2 = new X; | ||
std::cout << "\n----- std::vector<X> vx;\n"; | ||
std::vector<X> vx; | ||
std::cout << "\n----- vx.push_back(x1);\n"; | ||
vx.push_back(x1); | ||
std::cout << "\n----- vx.push_back(*x2);\n"; | ||
vx.push_back(*x2); // The vector is reallocate here. | ||
std::cout << "\n----- std::list<X> vl;\n"; | ||
std::list<X> vl; | ||
std::cout << "\n----- vl.push_back(x1);\n"; | ||
vl.push_back(x1); | ||
std::cout << "\n----- vl.push_back(*x2);\n"; | ||
vl.push_back(*x2); // The list does not need reallocate. | ||
std::cout << "\n----- delete x2;\n"; | ||
delete x2; | ||
std::cout << "\n----- \n"; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <iostream> | ||
using std::cout; using std::endl; | ||
|
||
class numbered { | ||
public: | ||
numbered() : mysn(++sn) {} | ||
int mysn; | ||
private: | ||
static int sn; | ||
}; | ||
|
||
int numbered::sn = 0; | ||
|
||
void f(numbered s) { | ||
cout << s.mysn << endl; | ||
} | ||
|
||
int main() { | ||
numbered a, b = a, c = b; | ||
f(a); // 1 | ||
f(b); // 1 | ||
f(c); // 1 | ||
numbered d; | ||
f(d); // 2 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <iostream> | ||
using std::cout; using std::endl; | ||
|
||
class numbered { | ||
public: | ||
numbered() : mysn(++sn) {} | ||
numbered(const numbered &) : mysn(++sn) {} | ||
int mysn; | ||
private: | ||
static int sn; | ||
}; | ||
|
||
int numbered::sn = 0; | ||
|
||
void f(numbered s) { | ||
// `s` is copy initialized from argument, thus `s.mysn` is not the same with | ||
// the argument we passed in. | ||
cout << s.mysn << endl; | ||
} | ||
|
||
int main() { | ||
numbered a, b = a, c = b; // a.mysn = 1, b.mysn = 2, c.mysn = 3 | ||
f(a); // 4 | ||
f(b); // 5 | ||
f(c); // 6 | ||
numbered d; | ||
f(d); // 8 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
using std::cout; using std::endl; | ||
|
||
class numbered { | ||
public: | ||
numbered() : mysn(++sn) {} | ||
numbered(const numbered &) : mysn(++sn) {} | ||
int mysn; | ||
private: | ||
static int sn; | ||
}; | ||
|
||
int numbered::sn = 0; | ||
|
||
void f(const numbered &s) { // the reference will not use copy initialization | ||
cout << s.mysn << endl; | ||
} | ||
|
||
int main() { | ||
numbered a, b = a, c = b; // a.mysn = 1, b.mysn = 2, c.mysn = 3 | ||
f(a); // 1 | ||
f(b); // 2 | ||
f(c); // 3 | ||
numbered d; | ||
f(d); // 4 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
See [ex13.14](ex13.14.cpp), [ex13.15](ex13.15.cpp) and [ex13.16](ex13.16.cpp). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <string> | ||
#include <iostream> | ||
|
||
class Employee { | ||
public: | ||
typedef int id_type; | ||
Employee() : name(), id(++eid) {} | ||
explicit Employee(const std::string &n) : name(n), id(++eid) {} | ||
|
||
const std::string &getName() const { return name; } | ||
id_type getID() const { return id; } | ||
|
||
private: | ||
std::string name; | ||
id_type id; | ||
static id_type eid; | ||
}; | ||
|
||
Employee::id_type Employee::eid = 0; | ||
|
||
int main() { | ||
Employee e1; | ||
Employee e2("Zhang San"); | ||
|
||
std::cout << e1.getName() << " " << e1.getID() << std::endl; | ||
std::cout << e2.getName() << " " << e2.getID() << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// The Employee class need to define its own versions of the copy-control | ||
// members, because we must ensure that the copyed object has different ID from | ||
// the original object. | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
class Employee { | ||
public: | ||
typedef int id_type; | ||
|
||
Employee() : name(), id(++eid) {} | ||
explicit Employee(const std::string &n) : name(n), id(++eid) {} | ||
Employee(const Employee &e) : name(e.name), id(++eid) {} | ||
|
||
Employee &operator=(const Employee &e); | ||
|
||
const std::string &getName() const { return name; } | ||
id_type getID() const { return id; } | ||
|
||
private: | ||
std::string name; | ||
id_type id; | ||
static id_type eid; | ||
}; | ||
|
||
Employee::id_type Employee::eid = 0; | ||
|
||
Employee &Employee::operator=(const Employee &e) { | ||
name = e.name; | ||
//id = ++eid; // keep the old id | ||
return *this; | ||
} | ||
|
||
int main() { | ||
Employee e1; | ||
std::cout << e1.getName() << " " << e1.getID() << std::endl; | ||
Employee e2("Zhang San"); | ||
std::cout << e2.getName() << " " << e2.getID() << std::endl; | ||
Employee e3 = e2; | ||
std::cout << e3.getName() << " " << e3.getID() << std::endl; | ||
e1 = e2; | ||
std::cout << e1.getName() << " " << e1.getID() << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The parameter of the constructor should be a reference, otherwise the call would never succeed. To call the copy constructor, we'd need to use the copy constructor to copy the argument, but to copy the argument, we'd need to call the copy constructor, and so on indefinitely. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
- Copy `TextQuery` or `QueryResult` | ||
|
||
The data member (built-in type, class type, smart pointer and container) of the class is copyed, in which the smart pointer is copyed and the reference count is increased. | ||
|
||
- Assign `TextQuery` or `QueryResult` | ||
|
||
The data member (built-in type, class type, smart pointer and container) of the class is copyed, in which the smart pointer is copyed and the new reference count is increased, the old reference count is decreased. | ||
|
||
- Destory `TextQuery` or `QueryResult` | ||
|
||
The data member (built-in type, class type, smart pointer and container) of the class is destroyed, in which the smart pointer is destroyed and the reference count is decreased. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
No, they don't need. Becasue all the members of class `TextQuery` and `QueryResult` are copyed or destroyed correctly by the synthesized copy-control members. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
See [ex13.11](13.11.cpp). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <string> | ||
#include <iostream> | ||
|
||
class HasPtr { | ||
public: | ||
HasPtr(const std::string &s = std::string()) | ||
: ps(new std::string(s)), i(0) {} | ||
HasPtr(const HasPtr &ori) | ||
: ps(new std::string(*ori.ps)), i(ori.i) {} | ||
~HasPtr(); | ||
|
||
HasPtr &operator=(const HasPtr &rhs); | ||
|
||
const std::string &get() const { return *ps; } | ||
void set(const std::string &s) { *ps = s; } | ||
|
||
private: | ||
std::string *ps; | ||
int i; | ||
}; | ||
|
||
HasPtr::~HasPtr() { | ||
delete ps; | ||
} | ||
|
||
HasPtr &HasPtr::operator=(const HasPtr &rhs) { | ||
// This copy-assignment operator is correct even if the object is assigned to | ||
// itself. See ex13.11 for the wrong version. | ||
auto newps = new std::string(*rhs.ps); | ||
delete ps; | ||
ps = newps; | ||
i = rhs.i; | ||
return *this; | ||
} | ||
|
||
int main() { | ||
HasPtr hp1 = "World"; | ||
HasPtr hp2 = hp1; | ||
HasPtr hp3; | ||
hp3 = hp1; | ||
hp1.set("Hello"); | ||
|
||
std::cout << hp1.get() << std::endl; | ||
std::cout << hp2.get() << std::endl; | ||
std::cout << hp3.get() << std::endl; | ||
|
||
hp1 = hp1; | ||
std::cout << "After `hp1 = hp1`: " << hp1.get() << std::endl; | ||
|
||
return 0; | ||
} |
Oops, something went wrong.