You are on page 1of 9

#pragma once

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class Sir
{
char *s;

public:
Sir();
Sir(const char*);
Sir(const Sir&);

Sir& operator=(const Sir&);


Sir& operator=(const char*);
Sir& operator+=(const Sir&);
Sir operator+(const Sir&) const;
Sir operator+(const char*) const;

void Afisare() const;

friend ostream& operator<<(ostream&, const Sir&);


friend ostream& operator>>(ostream&, Sir&);

operator const char*() const


{
return this->s;
}

char& operator[](const int&);

~Sir();

protected:

void Change_s(const char* X)


{
s = NULL;
if (X)
{
s = new char[strlen(X) + 1];
if (!s)
throw("BAD MEMORY ALLOCATION");

strcpy(s, X);
}
}
};

class Siruri : public Sir


{
char* s2;

public:
Siruri()
{
s2 = NULL;
}

Siruri(const char* X)
{
s2 = NULL;
if (X)
{
s2 = new char[strlen(X) + 1];
if (!s2)
throw("BAD MEMORY ALLOCATION");

strcpy(s2, X);
}

Siruri(const char* X, const char* Y)


{
Sir::Change_s(Y);
s2 = NULL;
if (X)
{
s2 = new char[strlen(X) + 1];
if (!s2)
throw("BAD MEMORY ALLOCATION");
strcpy(s2, X);
}

void Afisare() const


{
Sir::Afisare();
cout << " ";
cout << s2;
}

~Siruri()
{
if (s2)
delete[] s2;
}
};

----

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include "Sir.h"
using namespace std;

Sir::Sir()
{
s = NULL;
}

void Sir::Afisare() const


{
if (s)
cout << s;
}

Sir::Sir(const char* X)
{
s = NULL;
if (X)
{
s = new char[strlen(X) + 1];
if (!s)
throw("BAD MEMORY ALLOCATION");

strcpy(s, X);
}
}

Sir::Sir(const Sir& X)
{
s = NULL;
if (X.s)
{
s = new char[strlen(X.s) + 1];
if (!s)
throw("BAD MEMORY ALLOCATION");

strcpy(s, X.s);
}
}

//TD = termen dreapta


Sir& Sir::operator=(const Sir& TD)
{
if (this->s != TD.s)
{
if (s)
delete[] s;

//s = NULL;

if (TD.s)
{
this->s = new char[strlen(TD.s) + 1];
if (!s)
throw("BAD MEMORY ALLOCATION");

strcpy(s, TD.s);
}
}

return *this;
}

Sir& Sir::operator=(const char* td)


{
if (this->s != td)
{
if (s)
delete[] s;

//s = NULL;

if (td)
{
this->s = new char[strlen(td) + 1];
if (!s)
throw("BAD MEMORY ALLOCATION");

strcpy(s, td);
}
}

return *this;
}

Sir Sir::operator+(const Sir& TD) const


{
char *rezultat = NULL;
int l1 = 0;
int l2 = 0;

if (s)
l1 = strlen(s);
if (TD.s)
l2 = strlen(TD.s);

if (l1 + l2)
{
rezultat = new char[l1 + l2 + 1];
if (!rezultat)
throw("BAD MEMORY ALLOCATION");
strcpy(rezultat, "");

if (s)
strcat(rezultat, s);

if (TD.s)
strcat(rezultat, TD.s);
}

return rezultat;

Sir Sir::operator+(const char* TD) const


{
char *rezultat = NULL;
int l1 = 0;
int l2 = 0;

if (s)
l1 = strlen(s);
if (TD)
l2 = strlen(TD);

if (l1 + l2)
{
rezultat = new char[l1 + l2 + 1];
if (!rezultat)
throw("BAD MEMORY ALLOCATION");

strcpy(rezultat, "");

if (s)
strcat(rezultat, s);

if (TD)
strcat(rezultat, TD);
}

return rezultat;

}
Sir& Sir::operator+=(const Sir& TD)
{
char *rezultat = NULL;
int l1 = 0;
int l2 = 0;

if (s)
l1 = strlen(s);
if (TD.s)
l2 = strlen(TD.s);

if (l1 + l2)
{
rezultat = new char[l1 + l2 + 1];
if (!rezultat)
throw("BAD MEMORY ALLOCATION");

strcpy(rezultat, "");

if (s)
strcat(rezultat, s);

if (TD.s)
strcat(rezultat, TD.s);
}

s = rezultat;
return *this;
}

ostream& operator<<(ostream& os, const Sir& A)


{
if (A.s)
os << A.s;

return os;
}

char& Sir::operator[](const int& i)


{
if (i >= 0 && i < strlen(s))
return s[i];
else
exit(5);
}

Sir::~Sir()
{
if (s)
delete[] s;
}

----

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "Sir.h"
using namespace std;

int main()
{
Sir A("Azi "), B(A),C(" soare "),D;

A = A + "e minunat";

cout << (const char*)A;


cout << endl;

B = " si e";
B += C;

D = A + B;

cout << D;

cout << D[0];

cout << endl;

Siruri AUX("iarba","apa");

AUX.Afisare();
cout << endl;
system("pause");
return 0;
}

You might also like