Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Sir
- {
- private:
- char *s;
- public:
- Sir();
- Sir(int);
- Sir(char *);
- Sir(const char*);
- Sir(const Sir&);
- ~Sir();
- Sir operator+(Sir&);
- Sir operator+(const char*);
- Sir& operator=(Sir& x);
- Sir& operator+=(Sir& x);
- Sir& operator+=(const char*);
- char operator[](int i);
- operator char*();
- friend std::ostream& operator<<(std::ostream& out, Sir& s);
- int Length();
- void Afisare();
- };
- /* Constructor implicit */
- Sir::Sir()
- {
- s = NULL;
- }
- /* Copy constructor */
- Sir::Sir(const Sir& x)
- {
- if (x.s)
- {
- this->s = new char[strlen(x.s) + 1];
- strcpy(s, x.s);
- }
- else
- s = NULL;
- }
- /* Constructori de initializare*/
- Sir::Sir(int l)
- {
- s = new char[l + 1]();
- }
- Sir::Sir(char* x)
- {
- s = NULL;
- if (x)
- {
- s = new char[strlen(x) + 1];
- strcpy(s, x);
- }
- }
- Sir::Sir(const char* x) {
- s = NULL;
- if (x) {
- s = new char[strlen(x) + 1];
- strcpy(s, x);
- }
- }
- /* Destructor */
- Sir::~Sir()
- {
- if (s)
- delete[] s;
- }
- /* Metoda pentru lungime */
- int Sir::Length()
- {
- if (s)
- return strlen(s);
- else
- return 0;
- }
- /* Metoda pentru afisare */
- void Sir::Afisare()
- {
- cout << s << " ";
- }
- Sir Sir::operator+(Sir& x)
- {
- Sir a(this->Length() + x.Length());
- if (s)
- strcpy(a.s, s);
- if (x.s)
- strcat(a.s, x.s);
- return a;
- }
- Sir Sir::operator+(const char* add)
- {
- Sir other(add);
- return *this + other;
- }
- Sir& Sir::operator+=(Sir& x)
- {
- if (s)
- if (x.s)
- {
- char *temp = new char[strlen(s) + 1];
- strcpy(temp, s);
- delete[] s;
- s = new char[strlen(temp) + strlen(x.s) + 1];
- strcpy(s, temp);
- strcat(s, x.s);
- delete[] temp;
- return *this;
- }
- else
- return *this;
- else
- {
- if (x.s)
- {
- s = new char[strlen(x.s) + 1];
- strcpy(s, x.s);
- return *this;
- }
- else
- return *this;
- }
- }
- Sir& Sir::operator+=(const char* x) {
- Sir x_s(x);
- *this += x_s;
- return *this;
- }
- Sir& Sir::operator=(Sir& x)
- {
- if (s)
- delete[] s;
- if (x.s)
- {
- s = new char[strlen(x.s) + 1];
- strcpy(s, x.s);
- }
- return *this;
- }
- /* poti scrie (char*)x, cu x Sir, fara sa comenteze compilatorul */
- Sir::operator char *()
- {
- return s;
- }
- /* afisare, poti scrie cout << x , cu x de tip Sir */
- std::ostream& operator<<(std::ostream& out, Sir& s) {
- out << s.s << " ";
- return out;
- }
- /* poti sa scrii x[2], ca la string sau array de char-uri si o sa-ti dea litera respectiva
- char Sir::operator[](int i) {
- if (s)
- return s[i];
- else
- return NULL;
- }*/
- int main()
- {
- Sir a("abc"); /* constructor de initializare */
- Sir b(3); /* constructor de lungime */
- Sir d(6);
- Sir c(a); /* copyconstructor */
- Sir aux(" This");
- Sir aux2(" is");
- Sir test = aux; /* = */
- test = test + aux2; /* +(Sir)*/
- test = test + " very"; /*+(const char*) */
- test += " cool, "; /* +=(const char*) */
- test += Sir("they all work\n"); /* +=(Sir) */
- std::cout << test; /* << */
- std::cout << "Al doilea caracter este " << ((char*)test)[2] << "\n"; /* operator [] */
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement