自己实现 String 类
实现Stirng类:普通构造、复制构造、赋值函数、重载输出函数 <<(友元)
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std; class String
{
public:
String (const char *str=NULL); //普通构造函数
String (const String &other); //复制构造函数
~String (void); //析构函数
String &operator=(const String &other); //赋值函数
friend ostream &operator<<(ostream &output, const String &other); //输出函数
private:
char *data; //保存字符串
}; //普通构造函数
String::String(const char *str)
{
if (str == NULL)
{
data = new char[];
*data = '\0';
}
else
{
int len = strlen(str);
data = new char[len+];
strcpy(data, str);
}
} //复制构造函数
String::String(const String &other)
{
int len = strlen(other.data);
data = new char[len+];
strcpy(data, other.data);
} //析构函数
String::~String()
{
delete[] data;
} //赋值函数
String &String::operator=(const String &other)
{
if (this == &other)
{
return *this;
}
delete[] data; int len = strlen(other.data);
data = new char[len+];
strcpy(data, other.data); return *this;
} //输出函数
ostream &operator<<(ostream &output, const String &other)
{
output<<other.data;
return output;
} int main()
{
String s1("hello");
cout << s1 << endl;
String s2=s1;
cout << s2 << endl;
String s3;
s3=s1;
cout << s3 << endl;
}
自己实现 String 类的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- ICE的Glacier2使用
1.使用Glacier2的步骤: A.编写一个Glacier2的配置文件,参见样例 B.设置Glacier2的访问鉴权(密码或者证书),passwords文件每行样例"test xx ...
- unix的输入输出操作
unix的输入输出操作 使用的头文件 #include <unistd.h> #include <stdio.h> 函数说明 ssize_t read(int fd, void ...
- grpc asp.net core 集成时一些配置的说明
一 什么是grpc google出了一款分布式通讯框架:grpc.我想这也不是新的东西了,在13年的一个项目中,用在了数据层和业务端之间的通讯上,当时并没有觉得怎么样,因为wcf很轻松的也可以可以实 ...
- YII1.1分页
一.控制器 $criteria = new CDbCriteria(); //这里可以加一些条件 $criteria->addCondition('parent_id='.$this->c ...
- 深入理解Spring IOC
转载自 http://www.cnblogs.com/xdp-gacl/p/4249939.html 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概 ...
- Makefile生成
安装:sudo yum install automake使用: 1 运行autoscan生成两个文件:autoscan.log和configure.scan.将configure.scan重命名为co ...
- 使用base64对图片的加密解密
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOu ...
- 查看,修改ceph节点的ceph配置命令
标签(空格分隔): ceph,ceph运维,ceph配置 查看ceph配置 1. 查看ceph默认配置: # ceph --show-config 2. 查看 type.num 的ceph默认配置: ...
- __align(num) 分析
这几天用2440读写SD卡(FAT32文件系统),定义了个文件信息的数据结构里边数据类型有unsigned char, unsigned int, unsigned long几种,在从SD卡上读取数据 ...
- 2016.2.13 (年初六) oracle两张表update方法
A表customers和B表tmp_cust_city有3个相同字段, customer_id,city_name,customer_type 现要根据b表更新a表 更新一个字段情况: update ...