【c++】实现"String"类,完成运算符重载等

本文介绍如何在C++环境下,利用构造函数和运算符重载技术实现一个自定义的`String`类。通过示例代码展示了在Linux和VS2012编译器下的操作过程及预期结果。
摘要由CSDN通过智能技术生成

编译环境:linux,vs2012

Main.cpp

#include <iostream>
#include "String.h"

using namespace std;

int main()
{
	String s1("hello"); //验证转换构造函数
	s1.Display();

	String s2(s1); //验证拷贝构造函数
	s2.Display();

	String s3;
	s3 = s1;  //"=" 运算符重载
	s3.Display();

	String s4;
	s4 = "world"; //"="运算符重载
	s4.Display();

	cout << s4[4] << endl; //"[]"重载
	
	s4 += s3;  //"+="运算符重载
	s4.Display();

	String s5;
	s5 = s1 + s3; //"+"运算符重载
	s5.Display();

	String s6("my cout");
	cout << s6 << endl; //"<<"流运算符重载

	String s7;
	cin >> s7; //">>"流运算符重载
	cout << s7 << endl;
	
	char *newptr = static_cast<char *>(s2); //强制类型转换重载
	cout << newptr << endl;

	return 0;
}

String.h
#ifndef _STRING
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值