Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <malloc.h>
- using namespace std;
- class stroka{
- protected:
- int len;
- char* pCh;
- public:
- stroka(int = 0);
- stroka(char);
- stroka(const char*);
- stroka(const stroka&);
- ~stroka();
- char* GetStr(void) const {
- return pCh;
- }
- int GetLen(void) const {
- return len;
- }
- void Show(void);
- };
- stroka::stroka(int val) :len(val), pCh(new char[len + 1]) {
- len = val;
- if (val == 0) pCh[0] = '\0';
- else pCh = new char[len + 1];
- cout << "stroka::stroka(int val)" << endl;
- }
- stroka::stroka(char ch) :len(1), pCh(new char[len + 1])
- {
- pCh[0] = ch;
- pCh[1] = '\0';
- cout << "stroka::stroka(char ch)" << endl;
- }
- stroka::stroka(const char* S) :len(0), pCh()
- {
- //strcpy_s(pCh, len + 1, S);
- const char *cptr = S;
- while (*(cptr++) != '\0') len++;
- pCh = new char[len + 1];
- char *ptr1 = pCh;
- const char *ptr2 = S;
- for (int i = 0; i <= len; i++) {
- *ptr1 = *ptr2;
- ptr1++;
- ptr2++;
- }
- cout << "stroka::stroka(const char* S)" << endl;
- }
- stroka::stroka(const stroka& from) :len(0), pCh()
- {
- const char* cptr = from.pCh;
- while (*(cptr++) != '\0') len++;
- pCh = new char[len + 1];
- char* ptr1 = pCh;
- const char* ptr2 = from.pCh;
- for (int i = 0; i <= len; i++) {
- *ptr1 = *ptr2;
- ptr1++;
- ptr2++;
- }
- cout << "stroka::stroka(const stroka& from)" << endl;
- }
- stroka::~stroka()
- {
- if (pCh)
- delete[] pCh;
- cout << "stroka::~stroka()" << endl;
- }
- void stroka::Show(void)
- {
- cout << "pCh=" << pCh << endl;
- cout << "len=" << len << endl;
- }
- int main()
- {
- stroka obj("FBIT");
- stroka obj1 = obj;
- obj.Show();
- obj1.Show();
- stroka obj2('F');
- obj2.Show();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement