Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. // dma.h -- inheritance and dynamic memory allocation
  2. #ifndef DMA_H_
  3. #define DMA_H_
  4. #include <iostream>
  5. // Base Class Using DMA
  6. class baseDMA
  7. {
  8. private:
  9. char * label;
  10. int rating;
  11. public:
  12. baseDMA(const char * l = "null", int r = 0);
  13. baseDMA(const baseDMA & rs);
  14. virtual ~baseDMA();
  15. baseDMA & operator=(const baseDMA & rs);
  16. friend std::ostream & operator<<(std::ostream & os,
  17. const baseDMA & rs);
  18. };
  19. // derived class without DMA
  20. // no destructor needed
  21. // uses implicit copy constructor
  22. // uses implicit assignment operator
  23. class lacksDMA :public baseDMA
  24. {
  25. private:
  26. enum { COL_LEN = 40};
  27. char color[COL_LEN];
  28. public:
  29. lacksDMA(const char * c = "blank", const char * l = "null",
  30. int r = 0);
  31. lacksDMA(const char * c, const baseDMA & rs);
  32. friend std::ostream & operator<<(std::ostream & os,
  33. const lacksDMA & rs);
  34. };
  35.  
  36. // derived class with DMA
  37. class hasDMA :public baseDMA
  38. {
  39. private:
  40. char * style;
  41. public:
  42. hasDMA(const char * s = "none", const char * l = "null",
  43. int r = 0);
  44. hasDMA(const char * s, const baseDMA & rs);
  45. hasDMA(const hasDMA & hs);
  46. ~hasDMA();
  47. hasDMA & operator=(const hasDMA & rs);
  48. friend std::ostream & operator<<(std::ostream & os,
  49. const hasDMA & rs);
  50. };
  51. #endif
  52.  
  53.  
  54.  
  55. // dma.cpp --dma class methods
  56. #include "dma.h"
  57. #include <cstring>
  58. // baseDMA methods
  59. baseDMA::baseDMA(const char * l, int r)
  60. {
  61. label = new char[std::strlen(l) + 1];
  62. std::strcpy(label, l);
  63. rating = r;
  64. }
  65. baseDMA::baseDMA(const baseDMA & rs)
  66. {
  67. label = new char[std::strlen(rs.label) + 1];
  68. std::strcpy(label, rs.label);
  69. rating = rs.rating;
  70. }
  71. baseDMA::~baseDMA()
  72. {
  73. delete [] label;
  74. }
  75. baseDMA & baseDMA::operator=(const baseDMA & rs)
  76. {
  77. if (this == &rs)
  78. return *this;
  79. delete [] label;
  80. label = new char[std::strlen(rs.label) + 1];
  81. std::strcpy(label, rs.label);
  82. rating = rs.rating;
  83. return *this;
  84. }
  85. std::ostream & operator<<(std::ostream & os, const baseDMA & rs)
  86. {
  87. os << "Label: " << rs.label << std::endl;
  88. os << "Rating: " << rs.rating << std::endl;
  89. return os;
  90. }
  91. // lacksDMA methods
  92. lacksDMA::lacksDMA(const char * c, const char * l, int r)
  93. : baseDMA(l, r)
  94. {
  95. std::strncpy(color, c, 39);
  96. color[39] = ‘\0';
  97. }
  98. lacksDMA::lacksDMA(const char * c, const baseDMA & rs)
  99. : baseDMA(rs)
  100. {
  101. std::strncpy(color, c, COL_LEN - 1);
  102. color[COL_LEN - 1] = ‘\0';
  103. }
  104. std::ostream & operator<<(std::ostream & os, const lacksDMA & ls)
  105. {
  106. os << (const baseDMA &) ls;
  107. os << "Color: " << ls.color << std::endl;
  108. return os;
  109. }
  110. // hasDMA methods
  111. hasDMA::hasDMA(const char * s, const char * l, int r)
  112. : baseDMA(l, r)
  113. {
  114. style = new char[std::strlen(s) + 1];
  115. std::strcpy(style, s);
  116. }
  117. hasDMA::hasDMA(const char * s, const baseDMA & rs)
  118. : baseDMA(rs)
  119. {
  120. style = new char[std::strlen(s) + 1];
  121. std::strcpy(style, s);
  122. }
  123. hasDMA::hasDMA(const hasDMA & hs)
  124. : baseDMA(hs) // invoke base class copy constructor
  125. {
  126. style = new char[std::strlen(hs.style) + 1];
  127. std::strcpy(style, hs.style);
  128. }
  129. hasDMA::~hasDMA()
  130. {
  131. delete [] style;
  132. }
  133. hasDMA & hasDMA::operator=(const hasDMA & hs)
  134. {
  135. if (this == &hs)
  136. return *this;
  137. baseDMA::operator=(hs); // copy base portion
  138. delete [] style; // prepare for new style
  139. style = new char[std::strlen(hs.style) + 1];
  140. std::strcpy(style, hs.style);
  141. return *this;
  142. }
  143. std::ostream & operator<<(std::ostream & os, const hasDMA & hs)
  144. {
  145. os << (const baseDMA &) hs;
  146. os << "Style: " << hs.style << std::endl;
  147. return os;
  148. }
  149.  
  150. // usedma.cpp -- inheritance, friends, and DMA
  151. // compile with dma.cpp
  152. #include <iostream>
  153. #include "dma.h"
  154. int main()
  155. {
  156. using std::cout;
  157. using std::endl;
  158. baseDMA shirt("Portabelly", 8);
  159. lacksDMA balloon("red", "Blimpo", 4);
  160. hasDMA map("Mercator", "Buffalo Keys", 5);
  161. cout << "Displaying baseDMA object:\n";
  162. cout << shirt << endl;
  163. cout << "Displaying lacksDMA object:\n";
  164. cout << balloon << endl;
  165. cout << "Displaying hasDMA object:\n";
  166. cout << map << endl;
  167. lacksDMA balloon2(balloon);
  168. cout << "Result of lacksDMA copy:\n";
  169. cout << balloon2 << endl;
  170. hasDMA map2;
  171. map2 = map;
  172. cout << "Result of hasDMA assignment:\n";
  173. cout << map2 << endl;
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement