【面试题001-补充】C++ MyString类的封装
【面试题001-补充】C++ MyString类的封装
一,C++ MyString类的封装
String.h:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#ifndef _STRING_H_
#define _STRING_H_ #include <iostream> using namespace std; class String bool operator!() const; friend String operator+(const String &s1, const String &s2); friend ostream &operator<<(ostream &os, const String &str); void Display() const; private: #endif // _STRING_H_ |
String.cpp:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
#pragma warning(disable:4996)
#include "String.h" #include <string.h> //#include <iostream> //using namespace std; String::String(const char *str) String::String(const String &other) String &String::operator=(const String &other) return Assign(other.str_); String &String::operator=(const char *str) String &String::Assign(const char *str) bool String::operator!() const char &String::operator[](unsigned int index) return const_cast<char &>(static_cast<const String &>(*this)[index]); const char &String::operator[](unsigned int index) const String::~String() char *String::AllocAndCpy(const char *str) return newstr; void String::Display() const String operator+(const String &s1, const String &s2) String &String::operator+=(const String &other) delete[] str_; str_ = newstr; ostream &operator<<(ostream &os, const String &str) istream &operator>>(istream &is, String &str) |
main.cpp:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include "String.h"
#include <iostream> using namespace std; int main(void) char ch = s1[2]; s1[2] = 'A'; const String s2("xyzabc"); String s3 = "xxx"; String s5 = s3 + s4; String s6 = "aaa" + s3 + "sdfadfa" + "xxxx"; s3 += s4; cout << s3 << endl; String s7; |
Makefile:
|
1
2 3 4 5 6 7 8 9 10 11 12 |
.PHONY:clean
CPP=g++ CFLAGS=-Wall -g BIN=test OBJS=main.o String.o LIBS= $(BIN):$(OBJS) $(CPP) $(CFLAGS) $^ -o $@ $(LIBS) %.o:%.cpp $(CPP) $(CFLAGS) -c $< -o $@ clean: rm -f *.o $(BIN) |
运行结果:
|
1
2 3 4 5 6 7 8 9 |
c
abAdefg xyzabc xxxyyy aaaxxxsdfadfaxxxx xxxyyy xxxyyy adf adf |
【面试题001-补充】C++ MyString类的封装的更多相关文章
- MyString类的实现--基础中的基础C语言
MyString 类是学习 C++ 的过程中一个很重要的例子,涉及到面向对象的封装.堆内存申请和释放.函数的重载以及 C++ 的 “Big Three”.本例子重点在于复习和理解上述的 C++ 特性, ...
- 022医疗项目-模块二:药品目录的导入导出-对XSSF导出excel类进行封装
资源全部来源于传智播客. 好的架构师写的程序,就算给刚入门的新手看,新手一看就知道怎么去用.所以我们要对XSSF导出excel类进行封装.这是架构师的工作,但我们也要知道. 我们写一个封装类: 这个类 ...
- c++的类的封装/继承/多态的简单介绍
本篇文章仅仅从很表层来介绍一个C++语言中的类,包括什么是类,类的封装性/继承性和多态性.高手直接跳过吧,看了浪费时间,新手或者想温习一下的可以浏览看看. 1. 什么是类? 到底什么是类(class) ...
- C++学习之动态数组类的封装
动态数组(Dynamic Array)是指动态分配的.可以根据需求动态增长占用内存的数组.为了实现一个动态数组类的封装,我们需要考虑几个问题:new/delete的使用.内存分配策略.类的四大函数(构 ...
- iOS开发--QQ音乐练习,旋转动画的实现,音乐工具类的封装,定时器的使用技巧,SliderBar的事件处理
一.旋转动画的实现 二.音乐工具类的封装 -- 返回所有歌曲,返回当前播放歌曲,设置当前播放歌曲,返回下一首歌曲,返回上一首歌曲方法的实现 头文件 .m文件 #import "ChaosMu ...
- Java—类的封装、继承与多态
一.类和对象 1.类 类是数据以及对数据的一组操作的封装体. 类声明的格式: 类声明 { 成员变量的声明: 成员方法的声明及实现: } 1.1 声明类 [修饰符] class 类<泛型> ...
- 第三篇 :微信公众平台开发实战Java版之请求消息,响应消息以及事件消息类的封装
微信服务器和第三方服务器之间究竟是通过什么方式进行对话的? 下面,我们先看下图: 其实我们可以简单的理解: (1)首先,用户向微信服务器发送消息: (2)微信服务器接收到用户的消息处理之后,通过开发者 ...
- 025医疗项目-模块二:药品目录的导入导出-HSSF导入类的封装
上一篇文章提过,HSSF的用户模式会导致读取海量数据时很慢,所以我们采用的是事件驱动模式.这个模式类似于xml的sax解析.需要实现一个接口,HSSFListener接口. 原理:根据excel底层存 ...
- 黑马程序员——JAVA基础之简述 类的封装
------- android培训.java培训.期待与您交流! ---------- 类的封装(Encapsulation) 封装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 封装优 ...
随机推荐
- JS判断客户端是手机还是PC
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", " ...
- Assembly(程序集) 反射和缓存
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C程序调用shell脚本共有三种方法
C程序调用shell脚本共有三种法子 :system().popen().exec系列函数call_exec1.c ,内容为:system() 不用你自己去产生进程,它已经封装了,直接加入自己的命令e ...
- DBus接口文档
gitgit.projects.genivi.org / ipc / common-api-dbus-tools.git / blob? search: re 0544e985b6e4a6c83ddf ...
- mysql笔记整理
删除整个表 TRUNCATE TABLE 表名; 持久链接 自动提交
- 关于qt5在win7下发布 & 打包
QT5 发布时,莫过于依赖动态链接库(dll) , 但是,QT5的动态链接库貌似都有2套 ,例如 Qt5Core (针对realese) , Qt5Cored (针对debug) ,凡事末尾带d的都是 ...
- UML 小结(3)- UML的结构及各个阶段的应用
UML的结构: 其中各个图的作用如下: 用例图:用来描述用户的需求,从用户的角度描述系统的功能,并指出各功能的执行者,强调谁在使用系统,系统为执行者完成哪些功能. 静态图包括类图跟对象图 类图 ...
- Css compatibility
http://meiert.com/en/indices/css-properties/ http://www.standardista.com/css3/css3-selector-browser- ...
- listview中getview异步加载网络图片
前言:本以为异步加载挺简单,因为网上代码多,但真想要做好,还真不那么简单,从看代码到弄懂再到自己写,实在是有太多的东西需要学了,用了两天的时间,终于弄出来了,因为用到回调函数,所以理解起来可能难度有点 ...
- SQL Server数据库事务日志序列号(LSN)介绍
原文:http://blog.csdn.net/tjvictor/article/details/5251463 日志序列编号(LSN)是事务日志里面每条记录的编号. 当你执行一次备份时,一些 ...