当前位置: 首页 > news >正文

c++ code:(11)inheritance

/* 
输入
无
输出
1. abcd - efgh - abcd -
2. abcd -
3.
4. abcd - efgh -
5. efgh -
6. c
7. abcd -
8. ijAl -
9. ijAl - mnop
10. qrst - abcd -
11. abcd - qrst - abcd - uvw xyz
about
big
me
take
abcd
qrst - abcd -
*/
//vs 2015 编译过不了呢~  05/23 2019
#include <cstdlib>
#include <iostream>
using namespace std;
//#pragma warning(disable:2556)
int strlen(const char * s)
{
	int i = 0;
	for (; s[i]; ++i);
	return i;
}
void strcpy(char * d, const char * s)
{
	int i = 0;
	for (i = 0; s[i]; ++i)
		d[i] = s[i];
	d[i] = 0;
}
int strcmp(const char * s1, const char * s2)
{
	for (int i = 0; s1[i] && s2[i]; ++i) {
		if (s1[i] < s2[i])
			return -1;
		else if (s1[i] > s2[i])
			return 1;
	}
	return 0;
}
void strcat(char * d, const char * s)
{
	int len = strlen(d);
	strcpy(d + len, s);
}
class MyString
{
	//your code starts here
private:
	char * str;
	int size;
public:
	MyString() {
		str = new char[2]; //确保分配的是数组
		str[0] = 0;//既然是个字符串,里面起码也是个空串,不能让 str== NULL
		size = 0;
	}
	MyString(const char * s) {
		//如果 s == NULL,就让它出错吧
		size = strlen(s);
		str = new char[size + 1];
		strcpy(str, s);
	}
	MyString & operator=(const char * s) {//如果 s == NULL,就让它出错吧
		int len = strlen(s);
		if (size < len) {
			delete[] str;
			str = new char[len + 1];
		}
		strcpy(str, s);
		size = len;
		return *this;
	}
	void duplicate(const MyString & s) {
		if (size < s.size) { //否则就不用重新分配空间了
			delete[] str;
			str = new char[s.size + 1];
		}
		strcpy(str, s.str);
		size = s.size;
	}
	MyString(const MyString & s) :size(0), str(new char[1]) {
		duplicate(s);
	}
	MyString & operator=(const MyString & s) {
		if (str == s.str)
			return *this;
		duplicate(s);
		return *this;
	}
	bool operator==(const MyString & s) const {
		return strcmp(str, s.str) == 0;
	}
	bool operator<(const MyString & s) const {
		return strcmp(str, s.str) < 0;
	}
	bool operator>(const MyString & s) const {
		return strcmp(str, s.str) > 0;
	}
	MyString operator + (const MyString & s) {
		char * tmp = new char[size + s.size + 2];//确保能分配一个数组
			strcpy(tmp, str);
		strcat(tmp, s.str); MyString os(tmp);
		delete[] tmp;
		return os;
	}
	MyString & operator += (const MyString & s) {
		char * tmp = new char[size + s.size + 2];
		strcpy(tmp, str);
		strcat(tmp, s.str);
		size += s.size;
		delete[] str;
		str = tmp;
		return *this;
	}
	char & operator[](int i) const {
		return str[i];
	}
	MyString operator()(int start, int len) const {
		char * tmp = new char[len + 1];
		for (int i = 0; i < len; ++i)
			tmp[i] = str[start + i];
		tmp[len] = 0;
		MyString s(tmp);
		delete[] tmp;
		return s;
	}
	~MyString() { delete[] str; }
	friend ostream & operator << (ostream & o, const MyString & s)
	{
		o << s.str;
		return o;
	}
	friend MyString operator +(const char * s1, const MyString & s2)
	{
		MyString tmp(s1);
		tmp += s2;
		return tmp;
	}
	//your code ends here
}; int CompareString(const void * e1, const void * e2)
{
	MyString * s1 = (MyString *)e1;
	MyString * s2 = (MyString *)e2;
	if (*s1 < *s2)
		return -1;
	else if (*s1 == *s2)
		return 0;
	else if (*s1 > *s2)
		return 1;
}
int main()
{
	MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);
	MyString SArray[4] = { "big","me","about","take" };
	cout << "1. " << s1 << s2 << s3 << s4 << endl;
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A';
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	qsort(SArray, 4, sizeof(MyString), CompareString);
	for (int i = 0; i < 4; i++)
		cout << SArray[i] << endl;
	//s1 的从下标 0 开始长度为 4 的子串
	cout << s1(0, 4) << endl;
	//s1 的从下标 5 开始长度为 10 的子串
	cout << s1(5, 10) << endl;
	return 0;
}

 

 

#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class MyString :public string
{
public:
	MyString() :string() {};
	MyString(const char* s) :string(s) {};
	MyString(const string &s) :string(s) {};
	MyString operator()(int s, int l)
	{
		return substr(s, l);
	}

};
int main()
{
	MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);
	MyString SArray[4] = { "big","me","about","take" };
	cout << "1. " << s1 << s2 << s3 << s4 << endl;
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A';
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop"; cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	sort(SArray, SArray + 4);
	for (int i = 0; i < 4; i++)
		cout << SArray[i] << endl;
	//s1 的从下标 0 开始长度为 4 的子串
	cout << s1(0, 4) << endl;
	//s1 的从下标 5 开始长度为 10 的子串
	cout << s1(5, 10) << endl;
	return 0;
}

 

相关文章:

  • c++ code:(12)polymorphism
  • c++ code:(13)io and template
  • 英文论文写作素材
  • 王梓坤院士的学术思想
  • 使用EI、SCI搜索 (使用 websci中的endnote online引用 )
  • 玩转word 和 mathtype 的结合
  • paper 格式
  • 计算思维
  • 程序中的一些思想
  • 深入 理解char * ,char ** ,char a[ ] ,char *a[] 的区别
  • STL MAP四种插入方法
  • c++ STL函数大全(一):vector
  • c++ STL函数大全(二):map
  • c++ STL函数大全(三):queue
  • c++ STL函数大全(四):string
  • JavaScript中的对象个人分享
  • java中的hashCode
  • linux安装openssl、swoole等扩展的具体步骤
  • PermissionScope Swift4 兼容问题
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • spring boot下thymeleaf全局静态变量配置
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 从零开始的无人驾驶 1
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 观察者模式实现非直接耦合
  • 两列自适应布局方案整理
  • 前端性能优化——回流与重绘
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • 责任链模式的两种实现
  • Spring Batch JSON 支持
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • 带你开发类似Pokemon Go的AR游戏
  • (1)Nginx简介和安装教程
  • (3)(3.5) 遥测无线电区域条例
  • (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)...
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)ssm经济信息门户网站 毕业设计 141634
  • (一) springboot详细介绍
  • **CI中自动类加载的用法总结
  • .net core使用ef 6
  • .Net转前端开发-启航篇,如何定制博客园主题
  • [ACTF2020 新生赛]Include
  • [android] 看博客学习hashCode()和equals()
  • [bbk5179]第66集 第7章 - 数据库的维护 03
  • [Big Data - Kafka] kafka学习笔记:知识点整理
  • [C#] 我的log4net使用手册
  • [flink总结]什么是flink背压 ,有什么危害? 如何解决flink背压?flink如何保证端到端一致性?
  • [GN] Vue3快速上手1
  • [HNOI2018]排列
  • [J2ME]url请求返回参数非法(java.lang.illegalArgument)
  • [linux]资料收纳
  • [MRCTF2020]Ez_bypass1