实现一个String类:
//learnc170.h
/* c++自动提供下面的成员函数 1:默认构造函数 2:默认析构函数 3:复制构造函数 4:赋值运算符 5:地址运算符 在构造函数中使用new时应注意: 1:在析构函数中使用delete 2:new和delete必须兼容,new对应delete,new[] 对应delete[] 3:如果有多个构造函数,必须以相同的方式使用new(要么都使用new,要么都使用new[]). */
#ifndef LEARNC_170
#define LEARNC_170
#include<iostream>
class String
{
private:
char *str;
int len;
static int num_setting;//静态变量
public:
String();
String(const char *s);
String(const String & st);//复制构造函数 调用时机,赋值或者返回对象的时候。
~String();
friend std::ostream & operator<<(std::ostream &os, const String &st);
String& operator=(const String &st);//赋值运算符重载
static int howMany() {//静态函数,只能访问静态变量
return num_setting;
}
};
#endif // !LEARNC_170
//learnc171.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include "learnc170.h"
using std::cout;
using std::endl;
int String::num_setting = 0;
String::String(const char *s) {
len = strlen(s);
str = new char[len+1];
std::strcpy(str, s);
num_setting++;
cout << num_setting << ":\"" << str << "\" object created String(const char *s)" << endl;
}
String::String() {
len = 4;
str = new char[4];
std::strcpy(str, "C++");
num_setting++;
cout << num_setting << ":\"" << str << "\" object created String()" << endl;
}
String::String(const String & st) {
len = strlen(st.str);
str = new char[len + 1];
std::strcpy(str, st.str);
num_setting++;
cout << num_setting << ":\"" << str << "\" object created String(const String & st)" << endl;
}
String::~String() {
cout << ":\"" << str << "\" object deleted ~String()" << endl;
num_setting--;
cout << num_setting << " lefted" << endl;
delete[] str;
}
std::ostream & operator<<(std::ostream & os, const String & st) {
os << st.str;
return os;
}
String& String::operator=(const String &st) {
cout << "operator=" << endl;
if (this == &st) {
return *this;
}
cout << ":\"" << str << "\" object deleted" << endl;
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
//learnc172.cpp
#include<iostream>
#include "learnc170.h"
using std::cout;
using std::endl;
const String& call(const String &st);
int main() {
{
String str1 = "str1";//调用构造函数
String str2 = String("str2");//调用构造函数
String str3("str3");//调用构造函数
cout << "str1:" << str1 << endl;
cout << "str2:" << str2 << endl;
cout << "str3:" << str3 << endl;
cout << "----------------------" << endl;
String str = call(str1);//引用传参,返回引用
cout << "str:" << str << endl;
cout << "**************************" << endl;
String str4 = str1;//调用复制构造函数,如果没有则调用默认复制构造函数
cout << "str4:" << str4 << endl;
cout << "++++++++++++++++++++++++++" << endl;
String str5;//调用默认构造函数 C++
str5 = str1;//调用赋值运算符重载函数
cout << "str5:" << str5 << endl;
cout << String::howMany() << endl;
cout << "exiting the block" << endl;
}
cout << "end of main" << endl;
return 0;
}
const String& call(const String &st) {
cout << "String pass by refrence :" << st << endl;
return st;
}
输出结果如下:
1:"str1" object created String(const char *s)
2:"str2" object created String(const char *s)
3:"str3" object created String(const char *s)
str1:str1
str2:str2
str3:str3
----------------------
String pass by refrence :str1
4:"str1" object created String(const String & st)
str:str1
**************************
5:"str1" object created String(const String & st)
str4:str1
++++++++++++++++++++++++++
6:"C++" object created String()
operator=
:"C++" object deleted
str5:str1
6
exiting the block
:"str1" object deleted ~String()
5 lefted
:"str1" object deleted ~String()
4 lefted
:"str1" object deleted ~String()
3 lefted
:"str3" object deleted ~String()
2 lefted
:"str2" object deleted ~String()
1 lefted
:"str1" object deleted ~String()
0 lefted
end of main