Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- using namespace std;
- class Sir
- {
- protected:
- char* s;
- public:
- Sir();
- Sir(char* nume);
- ~Sir();
- void Afisare();
- Sir operator+=(Sir& td);
- Sir& operator=( Sir& td);
- };
- Sir::Sir()
- {
- s = NULL;
- }
- Sir::Sir(char* s_nou)
- {
- s = new char[strlen(s_nou) + 1];
- strcpy(s, s_nou);
- }
- Sir::~Sir()
- {
- /* if(s)
- {
- delete [] s;
- }*/
- }
- void Sir::Afisare()
- {
- cout << s << endl;
- cout << endl;
- }
- Sir Sir::operator+=(Sir& td)
- {
- Sir rez;
- rez.s = new char[strlen(s)+strlen(td.s) + 1];
- strcpy(rez.s,s);
- strcat(rez.s,td.s);
- delete []s;
- s=new char[strlen(rez.s)+1];
- strcpy(s,rez.s);
- return s;
- }
- Sir& Sir::operator=(Sir& td)
- {
- if (this == &td)
- return *this;
- else
- {
- if (s)
- {
- delete[]s;
- }
- if (td.s)
- {
- s = new char[strlen(td.s) + 1];
- strcpy(s, td.s);
- }
- return *this;
- }
- }
- class SirNr :public Sir
- {
- public:
- SirNr():Sir(){}
- SirNr(char* nume):Sir(nume)
- {
- Numere(s);
- }
- SirNr& operator+=(SirNr& td)
- {
- Sir::operator+=(td);
- return *this;
- }
- SirNr& operator=(SirNr& td)
- {
- Sir::operator=(td);
- return *this;
- }
- void Numere(char *(&nr))
- {
- char *rez;
- int a=0,b=0;
- for(int i=0;i<strlen(nr);i++)
- {
- if(int(nr[i])>=48 && int(nr[i])<=57)
- a++;
- }
- rez=new char[a+1];
- for(int i=0;i<strlen(nr);i++)
- {
- if(int(nr[i])>=48 && int(nr[i])<=57)
- {
- rez[b]=nr[i];
- b++;
- }
- }
- rez[a]=NULL;
- nr=new char[a+1];
- strcpy(nr,rez);
- delete[] rez;
- }
- };
- int main()
- {
- SirNr A("masina22"),B("me1312"),C;
- A.Afisare();
- B.Afisare();
- A+=B;
- A.Afisare();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement