Skip to content

Commit

Permalink
added ex13_27 and ex13_28
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jan 20, 2015
1 parent e0ec0d2 commit 7f1b850
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 81 deletions.
4 changes: 4 additions & 0 deletions ch13/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,7 @@ Copy constructor and copy-assignment operator should dynamicly allocate memory f
`StrBlob` is using smart pointers which can be managed with synthesized destructor, If an object of `StrBlob` is out of scope, the destructor for std::shared_ptr will be called automaticaly to free the memory dynamically allocated when the `use_count` goes to 0.
## Exercise 13.26 [hpp](ex13_26.h) | [cpp](ex13_26.cpp)
## [Exercise 13.27](ex13_27.h)
## Exercise 13.28 [hpp](ex13_28.h) | [cpp](ex13_28.cpp)
81 changes: 0 additions & 81 deletions ch13/ex13.27.cpp

This file was deleted.

42 changes: 42 additions & 0 deletions ch13/ex13_27.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// ex13_27.h
// Exercise 13.27
//
// Created by pezy on 1/20/15.
// Copyright (c) 2015 pezy. All rights reserved.
//
// Define your own reference-counted version of HasPtr.

#ifndef CP5_ex13_27_h
#define CP5_ex13_27_h

#include <string>

class HasPtr {
public:
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0), use(new size_t(1)) { }
HasPtr(const HasPtr &hp) : ps(hp.ps), i(hp.i), use(hp.use) { ++*use; }
HasPtr& operator=(const HasPtr &rhs) {
++*rhs.use;
if (--*use == 0) {
delete ps;
delete use;
}
ps = rhs.ps;
i = rhs.i;
use = rhs.use;
return *this;
}
~HasPtr() {
if (--*use == 0) {
delete ps;
delete use;
}
}
private:
std::string *ps;
int i;
size_t *use;
};

#endif
41 changes: 41 additions & 0 deletions ch13/ex13_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// ex13_28.cpp
// Exercise 13.28
//
// Created by pezy on 1/20/15.
// Copyright (c) 2015 pezy. All rights reserved.
//
// Given the following classes, implement a default constructor and the necessary copy-control members.

#include "ex13_28.h"

TreeNode& TreeNode::operator=(const TreeNode &rhs)
{
++*rhs.count;
if (--*count == 0) {
if (left) {
delete left;
left = nullptr;
}
if (right) {
delete right;
right = nullptr;
}

delete count;
count = nullptr;
}
value = rhs.value;
left = rhs.left;
right = rhs.right;
count = rhs.count;
return *this;
}

BinStrTree& BinStrTree::operator=(const BinStrTree &bst)
{
TreeNode *new_root = new TreeNode(*bst.root);
delete root;
root = new_root;
return *this;
}
54 changes: 54 additions & 0 deletions ch13/ex13_28.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// ex13_28.h
// Exercise 13.28
//
// Created by pezy on 1/20/15.
// Copyright (c) 2015 pezy. All rights reserved.
//
// Given the following classes, implement a default constructor and the necessary copy-control members.

#ifndef CP5_ex13_28_h
#define CP5_ex13_28_h

#include <string>
using std::string;

class TreeNode {
public:
TreeNode() : value(string()), count(new int(1)), left(nullptr), right(nullptr) {}
TreeNode(const TreeNode &rhs) : value(rhs.value), count(rhs.count), left(rhs.left), right(rhs.right) { ++*count; }
TreeNode& operator=(const TreeNode &rhs);
~TreeNode() {
if (--*count == 0) {
if (left) {
delete left;
left = nullptr;
}
if (right) {
delete right;
right = nullptr;
}
delete count;
count = nullptr;
}
}

private:
std::string value;
int *count;
TreeNode *left;
TreeNode *right;
};

class BinStrTree {
public:
BinStrTree() : root(new TreeNode()) {}
BinStrTree(const BinStrTree &bst) : root(new TreeNode(*bst.root)) {}
BinStrTree& operator=(const BinStrTree &bst);
~BinStrTree() { delete root; }

private:
TreeNode *root;
};

#endif

0 comments on commit 7f1b850

Please sign in to comment.