您的位置:首页 > 编程语言 > C语言/C++

C++ 构造函数使用new

2015-07-13 15:15 393 查看
C++Primer中的对于new的使用说明(懒得打字,就截图了 )





重写String类的代码
#include<iostream>
#include<cstring>
using namespace std;
const int ARSIZE = 10;
const int MAXLEN = 81;
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:

String(const char *s)
{
len = strlen(s);
cout<<"分配内存参数"<<endl;
str = new char[len + 1];
cout<<"分配内存后参数"<<endl;
strcpy(str,s);
num_strings++;
}

String()
{
len = 4;
cout<<"默认构造函数分配内存"<<endl;
str = new char[1];
cout<<"默认构造函数分配内存后"<<endl;
str[0] = '\0';
num_strings++;
}

String(const String &st)
{

num_strings ++;
len = st.len;
cout<<"复制构造分配内存"<<endl;
str = new char[len + 1];
cout<<"复制构造分配内存后"<<endl;
strcpy(str,st.str);
}

~String()
{
--num_strings;
cout<<"析构释放内存"<<endl;
delete [] str;
cout<<"析构释放内存后"<<endl;
}

int length() const
{
return len;
}

String &operator = (const String &st)
{

if(this == &st)
return *this;
// cout<<"=释放内存"<<endl;
// delete [] str;
// cout<<"=释放内存后"<<endl;
// len = st.len;
// cout<<"=分配内存"<<endl;
// str = new char[len +1];
// cout<<"=分配内存后"<<endl;
// strcpy(str,st.str);
// return *this;
memset(str,0,len+1);
memcpy(str,st.str,strlen(st.str));
return *this;
}

String &operator = (const char *s)
{
// cout<<"=释放内存"<<endl;
// delete [] str;
// cout<<"=释放内存后"<<endl;
// len = strlen(s);
// cout<<"=分配内存"<<endl;
// str = new char[len +1];
// cout<<"=分配内存后"<<endl;
// strcpy(str,s);
memset(str,0,len+1);
memcpy(str,s,strlen(s));
return *this;
}

char &operator[](int i)
{
return str[i];
}

const char & operator[](int i) const
{
return str[i];
}
friend bool operator<(const String &st1,const String &st2)
{
return (strcmp(st1.str,st2.str)<0);
}
friend bool operator>(const String &st1,const String &st2)
{
return st2<st1;
}
friend bool operator==(const String &st1,const String &st2)
{
return (strcmp(st1.str,st2.str) == 0);
}
friend ostream & operator<<(ostream &os,const String &st)
{
os << st.str;
return os;
}
friend istream & operator>>(istream & is,String &st)
{
cout<<""<<"进入这个方法"<<endl;
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
st = temp;
while(is && is.get() != '\n')
continue;
return is;
}
static int HowMany()
{
return num_strings;
}
};
int String::num_strings = 0;
int main()
{

// String name = "hahahaahahahah";
// cout<<name<<endl;
// cout<<name.length()<<endl;
// const char *pStr = "生活更美好";
// String theName(pStr);
// String myName = theName;
// myName = name;
// cout<<myName;

String name;
cin>>name;
cout<<name;
String sayings[ARSIZE];
char temp[MAXLEN];
int i;
for(i = 0;i < ARSIZE;i++)
{
cout<<endl;
cout<<i+1;
cout<<": ";
cin.get(temp,MAXLEN);
while(cin&&cin.get() != '\n')
continue;
if(!cin || temp[0] == '\0')
break;
else
sayings[i] = temp;
}

int total = i;
if(total > 0)
{
cout<<"Here are your sayings:\n";
for(i = 0;i < total ;i++)
{
cout<<sayings[i][0]<<":"<<sayings[i]<<endl;
}
int shortest = 0;
int first = 0;
for(i = 0;i<total;i++)
{
if(sayings[i].length() < sayings[shortest].length())
{
shortest = i;
}
if(sayings[i] < sayings[first])
{
first = i;
}
}
cout<<sayings[shortest]<<endl;
cout<<sayings[first]<<endl;
cout<<String::HowMany()<<endl;
}
else
{
cout<<"no input"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: