++

         

110. - 2


7.

1 class string

2 {

3 char *buf;

4 int length; //

( );

5

6 public:

7 virtual

8 ~string( void );

9 string( const char

*input_str = "" );

10 string( const string &r );

11

12 virtual const

string &operator=( const string &r );

13

14 virtual int

operator< ( const string &r ) const;

15 virtual int

operator> ( const string &r ) const;

16 virtual int

operator==( const string &r ) const;

17

18 virtual void

print( ostream &output ) const;

19 // ...

20 };

21 //-----------------------------------------------------

22 inline string::string( const char

*input_str /*= ""*/ )

23 {

24 length = strlen(input_str) + 1;

25 buf = new char[ length ];

26 strcpy( buf, input_str );

27 }

28 //-----------------------------------------------------

29 inline string::string( const string &r )

30 {

31 length = r.length;

32 buf = new char[ length ];

33 strcpy( buf, r.buf );

34 }

35 //-----------------------------------------------------

36 /* */ string:: ~string( void )

37 {

38 delete buf;

39 }

40 //-----------------------------------------------------

41 /* */ const string &string::operator=( const string &r)

42 {

43 if( this

!= &r )

44 {

45 if( length != r.length )

46 {

47 free( buf );

48 length = r.length;

49 buf = new char[ length ];

50 }

51 strcpy( buf, r.buf );

52 }

53 return *this;

54 }

55

56 //-----------------------------------------------------

57 /* */ int string::operator< ( const

string &r ) const

58 {

59 return strcmp(buf, r.buf) < 0;

60 }

61 //-----------------------------------------------------

62 /* */ int string::operator> ( const string &r ) const