S
spacebebop
#include <cstring>
#include <iostream>
using namespace std;
class X
{
public:
X();
X( char * );
~X();
void print();
private:
char *_str;
};
class Y : public X
{
public:
Y();
Y( char * );
~Y();
void print();
private:
char *_str;
};
X::X()
{
static char *str = "X's str empty";
_str = new char[ strlen( str ) ];
strcpy(_str, str);
cout << "X(), '" << str << "'\n";
}
X::X( char *str )
{
_str = new char[ strlen( str ) ];
strcpy(str, str );
cout << "X(char *), '" << str << "'\n";
}
X::~X()
{
cout << "~X(), deallocating '" << _str << "'\n";
delete _str;
}
void X:rint()
{
cout << "X:rint():\t'" << _str << "'\n";
}
Y::Y()
{
static char *str = "Y's str empty";
_str = new char[ strlen( str ) ];
strcpy(_str, str);
cout << "Y(), '" << str << "'\n";
}
Y::Y(char *str)
{
_str = new char[ strlen( str ) ];
strcpy( _str, str );
cout << "Y(char *), '" << str << "'\n";
}
Y::~Y()
{
cout << "~Y() deallocating '" << _str << "'\n";
delete _str;
}
void Y:rint()
{
cout << "Y:rint():\t'" << _str << "'\n";
}
int main()
{
X *x[3];
x[0] = new X( "X's data-index 0" );
x[1] = new Y( "Y's data-index 1" );
// Y y( "y's data-index 2" );
// x[2] = new Y( y );
x[0]->print();
x[1]->print();
// x[2]->print();
delete x[0];
delete x[1];
// delete x[2];
return 0;
}
#include <iostream>
using namespace std;
class X
{
public:
X();
X( char * );
~X();
void print();
private:
char *_str;
};
class Y : public X
{
public:
Y();
Y( char * );
~Y();
void print();
private:
char *_str;
};
X::X()
{
static char *str = "X's str empty";
_str = new char[ strlen( str ) ];
strcpy(_str, str);
cout << "X(), '" << str << "'\n";
}
X::X( char *str )
{
_str = new char[ strlen( str ) ];
strcpy(str, str );
cout << "X(char *), '" << str << "'\n";
}
X::~X()
{
cout << "~X(), deallocating '" << _str << "'\n";
delete _str;
}
void X:rint()
{
cout << "X:rint():\t'" << _str << "'\n";
}
Y::Y()
{
static char *str = "Y's str empty";
_str = new char[ strlen( str ) ];
strcpy(_str, str);
cout << "Y(), '" << str << "'\n";
}
Y::Y(char *str)
{
_str = new char[ strlen( str ) ];
strcpy( _str, str );
cout << "Y(char *), '" << str << "'\n";
}
Y::~Y()
{
cout << "~Y() deallocating '" << _str << "'\n";
delete _str;
}
void Y:rint()
{
cout << "Y:rint():\t'" << _str << "'\n";
}
int main()
{
X *x[3];
x[0] = new X( "X's data-index 0" );
x[1] = new Y( "Y's data-index 1" );
// Y y( "y's data-index 2" );
// x[2] = new Y( y );
x[0]->print();
x[1]->print();
// x[2]->print();
delete x[0];
delete x[1];
// delete x[2];
return 0;
}