SOLUTION:
#include "stdafx.h"
#include "iostream"
#include "string.h"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class String
{
protected:
enum
{ SZ = 80 };
char
str[SZ];
public:
String(
)
{
str[0]
= '\0';
}
String(
char s[ ] )
{
strcpy_s(str, s);
}
void display( ) const
{
cout << str << endl;
}
operator
char*( )
{
return str;
}
};
class Pstring : public String
{
private:
int
len;
char
ss[SZ];
public:
Pstring(
)
{
String::String( );
}
Pstring(char
s[ ])
{
len
= strlen(s);
if(len
>= SZ)
{
for(int
i = 0; i < SZ-1; i++)
ss[i]
= s[i];
ss[SZ-1]
= '\0';
strcpy_s(str,
ss);
}
else
strcpy_s(str,
s);
}
void
display( ) const
{
String::display( );
}
};
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
Pstring
ps1;
char
xstr[] = "Joyeux Noel! ";
ps1 =
xstr;
ps1.display(
);
Pstring
ps2 = "SZ-1 characters into str if the string constant is longer, but copy
the entire"
"
constant if it’s shorter. Write a main() program to test different lengths of
strings.";
cout
<< static_cast<char*>(ps2);
cout
<< endl;
system("pause");
}
OUTPUT:
0 comments: