zdravim, citam knihu mistrovstvi c++ a dostal som sa na cvicenie z ktoreho sa neviem dostat dalej.
Konkretne je to cvicenie 1 v lekcii cislo 12. Mam urcenu delkaraciu triedy a mam napisat definicie clenskych funkcii a funkciu main ktora ich vyuzije...
Môj kod je:
Kód:
#include <iostream>
#include <cstring>
using namespace std;
class Cow //delkaracia urcena v knihe
{
private:
char name[20];
char * hobby;
double weight;
public:
Cow();
Cow(const char * nm, const char * ho, double wt);
Cow(const Cow &c);
~Cow();
Cow & operator=(const Cow & c);
void ShowCow();
};
Cow::Cow()
{
strcpy(name, "Nezname");
hobby = new char[9];
strcpy(hobby, "Flakanie");
weight = 0;
}
Cow::Cow(const char *nm, const char *ho, double wt)
{
strcpy(name, nm);
hobby = new char[strlen(ho)];
strcpy(hobby, ho);
weight = wt;
}
Cow::Cow(const Cow &c)
{
strcpy(name, c.name);
hobby = new char[strlen(c.hobby)];
strcpy(hobby, c.hobby);
weight = c.weight;
}
Cow & Cow::operator =(const Cow &c)
{
if(this == &c)
return *this;
strcpy(name, c.name);
delete [] hobby;
hobby = new char[strlen(c.hobby)];
strcpy(hobby, c.hobby);
weight = c.weight;
return *this;
}
Cow::~Cow()
{
delete [] hobby;
}
void Cow::ShowCow()
{
cout << "Meno: " << name << endl;
cout << "Hobby: " << hobby << endl;
cout << "Vaha: " << weight << endl;
}
int main()
{
Cow c1;
Cow c2("Milka", "Futbal", 600);
cout << "Vypis c1 a c2\n";
c1.ShowCow();
c2.ShowCow();
cout << "Priradenie c2 do c1:\n";
c1 = c2;
cout << "C1 teraz:\n";
c1.ShowCow();
Cow c3("Etela", "Hokej", 650);
Cow c4(c3);
cout << "C4 inizializovane c3:\n";
c4.ShowCow();
cout << "Koniec.\n";
cin.get();
cin.get();
return 0;
}
Ked tento program zkompilujem tak normalne prebehne ako ma ale na konci ked sa rusia objekty tak mi vybehne toto okno:
http://imageshack.us/photo/my-images/404/snmkaq.jpg/
Aku tam mam prosim chybu?
Za vsetky odpovede vopred dakujem.