Dobrý den,
chtěl jsem si jen tak pro radost procvičit třídy, ale nějak se mi to vymklo z rukou, protože mi to háže assertion failure a netuším co bych měl na to zdrojáku opravit :-)
#include <iostream>
#include <cstring>
using namespace std;
class tri {
private:
int x;
double n;
bool y;
int *z;
void vynuluj(){*z = 0;};
void pricti(int a){*z += a;};
public:
explicit tri(int xx, double nn, bool yy, int zz):x(xx), n(nn), y(yy){z = new int;*z = zz;}; //deklarace i definice konstruktoru zkracena verze
tri operator=(tri &a);
tri operator+(tri &a);
friend ostream & operator<<(ostream &os, tri &a);
int ukazz(){return *z;};
~tri();
};
int main()
{
tri x(5, 6.3, true, 10), y(7, 3.3, true, 1), z(2, 16.3, false, 5), a(0, 0.0, false, 0);
cout << "x: " << x << "y: " << y << "z: " << z;
a = x + y;
cout << "a = x + y: " << a;
a = x + z;
cout << "a = x + z: " << a;
a = x + y + z;
cout << "a = x + y + z: " << a;
system("pause");
return 0;
}
tri tri::operator=(tri &a)
{
x = a.x;
n = a.n;
y = a.y;
delete z;
z = new int;
*z = a.ukazz();
return *this;
}
tri tri::operator+(tri &a)
{
tri b(0, 0.0, false, 0);
b.x = x + a.x;
b.n = n + a.n;
if(y == true && a.y == true)b.y = true;
else b.y = false;
b.vynuluj();
b.pricti(ukazz());
b.pricti(a.ukazz());
return b;
}
ostream & operator<<(ostream &os, tri &a)
{
os << a.x << ' ' << a.n << ' ' << a.y << ' ' << a.ukazz() << endl;
return os;
}
tri::~tri()
{
delete z;
}
podle me je nejaka nejaka chyba v tech pretizenych operatorech = a +, ale vubec netusim jaka :-) dekuji predem za pomoc a postrehy