![]() |
#2
ryzenshaw2018-03-06 20:23
|
代码如下

#pragma once
#ifndef STRING0_H_
#define STRING0_H_
#include<iostream>
class String {
private:
char *str;//存储数据
int len; //记录数据长度
static int num_strings; //静态成员变量,计算类对象的数量
static const int CINLIM = 80; //静态常量 重载输入函数时用来做char数组的常量
public:
String(); //默认构造函数
String(const char *c); //用字符串常量初始化的构造函数
~String(); //析构函数
String(const String &s); //复制构造函数
int length() { return len; }//返回长度
String& operator=(const String &s);//重载运算符=,提高效率
String& operator=(const char *c); //重载运算符=,提高效率,不需要临时创建和删除对象
friend std::ostream& operator<<(std::ostream &os,const String &s);//输出函数
friend std::istream& operator>>(std::istream &is, String &s);//输入函数
bool operator<(const String &s)const {
return(std::strcmp(str, s.str) < 0);
}//比较函数,推荐用 friend bool operator<(const String &s1,const String &2){return (std::strcmp(s1.str,s2.str)<0);} 代替
bool operator>(const String &s)const {
return(std::strcmp(str, s.str) > 0);
}//比较函数
friend bool operator==(const String &s1, const String &s2)
{
return (std::strcmp(s1.str, s2.str) == 0);
}//比较函数,可以用友元函数来实现
char & operator[](int i);//如数组一样访问字符的函数,对非const对象使用
const char& operator[](int i)const;//如数组一样访问字符串的函数,对const对象使用
static int howmany();//静态成员函数,无法使用对象来调用,只能用类与作用域解析运算式来调用,如 int count = String::howmany();
};
#endif // !STRING0_H_
#ifndef STRING0_H_
#define STRING0_H_
#include<iostream>
class String {
private:
char *str;//存储数据
int len; //记录数据长度
static int num_strings; //静态成员变量,计算类对象的数量
static const int CINLIM = 80; //静态常量 重载输入函数时用来做char数组的常量
public:
String(); //默认构造函数
String(const char *c); //用字符串常量初始化的构造函数
~String(); //析构函数
String(const String &s); //复制构造函数
int length() { return len; }//返回长度
String& operator=(const String &s);//重载运算符=,提高效率
String& operator=(const char *c); //重载运算符=,提高效率,不需要临时创建和删除对象
friend std::ostream& operator<<(std::ostream &os,const String &s);//输出函数
friend std::istream& operator>>(std::istream &is, String &s);//输入函数
bool operator<(const String &s)const {
return(std::strcmp(str, s.str) < 0);
}//比较函数,推荐用 friend bool operator<(const String &s1,const String &2){return (std::strcmp(s1.str,s2.str)<0);} 代替
bool operator>(const String &s)const {
return(std::strcmp(str, s.str) > 0);
}//比较函数
friend bool operator==(const String &s1, const String &s2)
{
return (std::strcmp(s1.str, s2.str) == 0);
}//比较函数,可以用友元函数来实现
char & operator[](int i);//如数组一样访问字符的函数,对非const对象使用
const char& operator[](int i)const;//如数组一样访问字符串的函数,对const对象使用
static int howmany();//静态成员函数,无法使用对象来调用,只能用类与作用域解析运算式来调用,如 int count = String::howmany();
};
#endif // !STRING0_H_

#include "stdafx.h"
#include"String0.h"
#include<cstring>
#include<cstdlib>
using std::cout;
int String::num_strings = 0;
String::String() {
len = 4;
str = new char[4];
strcpy_s(str,4, "C++");//strcpy函数不在这里定义,所以需要std作用域,char* strcpy(char*,const char*);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
}
String::String(const char* c){
len = std::strlen(c);
str = new char[len + 1];
strcpy_s(str,len+1, c);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
}
String::String(const String &s) {
len = s.len;
str = new char[len + 1];
strcpy_s(str,len+1, s.str);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
}
String::~String() {
cout << "\"" << str << "\"object deleted,";
num_strings--;
cout << num_strings << "left\n";
delete[] str;
}
String& String::operator=(const String &s) {
if (this == &s)
return *this;
else {
delete[]str;//这一步必不可少,必须先释放里面的内存,在为其分配足够的内存
len = s.len;
str = new char[len + 1];
strcpy_s(str,len+1,s.str);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
}
return *this;
}
String& String::operator=(const char* c) {
delete[]str;
len = std::strlen(c);
str = new char[len + 1];
strcpy_s(str, len + 1, str);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
return *this;
}
std::ostream& operator<<(std::ostream & os, const String &s) {
os << s.str;
return os;
}
std::istream& operator>>(std::istream & is, String &s) {
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is) {
s = temp;//重载赋值函数实现
}
while (is&&is.get() != '\n')continue;
return is;
}
char& String::operator[](int i) {
return str[i];
}
const char& String::operator[](int i)const {
return str[i];
}
int String::howmany()
{
return num_strings;
}
// TODO: 在 STDAFX.H 中引用任何所需的附加头文件,
//而不是在此文件中引用
#include"String0.h"
#include<cstring>
#include<cstdlib>
using std::cout;
int String::num_strings = 0;
String::String() {
len = 4;
str = new char[4];
strcpy_s(str,4, "C++");//strcpy函数不在这里定义,所以需要std作用域,char* strcpy(char*,const char*);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
}
String::String(const char* c){
len = std::strlen(c);
str = new char[len + 1];
strcpy_s(str,len+1, c);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
}
String::String(const String &s) {
len = s.len;
str = new char[len + 1];
strcpy_s(str,len+1, s.str);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
}
String::~String() {
cout << "\"" << str << "\"object deleted,";
num_strings--;
cout << num_strings << "left\n";
delete[] str;
}
String& String::operator=(const String &s) {
if (this == &s)
return *this;
else {
delete[]str;//这一步必不可少,必须先释放里面的内存,在为其分配足够的内存
len = s.len;
str = new char[len + 1];
strcpy_s(str,len+1,s.str);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
}
return *this;
}
String& String::operator=(const char* c) {
delete[]str;
len = std::strlen(c);
str = new char[len + 1];
strcpy_s(str, len + 1, str);
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
return *this;
}
std::ostream& operator<<(std::ostream & os, const String &s) {
os << s.str;
return os;
}
std::istream& operator>>(std::istream & is, String &s) {
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is) {
s = temp;//重载赋值函数实现
}
while (is&&is.get() != '\n')continue;
return is;
}
char& String::operator[](int i) {
return str[i];
}
const char& String::operator[](int i)const {
return str[i];
}
int String::howmany()
{
return num_strings;
}
// TODO: 在 STDAFX.H 中引用任何所需的附加头文件,
//而不是在此文件中引用

// String(类和动态内存分配笔记).cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include"String0.h"
const int NUM = 10;
using namespace std;
int main()
{
String name;
cout << "Hi,what's your name?\n>>";
cin >> name;//输入姓名,就是到这一步就跳出了这个窗口
cout << "Misty Gutz,please enter up to 10 short sayings <empty line to quit>:";
char*temp=NULL;
int i = 1;
String *sayings[NUM];//创建一个指针数组
while(cin>>temp){
sayings[i] = new String(temp);
cout << String::howmany()<< ":" << *sayings[i];
}
cout << "Here are your sayings:";
for (int i = 0; i < String::howmany(); i++) {
cout << sayings[i][0] << ":" << *sayings[i] << endl;
}
String first = *sayings[0];
String Short;
int j,k=0;
for (j = 1; j <String::howmany(); j++) {
if (sayings[j]->length() < sayings[j - 1]->length())
k = j;
}//求最短的String对象
cout << "Shortest saying:";
cout << *sayings[j];
cout << "First alphabetically:" << endl;
cout << first[0];
cout << "This program used " << String::howmany() << "String objects.Bye";
return 0;
}
//
#include "stdafx.h"
#include<iostream>
#include"String0.h"
const int NUM = 10;
using namespace std;
int main()
{
String name;
cout << "Hi,what's your name?\n>>";
cin >> name;//输入姓名,就是到这一步就跳出了这个窗口
cout << "Misty Gutz,please enter up to 10 short sayings <empty line to quit>:";
char*temp=NULL;
int i = 1;
String *sayings[NUM];//创建一个指针数组
while(cin>>temp){
sayings[i] = new String(temp);
cout << String::howmany()<< ":" << *sayings[i];
}
cout << "Here are your sayings:";
for (int i = 0; i < String::howmany(); i++) {
cout << sayings[i][0] << ":" << *sayings[i] << endl;
}
String first = *sayings[0];
String Short;
int j,k=0;
for (j = 1; j <String::howmany(); j++) {
if (sayings[j]->length() < sayings[j - 1]->length())
k = j;
}//求最短的String对象
cout << "Shortest saying:";
cout << *sayings[j];
cout << "First alphabetically:" << endl;
cout << first[0];
cout << "This program used " << String::howmany() << "String objects.Bye";
return 0;
}