C String, Proper Type, and String Objects - PowerPoint PPT Presentation

About This Presentation
Title:

C String, Proper Type, and String Objects

Description:

Implementation hidden from the public view. Responsible for its own existence ... size = 0; In-Class Exercise: Clone. If makes a copy of the parameter S. ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 44
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:
Tags: hidden | objects | of | proper | size | string | type | web

less

Transcript and Presenter's Notes

Title: C String, Proper Type, and String Objects


1
C String, Proper Type, and String Objects
  • Andy Wang
  • COP 4530 Data Structures, Algorithms, and
    Generic Programming

2
Lecture Overview
  • Goal
  • Design a string class that overcomes the
    disadvantages of C strings
  • Roadmap
  • Critique of C strings
  • The need for proper type
  • String objects

3
In-Class Exercise
  • Write a C function that copies one string to
    another.
  • void strcpy(char dest, char src)

4
Assumptions of C Strings
  • Allocated memory
  • char str
  • str malloc(sizeof(char)11)
  • Null termination
  • str10 \0
  • strcpy(str, 0123456789)

5
What can go wrong?
  • void strcpy(char copy_cat, char cat)
  • int j
  • for (j 0 catj ! \0 j)
  • copy_catj catj
  • copy_catj \0

6
Other String Functions
  • Still assume null-terminated strings
  • Still assume allocated memory
  • Security holes
  • Internet worms
  • System break-ins

7
The Concept of Proper Type
  • A proper type should be
  • Responsible for its own data
  • Data protected from clients and other programs
  • Responsible for its own behavior
  • Make behavior available to clients
  • Implementation hidden from the public view
  • Responsible for its own existence
  • Automatic memory management

8
C Class Objects
  • Responsible for their own data
  • Protected or private data
  • Controlled access through public methods
  • Responsible for their own behavior
  • Public methods
  • Responsible for their own existence
  • Constructors/deconstructors

9
In-Class Exercise String Class
  • namespace rcl
  • class String
  • // friend
  • public
  • //
  • private
  • //
  • //

10
String Class
  • class String
  • // friend iostream operators
  • public
  • // constructors/destructors
  • // operators
  • // builders
  • // data accessors (const)
  • // string comparison function
  • private
  • // data
  • // helper methods
  • // equality and order comparison operators

11
Private Data
  • char str
  • unsigned int size // equal to C strlen()

class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
12
Data Accessors
  • unsigned int Size() const
  • unsigned int Length() const
  • char Element(unsigned int n) const

class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
13
Data Accessors
  • unsigned int StringSize() const
  • return size
  • unsigned int StringLength() const
  • // note strlen takes char therefore, we need
    to implement an operator that converts String to
    char.
  • return strlen(this)

14
In-Class Exercise Element
  • Return the nth character of the String. Watch
    out for boundary cases.
  • char StringElement(unsigned int n) const

15
Element
  • char StringElement(unsigned int n) const
  • char ch
  • if ((size 0) (n gt size))
  • return \0
  • else
  • return strn

16
String Comparison Function
  • static int StrCmp(const String, const String)

class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
17
StrCmp
  • int StringStrCmp(const String S1, const
    String S2)
  • if ((S1.Size() 0) (S2.Size() 0))
  • return 0
  • else if ((S1.Size() 0) (S2.Size() ! 0))
  • return 1
  • else if ((S1.Size() ! 0) (S2.Size() 0))
  • return 1
  • else
  • return (strcmp(S1.str, S2.str)

18
Equality Operators
  • int operator(const String S1, const String
    S2)
  • int operator!(const String S1, const String
    S2)
  • int operatorlt(const String S1, const String
    S2)
  • int operatorlt(const String S1, const String
    S2)
  • int operatorgt(const String S1, const String
    S2)
  • int operatorgt(const String S1,
  • const String S2)

class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
19
Comparison Operators
  • int operator(const String S1, const String
    S2)
  • return (StringStrCmp(S1, S2) 0)
  • int operator!(const String S1, const String
    S2)
  • return (StringStrCmp(S1, S2) ! 0)
  • int operatorgt(const String S1, const String S2)
  • return (StringStrCmp(S1, S2) gt 0)

20
Comparison Operators
  • int operatorgt(const String S1, const String
    S2)
  • return (StringStrCmp(S1, S2) gt 0)
  • int operatorlt(const String S1, const String S2)
  • return (StringStrCmp(S1, S2) lt 0)
  • int operatorlt(const String S1, const String
    S2)
  • return (StringStrCmp(S1, S2) lt 0)

21
Helper Methods
  • static void Error(const char)
  • static char newstr(int n)
  • static int xstrlen(const char)
  • static void xstrcpy(char , const char)
  • void Clear()
  • void Clone(const String S)

class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
22
Error
  • void StringError(const char msg)
  • cerr ltlt String error ltlt msg ltlt \n
  • exit(EXIT_FAILURE)

23
In-Class Exercise newstr
  • It creates a new String of size n (C String with
    an array size of n 1).
  • void Stringnewstr(int n)

24
newstr
  • char Stringnewstr(int n)
  • char Cptr 0
  • if (n gt 0)
  • Cptr new charn 1
  • if (Cptr 0)
  • Error(memory allocation failure)
  • Cptrn \0
  • return Cptr

25
xstrlen
  • int Stringxstrlen(const char s)
  • int len
  • if (s ! 0) // check for null pointer
  • for (len 0 slen ! \0 len)
  • return len

26
xstrcpy
  • void Stringxstrcpy(char dest, const char src)
  • if (src ! 0) // check for null pointer
  • if (dest ! 0) // check for null pointer
  • int j
  • for (j 0 srcj ! \0 j)
  • destj srcj
  • destj \0
  • else
  • Error(xstrcpy null destination)

27
In-Class Exercise Clear
  • If the String has storage allocated, delete it,
    and set the storage pointer and size to zero.
  • void Clear()

28
Clear
  • void StringClear()
  • if (str)
  • delete str
  • str 0
  • size 0

29
In-Class Exercise Clone
  • If makes a copy of the parameter S.
  • void StringClone(const String S)

30
Clone
  • void StringClone(const String S)
  • size S.Size()
  • if (size gt 0)
  • str newstr(size)
  • xstrcpy(str, S.str)
  • else
  • str 0

31
Operators
  • String operator(const String S)
  • char operator (unsigned int I) const
  • operator const char () const

class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
32
Operator
  • String Stringoperator(const String S)
  • if (this ! S)
  • Clear()
  • Clone(S)
  • return this

33
Operator
  • char Stringoperator (unsigned int n) const
  • if ((size 0) (n gt size))
  • Error(index out of range)
  • return strn

34
Operator const char
  • Stringoperator const char () const
  • return str

35
Builder Functions
  • void Wrap(const char Cptr)
  • void GetLine(istream inl)
  • int SetSize(unsigned int sz, char fill)

class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
36
Wrap
  • void StringWrap(const char Cptr)
  • Clear()
  • if (Cptr)
  • size xstrlen(Cptr)
  • str newstr(size)
  • xstrcpy(str, Cptr)

37
GetLine
  • void StringGetLine(istream is)
  • unsigned int curr_size 0, buff_size
    init_buff_size
  • char buffer new charbuff_size 1
  • for (char x is.get() ((x ! \n)
    (!is.eof()) x is.get())
  • if (curr_size buff_size)
  • buff_size 2
  • char newbuffer new charbuff_size 1
  • // copy buffer to newbuffer
  • delete buffer
  • buffer newbuffer
  • buffercurr_size x
  • buffercurr_size \0
  • Wrap(buffer)
  • delete buffer

38
SetSize
  • int StringSetSize(unsigned int sz, char fill)
  • if (sz ! Size())
  • char newdata newstr(sz)
  • if (newdata 0) return 0
  • unsigned int j
  • if (sz lt Size())
  • for (j 0 j lt sz j)
  • newdataj strj
  • else
  • for (j 0 j lt Size() j)
  • newdataj strj
  • for (j Size() j lt sz j)
  • newdataj fill
  • delete str
  • str newdata
  • size sz

39
Constructors/Destructors
  • String()
  • explicit String(const char Cptr)
  • String()
  • String (const String S)

class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
40
Constructors /Deconstructors
  • StringString() str(0), size(0)
  • StringString(const char Cptr) str(0),
    size(0)
  • Wrap(Cptr)
  • StringString(const String S)
  • Clone(S)
  • StringString()
  • if (str) delete str

41
I/O Operators
  • friend ostream operatorltlt(ostream os, const
    String S)
  • friend istream operatorgtgt(istream is, String
    S)

class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
42
Operatorltlt
  • ostream operatorltlt(ostream os, const String S)
  • os ltlt S.str
  • return os

43
Operatorgtgt
  • istream operatorgtgt(istream is, String S)
  • unsigned int curr_size 0 buff_size
    init_buff_size
  • char x
  • // skip clear space
  • char buffer Stringnewstr(buff_size)
  • buffercurr_size x
  • for (x is.peek() (// x is not white space or
    eof) x is.peek())
  • if (curr_size buff_size)
  • buff_size 2
  • char newbuffer Stringnewstr(buff_size)
  • // copy buffer into newbuffer
  • delete buffer
  • buffer newbuffer
  • buffercurr_size x
  • is.get()
  • buffercurr_size \0
  • S.Wrap(buffer)
Write a Comment
User Comments (0)
About PowerShow.com