Лекции №1 С++ (Результаты)

Посмотреть архив целиком

3



Файл «s.h»:

#include

#include

#include

#include

class String

{ private:

char *str,name;

int len;

public:

void print() { cout<<"String: "<

cout<

}

String(int Len,char Name){ len=Len;

str=new char[len+1]; str[0]='\0';

name=Name;

cout<<"Constructor string with length "<

}

String(char *vs,char Name) { len=strlen(vs);

str=new char[len+1];

strcpy(str,vs);

name=Name;

cout<<"Constructor "<

}

String(String &S){ len=S.Length();

str=new char[len+1];

strcpy(str,S.str);

name='K';

cout<<"Constructor copy from "<

}

~String(){ delete [] str;

cout<<"Destructor "<

int Length(){return len;}

char operator[](int n) {return ((n>=0)&(n

String operator +(String &A)

{ cout<<"Operation +"<<"\n";

int j=len+A.Length();

String S(j,'S');

strcpy(S.str,str);

strcat(S.str,A.str);

cout<<"Operation +"<<"\n";

return S;

}

String operator +(char c)

{ cout<<"Operation +c"<<"\n";

int j=len+1;

String S(j,'Q');

strcpy(S.str,str);

S.str[len]=c; S.str[len+1]='\0';

cout<<"Operation +c"<<"\n";

return S;

}

String& operator=(String &S)

{ cout<<"Operation ="<<"\n";

len=S.Length();

if (str!=NULL) delete[]str;

str=new char[len+1];

strcpy(str,S.str);

cout<<"Operation ="<<"\n";

return *this;

}

};



Файл «SSS.cpp»:

#include "stdafx.h"

#include "s.h"

int main(int argc, char* argv[])

{ String A("ABC",'A'),B("DEF",'B'),C(6,'C');

C.print();

C=A+B;

C.print();

C=C+'a';

C.print();

return 0;

}


Программа

Функции

Результаты

String A("ABC"),

B("DEF"),

C(6);






Constructor A

Constructor B

Constructor string with length C

C.print();


String: C: Length: 6

C=A+B;



















operator +(String &A)

{ cout<<"Operation +"<<"\n";

int i,j=len+A.Length();

String S(j,'S');

strcpy(S.str,str);

for (i=0;i<=A.Length();i++)

S.putchar_to_str(i+len,A[i]);

cout<<"Operation +"<<"\n";

return S;

}

operator=(String &S)

{ cout<<"Operation ="<<"\n";

len=S.Length();

if (str!=NULL) delete[]str;

str=new char[len+1];

strcpy(str,S.str);

cout<<"Operation ="<<"\n";

return *this;

}


Operation +


Constructor string with length S




Operation +

Constructor copy from S to K

Destructor S


Operation =





Operation =

Destructor K


C.print();


String: C:ABCDEF Length: 6

C=C+'a';

operator +(char c)

{ cout<<"Operation +c"<<"\n";

int j=len+1;

String S(j,'Q');

strcpy(S.str,str);

S.str[len]=c; S.str[len+1]='\0';

cout<<"Operation +c"<<"\n";

return S;

}

operator=(String &S)

{ cout<<"Operation ="<<"\n";

len=S.Length();

if (str!=NULL) delete[]str;

str=new char[len+1];

strcpy(str,S.str);

cout<<"Operation ="<<"\n";

return *this;

}


Operation +c


Constructor string with length Q



Operation +c

Constructor copy from Q to K

Destructor Q


Operation =





Operation =

Destructor K

C.print();


String: C:ABCDEFa Length: 7

}



Destructor C

Destructor B

Destructor A



Случайные файлы

Файл
38200.doc
128899.rtf
116702.doc
61103.rtf
29979.rtf