#include<iostream>
#include<string>
using namespace std;
/*Esempio di classe str derivata da string*/
class str : public string
{
public:
//costruttore di default
str():string()
{
}
//costruttore che passa attraverso il costruttore a parametri
str(const string& s):string(s)
{
}
/*overloaging operator <<*/
friend ostream& operator<<(ostream& o, string& s)
{
o<<"Stringa: "<<s;
return o;
}
/*metodo statico che explode una stringa in un array*/
public:
static str* explode(char c,str& s)
{
int i=0,j=0;
str* st=new str[100];
while(s[i]!='\0')
{
if(s[i]==c)
j++;
else
{
st[j]+=s[i];
}
i++;
}
return st;
}
};
int main()
{
str s("ciao");
cout<<s<<endl;
str h("ciao,com, va, bene,o");
str* f = str::explode(',',h);
int y=0;
while(f[y]!="\0")
{
cout<<f[y]<<endl;
y++;
}
}
Post Views:
29