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;
}
重写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;
}
相关文章推荐
- c++变量的作用域、生存期和可见性
- char使用的常见问题
- C语言基础scanf的用法
- 【protobuf 001】 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
- C语言概述
- C++11 lambda 表达式
- C语言运算符优先级列表
- C++中函数调用时的三种参数传递方式
- C++ COM组件相关
- c++动态库中使用命名空间的问题
- 使用 C++ 处理 JSON 数据交换格式
- DLL与LIB库文件的生成和使用---C++
- C语言学习进程:fseek与ftell函数
- C/C++局部静态变量
- 数组--C语言中关于数组的小问题
- C语言gmtime()函数:获取当前时间和日期
- C++面向对象模型
- C++ 字符串编程训练4
- Visual C++中的编译器优化
- C++ 字符串编程训练3