String Class

s1

 

Class String

{

private:

char* m_szData;

unsigned m_nlength; //object variable

static unsigned s_nCount; //class variable

public:

String();

String(char*, szString);

}

cs

ds

data

lenth

count

stack

heap

"This is not a test"

 

(s1 pointer for ds: __,data,length)

int main()

P

String S1("This is not a test!);

Static member variable, refers to the class and not the object or any object, not part of any object. Exists for entire execution of program regardless of whether there is a string or not...access through a string object.

  1. Cs: All instructions.
  2. Ds: Variables Local to main.
  3. Global Variables.

    Constants.

    Variables declare as static.

  4. Stack: Local and Formal variables in all non-main f unctions except for those declared static.
  5. Heap: Dynamically allocated memory.

(Global variable: accessable at anytime from anywhere.)

int Factorial(int nIndex)

{

static int s=0;

if(nIndex ==1)

{

return1;

}

else

{

return nIndex * Fact(nIndex –1);

}

s++;

}

--stored in DS not heap

--if wasn’t static, would be stored in heap

 

 

 #2

Class String

{

private:

char* m_szData;

unsigned m_nlength; //object variable

static unsigned s_nCount; //class variable

public:

String();

String(char*, szString);

}

//String.cpp

unsigned string::s_nCount = 0;

String::String()

{

m_nlength = 0;

n_szData = NULL;

s_nCount++;

}

String::String(char* szString) //how many string objects are being created

{

m_nLength = strlen(szString);

m_szData = new char[m_nLength+1];

strcpy(n_szData, szString);

s_nCount++;

}

Class String

{

private:

char* m_szData;

unsigned m_nlength; //object variable

static unsigned s_nCount; //class variable

public:

String();

String(char*, szString);

private:

static void IncCount();

}

//String.cpp

unsigned string::s_nCount = 0;

String::String()

{

m_nlength = 0;

n_szData = NULL;

IncCount();

}

String::String(char* szString) //how many string objects are being created

{

m_nLength = strlen(szString);

m_szData = new char[m_nLength+1];

strcpy(n_szData, szString);

IncCount();

}

A static method cannot directly access an object variable //but can reference (pointers)

void string::IncCount()

{

s_nCount++;

m_nLength =0; //not legal if IncCount is static, is legal if IncCount is not static

}

 

//static method:

void BankAccount::Get NextAfccountNumber(char* szNum) //pointer accesses the variable

//void BankAccount::Get NextAfccountNumber(char* szAcct)

{

 

 

 

 #3

Class String

{

private:

char* m_szData;

unsigned m_nlength; //object variable

static unsigned s_nCount; //class variable

public:

String();

String(char*, szString);

private:

static void IncCount();

public:

string operator + (char* szString);

}

string s1 ("This is a");

string s2 = s1 + "test!";

//s2 -> "’this is a test!"

 

String String::operator + (char* szStr)

{

int nSize = n_nLength + strlen(szStr);

char* ptemp = new char [nSize +1] ;

if (n_szData !=NUll)

{

strcpy (ptemp, n_szData);

}

if(szStr !=Null)

{

strcpy(ptemp + m_nlength, sz Str);

}

//character array that is a concatenation of two strings

string ret(ptemp);

return ret;

}

Class String

{

private:

char* m_szData;

unsigned m_nlength; //object variable

static unsigned s_nCount; //class variable

public:

String();

String(char*, szString);

private:

static void IncCount();

public:

string operator + (char* szString);

string operator + (char& s);

}

string s1 ("This is a");

string s2 = s1 + "test!";

//s2 -> "’this is a test!"

string s3 = s2 +s1;

String String::operator + (char* szStr)

{

int nSize = n_nLength + strlen(szStr);

char* ptemp = new char [nSize +1] ;

if (n_szData !=NULL

{

strcpy (ptemp, n_szData);

}

if(szStr !=Null)

{

strcpy (ptemp + m_nlength, sz Str);

}

//character array that is a concatenation of two strings

string ret(ptemp);

delete [ ] pTemp;

return ret;

}

string string:: operator +(Stirng & s)

{

int nSize = n_nLength + s. m_nLength;

char* pTemp = now char [nSize +1];

if (m_nLength !=0)

{

strcpy(pTemp, m_szData);

{

if(s.n_nLength !=)

{

strcpy(ptemp + n_nLength, s. n_szData);

}

String ret(pTemp);

delete[ ]pTemp;

return ret,

}

 

 

 

Back | HOME | Next




Canpol Solutions Przeszukaj Katalog -- przeszukaj katalog / zamowienia, komentarze --  Poczta Polonijnej Witryny

Internet ExplorerUzywaj zawsze najnowszej przegladarki
M.I.E. lub, ewentualnie Netscape ktorej, niestety na razie nie polecamy..
Netscape


Canpol Solutions © Polonijna Witryna