C++ 继承 使用动态内存分配及友元的继承

dma.h

  1. #ifndef PRO1_DMA_H
  2. #define PRO1_DMA_H
  3. #include <iostream>
  4. using namespace std;
  5. class baseDMA {
  6. private:
  7. char *label;
  8. int rating;
  9. public:
  10. baseDMA(const char* l = "null", int r = 0);
  11. baseDMA(const baseDMA & rs);
  12. virtual ~baseDMA();
  13. baseDMA &operator = (const baseDMA & rs);
  14. friend ostream &operator << (ostream & os, const baseDMA & rs);
  15. };
  16. class lacksDMA:public baseDMA{
  17. private:
  18. enum {COL_LEN = 40};
  19. char color[COL_LEN];
  20. public:
  21. lacksDMA(const char * c = "blank", const char * l = "null", int r = 0);
  22. lacksDMA(const char * c, const baseDMA & rs);
  23. friend ostream&operator<<(ostream & os, const lacksDMA & rs);
  24. };
  25. class hasDMA:public baseDMA{
  26. private:
  27. char * style;
  28. public:
  29. hasDMA(const char * s = "none", const char * l = "null", int r = 0);
  30. hasDMA(const char * s, const baseDMA & rs);
  31. hasDMA(const hasDMA &hs);
  32. hasDMA &operator=(const hasDMA &rs);
  33. ~hasDMA();
  34. friend ostream &operator<<(ostream & os, const hasDMA & rs);
  35. };
  36. #endif //PRO1_DMA_H

dma.cpp

  1. #include "dma.h"
  2. #include <cstring>
  3. using namespace std;
  4. baseDMA::baseDMA(const char *l, int r) {
  5. label = new char[strlen(l) + 1];
  6. strcpy(label, l);
  7. rating = r;
  8. }
  9. baseDMA::baseDMA(const baseDMA &rs) {
  10. label = new char[strlen(rs.label) + 1];
  11. strcpy(label, rs.label);
  12. rating = rs.rating;
  13. }
  14. baseDMA::~baseDMA() {
  15. delete[] label;
  16. }
  17. baseDMA& baseDMA::operator=(const baseDMA &rs) {
  18. if (this == &rs)
  19. return *this;
  20. delete [] label;
  21. label = new char[strlen(rs.label) + 1];
  22. strcpy(label, rs.label);
  23. rating = rs.rating;
  24. return *this;
  25. }
  26. ostream & operator<<(ostream & os, const baseDMA & rs){
  27. os << "Label: " << rs.label << endl;
  28. os << "Rating: " << rs.rating << endl;
  29. return os;
  30. }
  31. lacksDMA::lacksDMA(const char * c, const char *l, int r) : baseDMA(l, r) {
  32. strncpy(color, c, 39);
  33. color[39] = '\0';
  34. }
  35. lacksDMA::lacksDMA(const char * c, const baseDMA &rs) : baseDMA(rs) {
  36. strncpy(color, c, COL_LEN - 1);
  37. color[COL_LEN - 1] = '\0';
  38. }
  39. hasDMA::~hasDMA() {
  40. delete [] style;
  41. }
  42. hasDMA &hasDMA::operator=(const hasDMA &hs) {
  43. if(this == &hs)
  44. return *this;
  45. baseDMA::operator=(hs);
  46. style = new char[strlen(hs.style) + 1];
  47. strcpy(style, hs.style);
  48. return *this;
  49. }
  50. ostream & operator<<(ostream & os, const lacksDMA & ls){
  51. os << (const baseDMA &) ls;
  52. os << "Color: " << ls.color << endl;
  53. return os;
  54. }
  55. hasDMA::hasDMA(const char *s, const char *l, int r):baseDMA(l, r) {
  56. style = new char[strlen(s) + 1];
  57. strcpy(style, s);
  58. }
  59. hasDMA::hasDMA(const char *s, const baseDMA &rs) : baseDMA(rs) {
  60. style = new char[strlen(s) + 1];
  61. strcpy(style, s);
  62. }
  63. hasDMA::hasDMA(const hasDMA &hs) : baseDMA(hs) {
  64. style = new char[strlen(hs.style) + 1];
  65. strcpy(style, hs.style);
  66. }
  67. ostream & operator<<(ostream&os, const hasDMA & hs){
  68. os << (const baseDMA &) hs;
  69. os << "Style: " << hs.style << endl;
  70. return os;
  71. }

main.cpp

  1. #include <iostream>
  2. #include "module9_dynamic_memory_allocation_friend_inheritance/dma.h"
  3. using namespace std;
  4. void usedma(){
  5. baseDMA shirt("Portabelly", 8);
  6. lacksDMA balloon("red", "Blimpo", 4);
  7. hasDMA map("Mercator", "Buffalo Keys", 5);
  8. cout << shirt << endl;
  9. cout << balloon << endl;
  10. cout << map << endl;
  11. lacksDMA balloon2(balloon);
  12. hasDMA map2;
  13. map2 = map;
  14. cout << balloon2 << endl;
  15. cout << map2 << endl;
  16. }
  17. int main() {
  18. usedma();
  19. return 0;
  20. }