Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef CIAG_H
- #define CIAG_H
- #include <iostream>
- #include <string.h>
- class Ciag
- {
- public:
- Ciag();
- Ciag(char* arr);
- Ciag(const Ciag& obj);
- ~Ciag();
- int dl(){ return lengthCiag; }
- int ile() { return counter; }
- char toCharArray();
- void wyswietl();
- Ciag& operator = (const Ciag& c);
- Ciag& operator +=(Ciag& c);
- private:
- std::size_t lengthCiag;
- char* ptrCiag;
- static std::size_t counter;
- };
- #endif // CIAG_H
- //plik cpp plik cpp plik cpp
- #include "ciag.h"
- std::size_t Ciag::counter = 0;
- Ciag::Ciag()
- {
- ptrCiag = nullptr;
- lengthCiag = 0;
- counter++;
- }
- Ciag::Ciag(char* arr)
- {
- lengthCiag = strlen(arr);
- ptrCiag = new char[lengthCiag + 1];
- strcpy(ptrCiag, arr);
- counter++;
- }
- Ciag::Ciag(const Ciag& obj)
- {
- lengthCiag = obj.lengthCiag;
- ptrCiag = new char[ lengthCiag + 1];
- strcpy(ptrCiag, obj.ptrCiag);
- }
- Ciag::~Ciag()
- {
- delete [] ptrCiag;
- ptrCiag = nullptr;
- counter--;
- }
- char Ciag::toCharArray()
- {
- return *ptrCiag;
- }
- Ciag& Ciag::operator =(const Ciag& c)
- {
- if(this != &c)
- {
- if(c.lengthCiag != lengthCiag)
- {
- delete [] ptrCiag;
- lengthCiag = 0;
- ptrCiag = nullptr;
- ptrCiag = new char[ c.lengthCiag + 1];
- lengthCiag = c.lengthCiag;
- }
- std::copy(c.ptrCiag, c.ptrCiag + c.lengthCiag, ptrCiag);
- }
- return *this;
- }
- Ciag& Ciag::operator +=(Ciag& c)
- {
- Ciag temp;
- delete [] temp.ptrCiag;
- temp.ptrCiag = nullptr;
- temp.ptrCiag = new char[ lengthCiag + c.lengthCiag + 1];
- temp.lengthCiag = lengthCiag + c.lengthCiag;
- temp.ptrCiag = c.ptrCiag;
- strcat(temp.ptrCiag, c.ptrCiag);
- return *this;
- }
- void Ciag::wyswietl()
- {
- std::cout << ptrCiag << "\n";
- }
- //main main main
- #include <iostream>
- #include "ciag.h"
- using namespace std;
- int main()
- {
- Ciag c1, c2, c3, c4;
- c1 = "Hejka";
- c3 = "lel";
- c4 = "nom";
- c2 = c1;
- c4 += c3;
- c1.wyswietl();
- c2.wyswietl();
- c4.wyswietl();
- cout << c4.dl();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement