fork download
  1. #include <iostream>
  2. #include <string.h>
  3. using std::cout;
  4. using std::endl;
  5. class Cow {
  6. private:
  7. char name[20];
  8. char hobby;
  9. double weight;
  10. public:
  11. Cow();
  12. Cow(const char nm, const char*ho, double wt);
  13. Cow(const Cow &c);
  14. ~Cow();
  15. Cow & operator=(const Cow & c);
  16. void ShowCow() const;
  17. };
  18.  
  19. Cow::Cow()
  20. {
  21. name[0] = '\0';
  22. hobby = new char[1];
  23. hobby[0] = '\0';
  24. weight = 0;
  25. }
  26.  
  27. Cow::Cow(const char* nm, const char*ho, double wt)
  28. {
  29. int len_h = strlen(ho);
  30. int len_n = strlen(nm);
  31. hobby = new char(len_h + 1);
  32. strcpy_s(name, len_n +1,nm);
  33. strcpy_s(hobby,len_h+1 ,ho);
  34. weight = wt;
  35. }
  36. Cow::~Cow()
  37. {
  38. delete[] hobby;
  39. }
  40. Cow::Cow(const Cow &c) // konstruktor kopiujący
  41. {
  42. int len_h = strlen(c.hobby);
  43. int len_n = strlen(c.name);
  44. hobby = new char[len_h + 1];
  45. strcpy_s(hobby, len_h+1, c.hobby);
  46. strcpy_s(name, len_n+1, c.name);
  47. weight = c.weight;
  48. }
  49.  
  50. Cow& Cow::operator=(const Cow & c) // celem operacji jest obiekt na rzecz którego wywołujemy metode ( jest on po lewej)
  51. {
  52. if (this == &c)
  53. return *this;
  54. delete[] hobby; // obiekt docelowy może być zajęty więc zwalniamy
  55. int len_h = strlen(c.hobby);
  56. hobby = new char[len_h + 1];
  57. strcpy_s(hobby,len_h+1 ,c.hobby);
  58. int len_n = strlen(c.name);
  59. strcpy_s(name, len_n+1 , c.name);
  60. weight = c.weight;
  61. return *this; // zwracamy obiekt na rzecz którego wywoływano metode
  62. }
  63.  
  64. void Cow::ShowCow() const
  65. {
  66. cout << name<<endl;
  67. cout << hobby << endl;
  68. cout << weight << endl;
  69. }
  70.  
  71. int main()
  72. {
  73. Cow krowa0;
  74. Cow krowa1 = Cow("jan", "trawa", 2);
  75. krowa1.ShowCow();
  76. system("pause");
  77. return 0;
  78. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In constructor ‘Cow::Cow()’:
prog.cpp:22:13: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
     hobby = new char[1];
             ^~~~~~~~~~~
prog.cpp:23:12: error: invalid types ‘char[int]’ for array subscript
     hobby[0] = '\0';
            ^
prog.cpp: At global scope:
prog.cpp:27:1: error: prototype for ‘Cow::Cow(const char*, const char*, double)’ does not match any in class ‘Cow’
 Cow::Cow(const char* nm, const char*ho, double wt)
 ^~~
prog.cpp:13:1: error: candidates are: Cow::Cow(const Cow&)
 Cow(const Cow &c);
 ^~~
prog.cpp:12:1: error:                 Cow::Cow(char, const char*, double)
 Cow(const char nm, const char*ho, double wt);
 ^~~
prog.cpp:19:1: error:                 Cow::Cow()
 Cow::Cow()
 ^~~
prog.cpp: In destructor ‘Cow::~Cow()’:
prog.cpp:38:14: error: type ‘char’ argument given to ‘delete’, expected pointer
     delete[] hobby;
              ^~~~~
prog.cpp: In copy constructor ‘Cow::Cow(const Cow&)’:
prog.cpp:42:26: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
     int len_h = strlen(c.hobby);
                        ~~^~~~~
In file included from prog.cpp:2:0:
/usr/include/string.h:394:15: note:   initializing argument 1 of ‘size_t strlen(const char*)’
 extern size_t strlen (const char *__s)
               ^~~~~~
prog.cpp:44:13: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
     hobby = new char[len_h + 1];
             ^~~~~~~~~~~~~~~~~~~
prog.cpp:45:37: error: ‘strcpy_s’ was not declared in this scope
     strcpy_s(hobby, len_h+1, c.hobby);
                                     ^
prog.cpp: In member function ‘Cow& Cow::operator=(const Cow&)’:
prog.cpp:54:14: error: type ‘char’ argument given to ‘delete’, expected pointer
     delete[] hobby; // obiekt docelowy może być zajęty więc zwalniamy
              ^~~~~
prog.cpp:55:26: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
     int len_h = strlen(c.hobby);
                        ~~^~~~~
In file included from prog.cpp:2:0:
/usr/include/string.h:394:15: note:   initializing argument 1 of ‘size_t strlen(const char*)’
 extern size_t strlen (const char *__s)
               ^~~~~~
prog.cpp:56:13: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
     hobby = new char[len_h + 1];
             ^~~~~~~~~~~~~~~~~~~
prog.cpp:57:36: error: ‘strcpy_s’ was not declared in this scope
     strcpy_s(hobby,len_h+1 ,c.hobby);
                                    ^
prog.cpp: In function ‘int main()’:
prog.cpp:74:35: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
 Cow krowa1 = Cow("jan", "trawa", 2);
                                   ^
prog.cpp:12:1: note:   initializing argument 1 of ‘Cow::Cow(char, const char*, double)’
 Cow(const char nm, const char*ho, double wt);
 ^~~
stdout
Standard output is empty