本文共 5167 字,大约阅读时间需要 17 分钟。
练习13.27
1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 14 using namespace std;15 16 class HasPtr {17 friend ostream& print(ostream& os, const HasPtr& pt);18 public:19 HasPtr(const string& s = string()) : ps(new string(s)), i(0), use(new size_t(1)) {}20 HasPtr(const HasPtr& pt) : ps(pt.ps), i(pt.i), use(pt.use) { ++*use; }21 HasPtr& operator=(const HasPtr& rhs);22 ~HasPtr();23 private:24 string *ps;25 int i;26 size_t *use;27 };28 29 ostream& print(ostream& os, const HasPtr& pt);30 31 int main()32 {33 HasPtr h1;34 HasPtr h2("hello");35 HasPtr h3(h2);36 HasPtr h4 = h1;37 print(cout, h1);38 print(cout, h2);39 print(cout, h3);40 print(cout, h4);41 system("pause");42 return 0;43 }44 45 HasPtr & HasPtr::operator=(const HasPtr & rhs)46 {47 ++(*rhs.use);48 if (--*use == 0)49 {50 delete ps;51 delete use;52 }53 ps = rhs.ps;54 i = rhs.i;55 use = rhs.use;56 return *this;57 // TODO: 在此处插入 return 语句58 }59 60 HasPtr::~HasPtr()61 {62 if (--*use == 0)63 {64 delete ps;65 delete use;66 }67 }68 69 ostream & print(ostream & os, const HasPtr & pt)70 {71 os << *pt.ps << " " << pt.i << " " << *pt.use << endl;72 return os;73 // TODO: 在此处插入 return 语句74 }
练习13.28
1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 14 using namespace std;15 16 class TreeNode {17 public:18 TreeNode(): value(""),count(0),left(nullptr),right(nullptr) {}19 TreeNode(string s, int no, TreeNode &p, TreeNode &q) : value(s), count(no), left(new TreeNode(p)), right(new TreeNode(q)) {}20 TreeNode(const TreeNode& p);21 TreeNode& operator=(const TreeNode& q);22 ~TreeNode();23 private:24 string value;25 int count;26 TreeNode *left;27 TreeNode *right;28 };29 30 int main()31 {32 TreeNode n1, n2, n4, n5;33 TreeNode n6("hello", 3, n4, n5);34 n2 = n6;35 TreeNode n3(n6);36 system("pause");37 return 0;38 }39 40 TreeNode::TreeNode(const TreeNode & p)41 {42 value = p.value;43 count = p.count;44 if (p.left != nullptr)45 left = new TreeNode(*p.left);46 else47 left = nullptr;48 if (p.right != nullptr)49 right = new TreeNode(*p.right);50 else51 right = nullptr;52 }53 54 TreeNode & TreeNode::operator=(const TreeNode & q)55 {56 value = q.value;57 count = q.count;58 if (q.left != nullptr)59 left = new TreeNode(*q.left);60 else61 left = nullptr;62 if (q.right != nullptr)63 right = new TreeNode(*q.right);64 else65 right = nullptr;66 return *this;67 // TODO: 在此处插入 return 语句68 }69 70 TreeNode::~TreeNode()71 {72 if (left != nullptr)73 delete left;74 if (right != nullptr)75 delete right;76 }
b)
1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 14 using namespace std; 15 16 class TreeNode { 17 public: 18 TreeNode(): value(""),count(0),left(nullptr),right(nullptr) {} 19 TreeNode(string s, int no, TreeNode &p, TreeNode &q) : value(s), count(no), left(new TreeNode(p)), right(new TreeNode(q)) {} 20 TreeNode(const TreeNode& p); 21 TreeNode& operator=(const TreeNode& q); 22 ~TreeNode(); 23 private: 24 string value; 25 int count; 26 TreeNode *left; 27 TreeNode *right; 28 }; 29 30 class BinStrTree { 31 public: 32 BinStrTree() : root(new TreeNode()) {} 33 BinStrTree(TreeNode &p) : root(new TreeNode(p)) {} 34 BinStrTree(const BinStrTree& b); 35 BinStrTree &operator=(const BinStrTree &bi); 36 ~BinStrTree(); 37 private: 38 TreeNode *root; 39 }; 40 41 int main() 42 { 43 TreeNode n1, n2, n4, n5; 44 TreeNode n6("hello", 3, n4, n5); 45 n2 = n6; 46 TreeNode n3(n6); 47 BinStrTree b1; 48 BinStrTree b2(n6); 49 b1 = b2; 50 BinStrTree b3(b1); 51 system("pause"); 52 return 0; 53 } 54 55 TreeNode::TreeNode(const TreeNode & p) 56 { 57 value = p.value; 58 count = p.count; 59 if (p.left != nullptr) 60 left = new TreeNode(*p.left); 61 else 62 left = nullptr; 63 if (p.right != nullptr) 64 right = new TreeNode(*p.right); 65 else 66 right = nullptr; 67 } 68 69 TreeNode & TreeNode::operator=(const TreeNode & q) 70 { 71 value = q.value; 72 count = q.count; 73 if (q.left != nullptr) 74 left = new TreeNode(*q.left); 75 else 76 left = nullptr; 77 if (q.right != nullptr) 78 right = new TreeNode(*q.right); 79 else 80 right = nullptr; 81 return *this; 82 // TODO: 在此处插入 return 语句 83 } 84 85 TreeNode::~TreeNode() 86 { 87 if (left != nullptr) 88 delete left; 89 if (right != nullptr) 90 delete right; 91 } 92 93 BinStrTree::BinStrTree(const BinStrTree & b) 94 { 95 root = new TreeNode(*b.root); 96 } 97 98 BinStrTree & BinStrTree::operator=(const BinStrTree & bi) 99 {100 root = new TreeNode(*bi.root);101 return *this;102 // TODO: 在此处插入 return 语句103 }104 105 BinStrTree::~BinStrTree()106 {107 delete root;108 }
转载于:https://www.cnblogs.com/wuyinfenghappy/p/7473338.html