Kolokwium II Poprawkowy
Dla podanego programu odnaleźć błędy i poprawić:
class A
{
protected:
char str[128];
public:
A() { memset(str, 0, sizeof(str)); }
A(char *sstr){ strcpy_s(str, sizeof(str), sstr); }
};
class B
{
protected:
A *pA;
public:
B(char *sstr);
B() : pA(0) {}
~B();
friend ostream & operator << (ostream &strm, const B &ob);
};
B::B(char *sstr)
{
try {
pA = new A(sstr);
}
catch(bad_alloc) {
//..................
}
}
B::~B()
{
if(pA) {
size_t dim = _msize(pA);
delete pA;
}
pA = NULL;
}
ostream & operator << (ostream &strm, const B &ob)
{
if(ob.pA) {
strm << *ob.pA;
}
return strm;
}
class D : public B
{
protected:
B tab[3];
public:
D(B *pt);
D() {}
friend ostream & operator << (ostream &strm, const D &ob);
};
D::D(B *pt)
{
for(size_t it=0; it<3; ++it)
tab[it] = pt[it];
}
ostream & operator << (ostream &strm, const D &ob)
{
strm << "D: ";
for(size_t it=0; it<3; ++it)
strm << ob.tab[it];
strm << endl;
return strm;
}
int _tmain(int argc, _TCHAR* argv[])
{
B tab_B[] = { B("aaaa"), B("bbbb"), B("cccc") };
D ob_D1(tab_B), ob_D2, ob_D3(tab_B);
cout << ob_D1 << ob_D2 << ob_D3;
ob_D1 = ob_D2;
cout << ob_D1 ;
ob_D1 = ob_D3;
cout << ob_D1;
D ob_D4 = ob_D1;
cout << ob_D4;
system("pause");
return 0;
}